Skip to content

Instantly share code, notes, and snippets.

@kutzhanov
Last active September 11, 2024 15:19
Show Gist options
  • Save kutzhanov/192da0dd27805b9b710ef02c7c51ec08 to your computer and use it in GitHub Desktop.
Save kutzhanov/192da0dd27805b9b710ef02c7c51ec08 to your computer and use it in GitHub Desktop.
Deploy Docker container into AWS ECS Fargate using Gitlab CI
image: docker:19.03
variables:
REPOSITORY_URL: <AWS_ACCOUNT_ID>.dkr.ecr.<REGION_NAME>.amazonaws.com/<ECR_REPOSITORY_NAME>
REGION: <REGION_NAME>
TASK_DEFINITION_NAME: <TASK_DEFINITION_NAME>
CLUSTER_NAME: <CLUSTER_NAME>
SERVICE_NAME: <SERVICE_NAME>
CPU: <CPU>
MEMORY: <MEMORY>
TASKS_COUNT: <DESIRED_TASKS_COUNT>
EXECUTION_ROLE_ARN: arn:aws:iam::<AWS_ACCOUNT_ID>:role/ecsTaskExecutionRole
AWS_ACCESS_KEY_ID: <AWS_ACCESS_KEY_ID>
AWS_SECRET_ACCESS_KEY: <AWS_SECRET_ACCESS_KEY>
services:
- docker:19.03.1-dind
before_script:
- apk add --no-cache python py-pip
- pip install awscli
- IMAGE_TAG="$CI_COMMIT_SHORT_SHA"
stages:
- build
- deploy
build:
stage: build
script:
- echo "Building image..."
- docker build -t $REPOSITORY_URL:latest .
- echo "Tagging image..."
- IMAGE_TAG="$CI_COMMIT_SHORT_SHA"
- docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$IMAGE_TAG
- echo "Pushing image..."
- docker push $REPOSITORY_URL:latest
- docker push $REPOSITORY_URL:$IMAGE_TAG
only:
- master
tags:
- docker
deploy:
stage: deploy
script:
- apk add --no-cache jq
- TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_DEFINITION_NAME" --region "${REGION}")
- NEW_CONTAINER_DEFINTIION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$REPOSITORY_URL:$IMAGE_TAG" '.taskDefinition.containerDefinitions[0].image = $IMAGE | .taskDefinition.containerDefinitions')
- echo "Registering new container definition..."
- aws ecs register-task-definition --region "${REGION}" --family "${TASK_DEFINITION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}" --cpu ${CPU} --memory ${MEMORY} --execution-role-arn "${EXECUTION_ROLE_ARN}" --network-mode "awsvpc" --requires-compatibilities "FARGATE"
- TASK_ARN=$(aws ecs list-tasks --region "${REGION}" --cluster "${CLUSTER_NAME}" --service-name "${SERVICE_NAME}")
- TASK_ID=$(echo ${TASK_ARN:72:36})
- echo "Updating the service..."
- aws ecs update-service --region "${REGION}" --cluster "${CLUSTER_NAME}" --service "${SERVICE_NAME}" --task-definition "${TASK_DEFINITION_NAME}" --desired-count "${TASKS_COUNT}"
- echo "Waiting until the previous task ${TASK_ID} is stopped..."
- aws ecs wait tasks-stopped --region "${REGION}" --cluster "${CLUSTER_NAME}" --tasks "${TASK_ID}"
- echo "The previous task ${TASK_ID} has stopped."
- TASK_ARN=$(aws ecs list-tasks --region "${REGION}" --cluster "${CLUSTER_NAME}" --service-name "${SERVICE_NAME}")
- TASK_ID=$(echo ${TASK_ARN:72:36})
- echo "New task ${TASK_ID} is running."
- aws ecs describe-tasks --region "${REGION}" --cluster "${CLUSTER_NAME}" --tasks "${TASK_ID}"
only:
- master
tags:
- docker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment