Skip to content

Instantly share code, notes, and snippets.

@enginaltay
Created September 6, 2022 18:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginaltay/cc8b577b1c62322d42c8991ff1ce6b6d to your computer and use it in GitHub Desktop.
Save enginaltay/cc8b577b1c62322d42c8991ff1ce6b6d to your computer and use it in GitHub Desktop.
ecs-gitlab-ci.yml
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