Created
May 3, 2020 17:32
Gitlab CI/CD to deploy a Spring Boot application in AWS ECS with auto service restart
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
image: docker:latest | |
variables: | |
REGION: [aws region] | |
REPOSITORY_URL: [your ECR repository url] | |
REPOSITORY_NAME: [repository name] | |
UAT_CLUSTER_NAME: [ECS Cluster name] | |
UAT_SERVICE_NAME: [service name] | |
UAT_TASK_DEFINTION_NAME: [task definition name] | |
EXECUTION_ROLE: [ecsTaskExecutionRole] | |
TASK_ROLE: [ecsTaskExecutionRole] | |
services: | |
- docker:19.03.5-dind | |
before_script: | |
- apk add --no-cache curl jq python py-pip | |
- pip install awscli | |
- $(aws ecr get-login --no-include-email --region "${REGION}") | |
- IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)" | |
stages: | |
- test | |
- build | |
- deploy | |
test: | |
stage: test | |
script: echo "Running Test Cases" | |
build: | |
stage: build | |
script: | |
- echo "Deleting old images..." | |
- IMAGES_TO_DELETE=$( aws ecr list-images --region "${REGION}" --repository-name $REPOSITORY_NAME --filter tagStatus=UNTAGGED --query 'imageIds[*]' --output json ) | |
- echo $IMAGES_TO_DELETE | |
- aws ecr batch-delete-image --region "${REGION}" --repository-name $REPOSITORY_NAME --image-ids "$IMAGES_TO_DELETE" || true | |
- echo "Building image..." | |
- docker build -f ./Dockerfile -t $REPOSITORY_URL:latest . | |
- echo "Tagging image..." | |
- docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$IMAGE_TAG | |
- echo "Pushing image..." | |
- $(aws ecr get-login --no-include-email --region "${REGION}") | |
- docker push $REPOSITORY_URL:latest | |
only: | |
- master | |
deploy_uat: | |
stage: deploy | |
script: | |
- echo $REPOSITORY_URL:$IMAGE_TAG | |
- TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$UAT_TASK_DEFINTION_NAME" --region "${REGION}") | |
- NEW_CONTAINER_DEFINTIION=$(echo $TASK_DEFINITION | python $CI_PROJECT_DIR/update_task.py $REPOSITORY_URL:latest) | |
- echo "Registering new container definition...." | |
- echo "${NEW_CONTAINER_DEFINTIION}" | |
- aws ecs register-task-definition --region "${REGION}" --execution-role-arn "${EXECUTION_ROLE}" --task-role-arn "${TASK_ROLE}" --cpu 256 --memory 512 --network-mode "awsvpc" --requires-compatibilities "FARGATE" --family "${UAT_TASK_DEFINTION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}" | |
- echo "Updating the service..." | |
- aws ecs update-service --region "${REGION}" --cluster "${UAT_CLUSTER_NAME}" --service "${UAT_SERVICE_NAME}" --task-definition "${UAT_TASK_DEFINTION_NAME}" | |
only: | |
- master | |
#Production: | |
# stage: deploy | |
# script: | |
# - echo $REPOSITORY_URL:$IMAGE_TAG | |
# - TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$PROD_TASK_DEFINTION_NAME" --region "${REGION}") | |
# - NEW_CONTAINER_DEFINTIION=$(echo $TASK_DEFINITION | python $CI_PROJECT_DIR/update_task_definition_image.py $REPOSITORY_URL:latest) | |
# - echo "Registering new container definition...." | |
# - echo "${NEW_CONTAINER_DEFINTIION}" | |
# - aws ecs register-task-definition --region "${REGION}" --execution-role-arn "${EXECUTION_ROLE}" --task-role-arn "${TASK_ROLE}" --network-mode "awsvpc" --requires-compatibilities "EC2" --family "${PROD_TASK_DEFINTION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}" | |
# - echo "Updating the service..." | |
# - aws ecs update-service --region "${REGION}" --cluster "${PROD_CLUSTER_NAME}" --service "${PROD_SERVICE_NAME}" --task-definition "${PROD_TASK_DEFINTION_NAME}" | |
# only: | |
# - master | |
# when: manual |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Content of update_task.py