Skip to content

Instantly share code, notes, and snippets.

@juzn01
Forked from Thakurvaibhav/sample-gitlab-ci.yaml
Created November 18, 2020 08:56
Show Gist options
  • Save juzn01/96337bf8639f3da9230af98b86ef850a to your computer and use it in GitHub Desktop.
Save juzn01/96337bf8639f3da9230af98b86ef850a to your computer and use it in GitHub Desktop.
#All the environment variables are defined under Settings > CI/CD > Secret Variables section
image: docker:latest
variables:
DOCKER_DRIVER: overlay2
services:
- docker:dind
stages:
- build_dev
- build_prod
- deploy_dev
- deploy_prod
before_script:
- docker login $REGISTRY -u $REGISTRY_USER -p $REGISTRY_USER_PASSWORD
build_dev:
stage: build_dev
only:
- /^dev-.*$/
except:
- branches
script:
- docker build --no-cache -t "$REGISTRY/$REPO_DEV:$CI_BUILD_REF_NAME" .
- docker push $REGISTRY/$REPO_DEV:$CI_BUILD_REF_NAME
build_prod:
stage: build_prod
only:
- /^prod-.*$/
except:
- branches
script:
- docker build --no-cache -t "$REGISTRY/$REPO_DEV:$CI_BUILD_REF_NAME" .
- docker push $REGISTRY/$REPO_DEV:$CI_BUILD_REF_NAME
deploy_dev:
stage: deploy_dev
script:
# Install python requirements
- apk update
- apk upgrade
- apk add util-linux pciutils usbutils coreutils binutils findutils grep
- apk add python python-dev py-pip
# AWS configs
- export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY
- export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_KEY
# Install awscli
- pip install awscli
# Configure deploy.json
- cd deployment
- sed -i -e "s/N_ENV/$NODE_ENV_DEVELOPMENT/g" deploy.json
- sed -i -e "s/REPO/$REPO_DEV/g" deploy.json
- sed -i -e "s/TAG/$CI_BUILD_REF_NAME/g" deploy.json
# Update task and service
- aws ecs register-task-definition --region <your-ecs-region> --cli-input-json file://deploy.json >> temp.json
- REV=`grep '"revision"' temp.json | awk '{print $2}'`
- echo $REV
- aws ecs update-service --cluster <ecs-cluster-name> --service <ecs-service-name> --task-definition <your-ecs-task-name>:${REV} --region <your-ecs-region>
when: manual
deploy_prod:
stage: deploy_prod
script:
# Install python requirements
- apk update
- apk upgrade
- apk add util-linux pciutils usbutils coreutils binutils findutils grep
- apk add python python-dev py-pip
# AWS configs
- export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY
- export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_KEY
# Install awscli
- pip install awscli
# Configure deploy.json
- cd deployment
- sed -i -e "s/N_ENV/$NODE_ENV_PRODUCTION/g" deploy.json
- sed -i -e "s/REPO/$REPO_PROD/g" deploy.json
- sed -i -e "s/TAG/$CI_BUILD_REF_NAME/g" deploy.json
# Update task and service
- aws ecs register-task-definition --region <your-ecs-region> --cli-input-json file://deploy.json >> temp.json
- REV=`grep '"revision"' temp.json | awk '{print $2}'`
- echo $REV
- aws ecs update-service --cluster <ecs-cluster-name> --service <ecs-service-name> --task-definition <your-ecs-task-name>:${REV} --region <your-ecs-region>
when: manual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment