-
-
Save enginaltay/cc8b577b1c62322d42c8991ff1ce6b6d to your computer and use it in GitHub Desktop.
ecs-gitlab-ci.yml
This file contains 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
variables: | |
CONTAINER_IMAGE_NAME: myapp | |
REGISTRY: "<your_aws_account_number_here>.dkr.ecr.us-east-1.amazonaws.com" | |
ECS_CLUSTER_NAME: "myecs-preprod" | |
ECS_SERVICE_NAME: "myecs-preprod-svc" | |
ECS_TASK_FAMILY: "myapp-preprod-fargate" | |
ENV: | |
value: "preprod" | |
description: "Enter ENV parameter." | |
stages: | |
- build | |
- deploy-preprod | |
before_script: | |
- $(aws ecr get-login --no-include-email --region eu-central-1) | |
- echo "Build Name:" "$CI_JOB_NAME" | |
- echo "Branch:" "$CI_COMMIT_REF_NAME" | |
- echo "Build Stage:" "$CI_JOB_STAGE" # using gitlab predefined env | |
build: | |
stage: build | |
script: | |
- docker build -t ${CONTAINER_IMAGE_NAME} . | |
- docker tag ${CONTAINER_IMAGE_NAME} ${REGISTRY}/${CONTAINER_IMAGE_NAME}:${CI_ENVIRONMENT_NAME}-${CI_PIPELINE_IID} | |
- docker push ${REGISTRY}/${CONTAINER_IMAGE_NAME}:${CI_ENVIRONMENT_NAME}-${CI_PIPELINE_IID} | |
environment: | |
name: preprod | |
deploy_preprod: | |
stage: deploy-preprod | |
only: | |
refs: | |
- master | |
script: | |
- echo "ECS FARGATE Cluster is = ${ECS_CLUSTER_NAME}" | |
# replace "latest" with respectful container image version release tag | |
- sed -i 's/latest/'"${CI_ENVIRONMENT_NAME}-${CI_PIPELINE_IID}"'/g' myapp-ecs-task-definition.json | |
- cat myapp-ecs-task-definition.json | |
# Create New Task Definition and Get Revision Number | |
- export TASK_REVISION=$(aws ecs register-task-definition --family ${ECS_TASK_FAMILY} --cli-input-json file://myapp-ecs-task-definition.json --region eu-central-1 | jq --raw-output '.taskDefinition.revision') | |
- echo "Registered ECS Task Definition = " $TASK_REVISION | |
# Rolling Update | |
- echo "Updating ECS Cluster = " $ECS_CLUSTER_NAME | |
- echo "Updating Service = " $ECS_SERVICE_NAME | |
- echo "Updating Task Definition = " $ECS_TASK_FAMILY:$TASK_REVISION | |
- UPDATE_ECS_SERVICE=$(aws ecs update-service --cluster $ECS_CLUSTER_NAME --service $ECS_SERVICE_NAME --task-definition $ECS_TASK_FAMILY:$TASK_REVISION --desired-count 1 --region eu-central-1 | jq --raw-output '.service.serviceName') | |
- echo "Deployment of $UPDATE_ECS_SERVICE has been completed" | |
environment: | |
name: preprod |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment