Skip to content

Commit 968e7c6

Browse files
committed
Refactor GitHub Actions workflow for building and deploying Docker images
1 parent 858360e commit 968e7c6

1 file changed

Lines changed: 94 additions & 40 deletions

File tree

.github/workflows/deploy.yaml

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,117 @@
1-
name: Deploy to EC2
1+
name: Build and Deploy
22

33
on:
44
push:
55
branches:
6-
- dev
76
- main
7+
release:
8+
types:
9+
- published
10+
11+
permissions:
12+
contents: read
13+
packages: write
14+
id-token: write
815

916
jobs:
10-
deploy:
17+
build-and-push:
1118
runs-on: ubuntu-latest
1219

1320
steps:
1421
- name: Checkout repository
1522
uses: actions/checkout@v4
1623

17-
- name: Set environment variables
24+
- name: Set up QEMU
25+
uses: docker/setup-qemu-action@v3
26+
27+
- name: Set up Docker Buildx
28+
uses: docker/setup-buildx-action@v3
29+
30+
- name: Log in to GHCR
31+
uses: docker/login-action@v3
32+
with:
33+
registry: ghcr.io
34+
username: ${{ github.actor }}
35+
password: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Compute image tags
38+
id: image
39+
env:
40+
EVENT_NAME: ${{ github.event_name }}
41+
RELEASE_TAG: ${{ github.event.release.tag_name }}
42+
RELEASE_PRERELEASE: ${{ github.event.release.prerelease }}
43+
REPOSITORY: ${{ github.repository }}
44+
SHA: ${{ github.sha }}
1845
run: |
19-
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
20-
echo "DEPLOY_DIR=/data/MusicCPRProd" >> $GITHUB_ENV
21-
echo "IMAGE_NAME=backend:prod" >> $GITHUB_ENV
22-
echo "CONTAINER_NAME=backend-prod" >> $GITHUB_ENV
23-
echo "HOST_PORT=8001" >> $GITHUB_ENV
46+
set -euo pipefail
47+
image="ghcr.io/${REPOSITORY,,}"
48+
short_sha="${SHA::12}"
49+
50+
tags=""
51+
52+
if [[ "$EVENT_NAME" == "push" ]]; then
53+
tags="${image}:nightly"
54+
tags+=$'\n'"${image}:sha-${short_sha}"
55+
elif [[ "$EVENT_NAME" == "release" ]]; then
56+
tags="${image}:${RELEASE_TAG}"
57+
if [[ "$RELEASE_PRERELEASE" != "true" ]]; then
58+
tags+=$'\n'"${image}:latest"
59+
fi
2460
else
25-
echo "DEPLOY_DIR=/data/MusicCPRDev" >> $GITHUB_ENV
26-
echo "IMAGE_NAME=backend:dev" >> $GITHUB_ENV
27-
echo "CONTAINER_NAME=backend-dev" >> $GITHUB_ENV
28-
echo "HOST_PORT=8000" >> $GITHUB_ENV
61+
echo "Unsupported event: $EVENT_NAME" >&2
62+
exit 1
2963
fi
30-
- name: Set up SSH
31-
run: |
32-
mkdir -p ~/.ssh
33-
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
34-
chmod 600 ~/.ssh/id_rsa
35-
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
3664
37-
- name: Deploy to EC2
38-
run: |
39-
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
40-
set -e
41-
echo "Deploying to ${{ env.DEPLOY_DIR }}"
65+
{
66+
echo "image=$image"
67+
echo "tags<<EOF"
68+
echo "$tags"
69+
echo "EOF"
70+
} >> "$GITHUB_OUTPUT"
4271
43-
cd ${{ env.DEPLOY_DIR }}
72+
- name: Build and push image
73+
uses: docker/build-push-action@v6
74+
with:
75+
context: .
76+
file: ./Dockerfile.aws
77+
push: true
78+
platforms: ${{ vars.DOCKER_PLATFORMS != '' && vars.DOCKER_PLATFORMS || 'linux/amd64,linux/arm64' }}
79+
tags: ${{ steps.image.outputs.tags }}
4480

45-
echo "Pulling latest changes..."
46-
git pull origin ${{ github.ref_name }}
81+
deploy-dev:
82+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
83+
needs: build-and-push
84+
runs-on: ubuntu-latest
4785

48-
echo "Stopping and removing old container"
49-
docker stop ${{ env.CONTAINER_NAME }} || true
50-
docker rm ${{ env.CONTAINER_NAME }} || true
86+
steps:
87+
- name: Configure AWS credentials
88+
uses: aws-actions/configure-aws-credentials@v4
89+
with:
90+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
91+
aws-region: ${{ vars.AWS_REGION }}
5192

52-
echo "Removing old image"
53-
docker rmi ${{ env.IMAGE_NAME }} || true
93+
- name: Trigger ECS deployment (dev)
94+
run: |
95+
aws ecs update-service \
96+
--cluster "${{ vars.ECS_DEV_CLUSTER }}" \
97+
--service "${{ vars.ECS_DEV_SERVICE }}" \
98+
--force-new-deployment
5499
55-
echo "Building new image..."
56-
docker build -t ${{ env.IMAGE_NAME }} .
100+
deploy-prod:
101+
if: github.event_name == 'release' && github.event.release.prerelease == false
102+
needs: build-and-push
103+
runs-on: ubuntu-latest
57104

58-
echo "Starting new container..."
59-
docker run -d --name ${{ env.CONTAINER_NAME }} \
60-
-p ${{ env.HOST_PORT }}:8000 \
61-
-v ./.env:/app/.env \
62-
--restart unless-stopped ${{ env.IMAGE_NAME }}
63-
EOF
105+
steps:
106+
- name: Configure AWS credentials
107+
uses: aws-actions/configure-aws-credentials@v4
108+
with:
109+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
110+
aws-region: ${{ vars.AWS_REGION }}
111+
112+
- name: Trigger ECS deployment (prod)
113+
run: |
114+
aws ecs update-service \
115+
--cluster "${{ vars.ECS_PROD_CLUSTER }}" \
116+
--service "${{ vars.ECS_PROD_SERVICE }}" \
117+
--force-new-deployment

0 commit comments

Comments
 (0)