Skip to content

Commit

Permalink
[xy] Add gitlab aws template (mage-ai#1742)
Browse files Browse the repository at this point in the history
* [xy] Add gitlab cicd template.

* [xy] Update branch.

* [xy] Update template.

* [xy] Fix compatibility

* [xy] Use json task definition as input.

* [xy] Remove extra fields.
  • Loading branch information
wangxiaoyou1993 authored Jan 13, 2023
1 parent 456e61e commit 05c75d1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions templates/gitlab_cicd/aws.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
image: docker:19.03.10

services:
- docker:dind

variables:
REPOSITORY_URL: <REPOSITORY_URL>
TASK_DEFINITION_NAME: <Task_Definition>
CLUSTER_NAME: <CLUSTER_NAME>
SERVICE_NAME: <SERVICE_NAME>

before_script:
- apk add --no-cache curl jq python py-pip
- pip install awscli
- $(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:
- main

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}")
- NEW_TASK_DEFINTIION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$REPOSITORY_URL:$IMAGE_TAG" '.taskDefinition | .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
- echo "Registering new container definition..."
- aws ecs register-task-definition --region "${AWS_DEFAULT_REGION}" --family "${TASK_DEFINITION_NAME}" --cli-input-json "${NEW_TASK_DEFINTIION}"
- echo "Updating the service..."
- aws ecs update-service --region "${AWS_DEFAULT_REGION}" --cluster "${CLUSTER_NAME}" --service "${SERVICE_NAME}" --task-definition "${TASK_DEFINITION_NAME}"
only:
- main

0 comments on commit 05c75d1

Please sign in to comment.