-
Notifications
You must be signed in to change notification settings - Fork 35
103 lines (91 loc) · 4.33 KB
/
ci-cd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
name: CI/CD Pipeline
on:
push:
branches:
- main
paths:
- 'apps/backend/**'
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
run: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }}
- name: Check latest image version in ECR
id: check_version
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }}
run: |
VERSION=$(cat apps/backend/package.json | jq -r '.version')
LATEST_ECR_VERSION=$(aws ecr describe-images --repository-name $ECR_REPOSITORY_BACKEND_NAME --region $AWS_REGION --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' --output text)
if [ "$VERSION" == "$LATEST_ECR_VERSION" ]; then
echo "::set-output name=should_deploy::false"
exit 0
else
echo "::set-output name=should_deploy::true"
fi
- name: Build Docker image
if: steps.check_version.outputs.should_deploy == 'true'
env:
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }}
run: |
docker build -f apps/backend/Dockerfile --no-cache --progress=plain -t $ECR_REPOSITORY_BACKEND_NAME .
- name: Tag Docker image
if: steps.check_version.outputs.should_deploy == 'true'
env:
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }}
run: |
VERSION=$(cat apps/backend/package.json | jq -r '.version')
docker tag $ECR_REPOSITORY_BACKEND_NAME:latest $ECR_REGISTRY/$ECR_REPOSITORY_BACKEND_NAME:$VERSION
- name: Push Docker image to Amazon ECR
if: steps.check_version.outputs.should_deploy == 'true'
env:
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }}
run: |
VERSION=$(cat apps/backend/package.json | jq -r '.version')
docker push $ECR_REGISTRY/$ECR_REPOSITORY_BACKEND_NAME:$VERSION
- name: Deploy to EC2
if: steps.check_version.outputs.should_deploy == 'true'
env:
AWS_SSH_KEY: ${{ secrets.AWS_SSH_KEY }}
AWS_EC2_HOST: ${{ secrets.AWS_EC2_HOST }}
AWS_EC2_USER_NAME: ${{ secrets.AWS_EC2_USER_NAME }}
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ECR_REPOSITORY_BACKEND_NAME: ${{ secrets.ECR_REPOSITORY_BACKEND_NAME }}
run: |
echo "$AWS_SSH_KEY" > private_key.pem
chmod 600 private_key.pem
ssh -o StrictHostKeyChecking=no -i private_key.pem $AWS_EC2_USER_NAME@$AWS_EC2_HOST '
export AWS_REGION='"$AWS_REGION"'
export ECR_REGISTRY='"$ECR_REGISTRY"'
export ECR_REPOSITORY_BACKEND_NAME='"$ECR_REPOSITORY_BACKEND_NAME"'
aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $ECR_REGISTRY
LATEST_IMAGE_VERSION=$(aws ecr describe-images --repository-name $ECR_REPOSITORY_BACKEND_NAME --region $AWS_REGION --query "sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]" --output text)
echo "Latest image version: $LATEST_IMAGE_VERSION"
cd nginx/
sed -i "s|studio-backend:[^[:space:]]*|studio-backend:$LATEST_IMAGE_VERSION|g" docker-compose.yml
if [ $? -eq 0 ]; then
echo "Image version updated successfully in docker-compose.yml."
else
echo "Failed to update image version in docker-compose.yml."
exit 1
fi
sudo docker compose down
sudo docker compose pull
sudo docker compose up -d
'