Skip to content

Commit b28e98a

Browse files
committed
feat: init repository
- Add compose manifest - Minimal readme - Draft migration workflow Signed-off-by: Theo Bob Massard <tbobm@protonmail.com>
0 parents  commit b28e98a

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

.github/workflows/migration.yaml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
name: Run DB Migrations
3+
4+
on:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
migrate-database:
11+
name: Run ECS One-Off Task
12+
runs-on: ubuntu-latest
13+
env:
14+
AWS_REGION: eu-west-1
15+
CLUSTER_NAME: sample-cluster
16+
TASK_DEFINITION: db-migration-job
17+
SUBNET_ID: subnet-abc123
18+
SECURITY_GROUP_ID: sg-abc123
19+
CONTAINER_NAME: migration
20+
IMAGE_URI: db-iac:latest
21+
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
26+
- name: Configure AWS credentials
27+
uses: aws-actions/configure-aws-credentials@v4
28+
with:
29+
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsDeploymentRole
30+
aws-region: ${{ env.AWS_REGION }}
31+
32+
- name: Fetch latest task definition
33+
id: get-task-def
34+
run: |
35+
aws ecs describe-task-definition --task-definition $TASK_DEFINITION \
36+
--region $AWS_REGION > taskdef.json
37+
38+
- name: Fill in the new image ID in the Amazon ECS task definition
39+
id: updated-task-def
40+
uses: aws-actions/amazon-ecs-render-task-definition@v1
41+
with:
42+
task-definition: new-task-def.json
43+
container-name: $CONTAINER_NAME
44+
image: $IMAGE_URI
45+
46+
- name: Deploy Amazon ECS task definition
47+
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
48+
id: register-task-def
49+
with:
50+
task-definition: ${{ steps.updated-task-def.outputs.task-definition }}
51+
52+
- name: Run ECS task
53+
id: run-task
54+
run: |
55+
TASK_ARN=$(aws ecs run-task \
56+
--cluster $CLUSTER_NAME \
57+
--launch-type FARGATE \
58+
--network-configuration "awsvpcConfiguration={subnets=[$SUBNET_ID],securityGroups=[$SECURITY_GROUP_ID],assignPublicIp=DISABLED}" \
59+
--task-definition ${{ steps.register-task-def.outputs.task_def_arn }} \
60+
--region $AWS_REGION \
61+
--started-by github-actions \
62+
--query 'tasks[0].taskArn' \
63+
--output text)
64+
65+
echo "task_arn=$TASK_ARN" >> $GITHUB_OUTPUT
66+
67+
- name: Wait for task to complete
68+
run: |
69+
aws ecs wait tasks-stopped \
70+
--cluster $CLUSTER_NAME \
71+
--tasks ${{ steps.run-task.outputs.task_arn }} \
72+
--region $AWS_REGION
73+
74+
- name: Check task exit code
75+
run: |
76+
EXIT_CODE=$(aws ecs describe-tasks \
77+
--cluster $CLUSTER_NAME \
78+
--tasks ${{ steps.run-task.outputs.task_arn }} \
79+
--region $AWS_REGION \
80+
--query "tasks[0].containers[?name=='${CONTAINER_NAME}'].exitCode" \
81+
--output text)
82+
83+
echo "Task exited with code $EXIT_CODE"
84+
85+
if [ "$EXIT_CODE" != "0" ]; then
86+
echo "Migration task failed with exit code $EXIT_CODE"
87+
exit 1
88+
fi
89+

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Boostrapping databases for local and prod
2+
3+
## Content
4+
5+
- infra: infrastructure as code for RDS / ECS migration task
6+
- db-as-code: setup for managing databases using atlas
7+
- cicd: example automation and tooling for handling databases with github action
8+
9+
## Setup
10+
11+
```console
12+
$ docker compose up -d --wait --quiet-pull db
13+
$ docker compose run -it atlas schema inspect -u "postgres://user:pass@db:5432/local_db?sslmode=disable"
14+
```

docker-compose.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
services:
3+
db:
4+
image: postgres:15
5+
restart: unless-stopped
6+
environment:
7+
POSTGRES_USER: user
8+
POSTGRES_PASSWORD: pass
9+
POSTGRES_DB: local_db
10+
ports:
11+
- "5432:5432"
12+
volumes:
13+
- pgdata:/var/lib/postgresql/data
14+
healthcheck:
15+
test: ["CMD", "pg_isready", "-U", "user"]
16+
interval: 5s
17+
timeout: 3s
18+
retries: 5
19+
20+
atlas:
21+
image: arigaio/atlas:latest
22+
depends_on:
23+
db:
24+
condition: service_healthy
25+
volumes:
26+
- .:/workspace
27+
working_dir: /workspace
28+
29+
volumes:
30+
pgdata:

0 commit comments

Comments
 (0)