Skip to content

Instantly share code, notes, and snippets.

@dvsingh9
Created May 3, 2020 17:32
Show Gist options
  • Save dvsingh9/d9e3c6c417a1816c26c770b1ac21ce49 to your computer and use it in GitHub Desktop.
Save dvsingh9/d9e3c6c417a1816c26c770b1ac21ce49 to your computer and use it in GitHub Desktop.
Gitlab CI/CD to deploy a Spring Boot application in AWS ECS with auto service restart
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
@dvsingh9
Copy link
Author

dvsingh9 commented May 3, 2020

Content of update_task.py

`import sys, json, argparse

parser = argparse.ArgumentParser('Replaces image in the task definition')
parser.add_argument('image_uri', metavar='I', type=str, nargs='+',
                   help='The new image URI')

args = parser.parse_args()

definition = json.load(sys.stdin)['taskDefinition']['containerDefinitions']
definition[0]['image'] = args.image_uri[0]

definition[0]['cpu'] =256
definition[0]['memory'] =512

print json.dumps(definition)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment