-
-
Save mikamboo/f98233b03d0b10ebc1a33adb5260e43a to your computer and use it in GitHub Desktop.
Dockerizing a Node.js and MongoDB app with CI/CD Pipelines on Gitlab for staging and production environments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: docker:stable | |
variables: | |
DOCKER_DRIVER: overlay2 | |
CONTAINER_IMAGE: registry.gitlab.com/$CI_PROJECT_PATH | |
STAGE_CONTAINER: dev | |
PROD_CONTAINER: prod | |
DEV_FOLDER: /path/to/repo/dev | |
PROD_FOLDER: /path/to/repo/prod | |
services: | |
- docker:dind | |
stages: | |
- build | |
- stage | |
- deploy | |
build_dev: | |
only: | |
- dev | |
before_script: | |
- docker login --username gitlab-ci-token --password $CI_BUILD_TOKEN registry.gitlab.com | |
after_script: | |
- docker logout registry.gitlab.com | |
image: docker:stable | |
stage: build | |
script: | |
- docker build --tag $CONTAINER_IMAGE/$STAGE_CONTAINER:$CI_COMMIT_SHA --tag $CONTAINER_IMAGE/$STAGE_CONTAINER:latest . | |
- docker push $CONTAINER_IMAGE/$STAGE_CONTAINER:$CI_COMMIT_SHA | |
- docker push $CONTAINER_IMAGE/$STAGE_CONTAINER:latest | |
build_master: | |
only: | |
- master | |
before_script: | |
- docker login --username gitlab-ci-token --password $CI_BUILD_TOKEN registry.gitlab.com | |
after_script: | |
- docker logout registry.gitlab.com | |
image: docker:stable | |
stage: build | |
script: | |
- docker build --tag $CONTAINER_IMAGE/$PROD_CONTAINER:$CI_COMMIT_SHA --tag $CONTAINER_IMAGE/$PROD_CONTAINER:latest . | |
- docker push $CONTAINER_IMAGE/$PROD_CONTAINER:$CI_COMMIT_SHA | |
- docker push $CONTAINER_IMAGE/$PROD_CONTAINER:latest | |
stage: | |
only: | |
- dev | |
stage: stage | |
image: kroniak/ssh-client | |
script: | |
- mkdir -p ~/.ssh | |
- chmod 700 ~/.ssh | |
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config | |
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
- chmod 600 ~/.ssh/id_rsa | |
- ssh -i ~/.ssh/id_rsa $SSH_HOST "docker login --username gitlab-ci-token --password $CI_BUILD_TOKEN registry.gitlab.com && cd $DEV_FOLDER && git fetch --all && git reset --hard origin/dev && docker-compose pull $STAGE_CONTAINER && sudo service $STAGE_CONTAINER restart && docker logout registry.gitlab.com" | |
deploy: | |
only: | |
- master | |
stage: deploy | |
image: kroniak/ssh-client | |
script: | |
- mkdir -p ~/.ssh | |
- chmod 700 ~/.ssh | |
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config | |
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
- chmod 600 ~/.ssh/id_rsa | |
- ssh -i ~/.ssh/id_rsa $SSH_HOST "docker login --username gitlab-ci-token --password $CI_BUILD_TOKEN registry.gitlab.com && cd $PROD_FOLDER && git fetch --all && git reset --hard origin/master && docker-compose pull $PROD_CONTAINER && sudo service $PROD_CONTAINER restart && docker logout registry.gitlab.com" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment