Skip to content

Commit fd02770

Browse files
committed
feat(ci): setup container image management
Signed-off-by: Theo Bob Massard <tbobm@protonmail.com>
1 parent a497d09 commit fd02770

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

.github/workflows/migration.yaml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
env:
1515
AWS_REGION: eu-west-1
1616
CLUSTER_NAME: sample-cluster
17+
ECR_REPO: db-migration
1718
TASK_DEFINITION: db-migration-job
1819
SUBNET_ID: subnet-abc123
1920
SECURITY_GROUP_ID: sg-abc123
@@ -27,14 +28,33 @@ jobs:
2728
- name: Configure AWS credentials
2829
uses: aws-actions/configure-aws-credentials@v4
2930
with:
30-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsDeploymentRole
31-
aws-region: ${{ env.AWS_REGION }}
31+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33+
aws-region: ${{ vars.AWS_REGION }}
34+
35+
- name: Login to Amazon ECR
36+
id: login-ecr
37+
uses: aws-actions/amazon-ecr-login@v2
38+
39+
- name: Get container image tag (git hash)
40+
id: image-vars
41+
run: |
42+
echo "image-uri=${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_REPO }}:${GITHUB_SHA::8}" >> "$GITHUB_OUTPUT"
43+
44+
- name: Build and push Container Image to ECR
45+
id: build-image
46+
uses: docker/build-push-action@v6
47+
with:
48+
push: true
49+
tags: ${{ steps.image-vars.outputs.image-uri }}
50+
provenance: false
51+
platforms: "linux/amd64"
3252

3353
- name: Fetch latest task definition
3454
id: get-task-def
3555
run: |
36-
aws ecs describe-task-definition --task-definition $TASK_DEFINITION \
37-
--region $AWS_REGION > taskdef.json
56+
aws ecs describe-task-definition --task-definition "$TASK_DEFINITION" \
57+
--region "$AWS_REGION" > taskdef.json
3858
3959
- name: Fill in the new image ID in the Amazon ECS task definition
4060
id: updated-task-def
@@ -54,30 +74,30 @@ jobs:
5474
id: run-task
5575
run: |
5676
TASK_ARN=$(aws ecs run-task \
57-
--cluster $CLUSTER_NAME \
77+
--cluster "$CLUSTER_NAME" \
5878
--launch-type FARGATE \
5979
--network-configuration "awsvpcConfiguration={subnets=[$SUBNET_ID],securityGroups=[$SECURITY_GROUP_ID],assignPublicIp=DISABLED}" \
60-
--task-definition ${{ steps.register-task-def.outputs.task_def_arn }} \
61-
--region $AWS_REGION \
80+
--task-definition "${{ steps.register-task-def.outputs.task_def_arn }}" \
81+
--region "$AWS_REGION" \
6282
--started-by github-actions \
6383
--query 'tasks[0].taskArn' \
6484
--output text)
6585
66-
echo "task_arn=$TASK_ARN" >> $GITHUB_OUTPUT
86+
echo "task_arn=$TASK_ARN" >> "$GITHUB_OUTPUT"
6787
6888
- name: Wait for task to complete
6989
run: |
7090
aws ecs wait tasks-stopped \
71-
--cluster $CLUSTER_NAME \
72-
--tasks ${{ steps.run-task.outputs.task_arn }} \
73-
--region $AWS_REGION
91+
--cluster "$CLUSTER_NAME" \
92+
--tasks "${{ steps.run-task.outputs.task_arn }}" \
93+
--region "$AWS_REGION"
7494
7595
- name: Check task exit code
7696
run: |
7797
EXIT_CODE=$(aws ecs describe-tasks \
78-
--cluster $CLUSTER_NAME \
98+
--cluster "$CLUSTER_NAME" \
7999
--tasks ${{ steps.run-task.outputs.task_arn }} \
80-
--region $AWS_REGION \
100+
--region "$AWS_REGION" \
81101
--query "tasks[0].containers[?name=='${CONTAINER_NAME}'].exitCode" \
82102
--output text)
83103
@@ -87,4 +107,3 @@ jobs:
87107
echo "Migration task failed with exit code $EXIT_CODE"
88108
exit 1
89109
fi
90-

terraform/ecr.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "aws_ecr_repository" "migration" {
2+
name = "db-migration"
3+
image_tag_mutability = "MUTABLE"
4+
force_delete = true
5+
6+
lifecycle {
7+
prevent_destroy = false
8+
}
9+
}

terraform/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ output "migration_task_definition_arn" {
33
description = "ARN of the migration ECS Task"
44
}
55

6+
output "ecr_repository_url" {
7+
description = "URL of the ECR repository"
8+
value = aws_ecr_repository.migration.repository_url
9+
}

0 commit comments

Comments
 (0)