Last active
March 10, 2021 18:53
-
-
Save ariel-diaz/bd39b4482f33074b522a35297cffb5b9 to your computer and use it in GitHub Desktop.
gitlab ci aws ecs
This file contains hidden or 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:19.03.10 | |
services: | |
- docker:dind | |
variables: | |
REPOSITORY_URL: <repository-url> | |
TASK_DEFINITION_NAME: <task-definition-name> | |
CLUSTER_NAME: <cluster-name> | |
SERVICE_NAME: <service-name | |
before_script: | |
- apk add --no-cache curl jq python py-pip | |
- pip install awscli | |
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID | |
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY | |
- aws configure set region $AWS_DEFAULT_REGION | |
- $(aws ecr get-login --no-include-email --region "${AWS_DEFAULT_REGION}") | |
- IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)" | |
stages: | |
- build | |
- deploy | |
build: | |
stage: build | |
script: | |
- echo "Building image..." | |
- docker build -t $REPOSITORY_URL:latest . | |
- echo "Tagging image..." | |
- docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$IMAGE_TAG | |
- echo "Pushing image..." | |
- docker push $REPOSITORY_URL:latest | |
- docker push $REPOSITORY_URL:$IMAGE_TAG | |
only: | |
refs: | |
- master | |
deploy: | |
stage: deploy | |
script: | |
- echo $REPOSITORY_URL:$IMAGE_TAG | |
- TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_DEFINITION_NAME" --region "${AWS_DEFAULT_REGION}") | |
- echo "${TASK_DEFINITION}" | |
- NEW_CONTAINER_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$REPOSITORY_URL:$IMAGE_TAG" '.taskDefinition.containerDefinitions[0].image = $IMAGE | .taskDefinition.containerDefinitions[0]') | |
## Register task | |
- echo "Registering new container definition..." | |
- aws ecs register-task-definition --region "${AWS_DEFAULT_REGION}" --family "${TASK_DEFINITION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINITION}" --memory 1024 --cpu 512 | |
## Update Service | |
- echo "Updating the service..." | |
- aws ecs update-service --region "${AWS_DEFAULT_REGION}" --cluster "${CLUSTER_NAME}" --service "${SERVICE_NAME}" --task-definition "${TASK_DEFINITION_NAME}" | |
only: | |
refs: | |
- master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment