-
Notifications
You must be signed in to change notification settings - Fork 21
159 lines (142 loc) · 5.65 KB
/
container_builds.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
name: Container ECR build + deploy
on:
push:
branches:
- main
- demo
permissions:
id-token: write
contents: read
jobs:
setup-env:
if: github.repository == 'UnlockedLabs/UnlockEdv2' || github.repository == 'PThorpe92/UnlockEdv2'
runs-on: ubuntu-latest
outputs:
changes: ${{ steps.check-changes.outputs.changes }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 3
- id: check-changes
run: |
paths=("frontend/" "backend/" "provider-middleware/" "backend/tasks")
changes=""
for path in "${paths[@]}"; do
count=$(git diff --name-only HEAD~1 | grep "^${path}" | wc -l)
changes+="${path}:${count},"
done
changes="${changes%,}"
echo "changes=${changes}" >> $GITHUB_OUTPUT
- name: Debug changes
run: echo "${{ steps.check-changes.outputs.changes }}"
build-and-push:
if: github.repository == 'UnlockedLabs/UnlockEdv2' || github.repository == 'PThorpe92/UnlockEdv2'
needs: setup-env
runs-on: ubuntu-latest
outputs:
deployments: ${{ steps.build-images.outputs.deployments }}
env:
CHANGES: ${{ needs.setup-env.outputs.changes }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
aws-region: us-west-2
mask-aws-account-id: true
- name: Log in to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- id: build-images
run: |
echo "CHANGES: $CHANGES"
deployments=()
IFS=',' read -ra entries <<< "$CHANGES"
for entry in "${entries[@]}"; do
if [ -z "$entry" ]; then
continue
fi
path=$(echo "$entry" | cut -d':' -f1)
count=$(echo "$entry" | cut -d':' -f2)
if [[ $count -ne 0 ]]; then
case $path in
"frontend/")
echo "Building frontend image"
docker buildx build --platform linux/amd64 -t=${{ steps.login-ecr.outputs.registry }}/frontend:latest --push frontend/.
deployments+=("frontend")
;;
"backend/")
echo "Building backend image"
docker buildx build --platform linux/amd64 -t=${{ steps.login-ecr.outputs.registry }}/unlockedv2:latest --push -f backend/Dockerfile .
deployments+=("server")
;;
"provider-middleware/")
echo "Building middleware image"
docker buildx build --platform linux/amd64 -t=${{ steps.login-ecr.outputs.registry }}/provider_middleware:latest --push -f provider-middleware/Dockerfile .
deployments+=("provider-service")
;;
"backend/tasks")
echo "Building scheduler image"
docker buildx build --platform linux/amd64 -t=${{ steps.login-ecr.outputs.registry }}/cron_tasks:latest --push -f backend/tasks/Dockerfile .
deployments+=("cron-tasks")
;;
esac
fi
done
echo "deployments=${deployments[*]}" >> $GITHUB_OUTPUT
restart-deployments:
if: github.repository == 'UnlockedLabs/UnlockEdv2' || github.repository == 'PThorpe92/UnlockEdv2'
needs: build-and-push
runs-on: ubuntu-latest
env:
SECURITY_GROUP_ID: ${{ secrets.SECURITY_GROUP_ID }}
BASTION_HOST: ${{ secrets.BASTION_HOST }}
SSH_KEY: ${{ secrets.SSH_KEY }}
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
aws-region: us-west-2
mask-aws-account-id: true
- name: Get Runner Public IP
id: get_runner_ip
run: |
RUNNER_IP=$(curl -s https://checkip.amazonaws.com)
echo "Runner IP: $RUNNER_IP"
echo "RUNNER_IP=$RUNNER_IP" >> $GITHUB_ENV
- name: Add Runner IP to Security Group
run: |
echo "Adding runner IP $RUNNER_IP to security group $SECURITY_GROUP_ID"
aws ec2 authorize-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol tcp --port 22 --cidr ${RUNNER_IP}/32
- name: Restart Deployments
env:
RUNNER_IP: ${{ env.RUNNER_IP }}
run: |
deployments="${{ needs.build-and-push.outputs.deployments }}"
if [[ -z "$deployments" ]]; then
echo "No deployments need restarting."
exit 0
fi
if [[ "${GITHUB_REF}" == "refs/heads/demo" ]]; then
CONTEXT="demo"
elif [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
CONTEXT="staging"
else
echo "Unknown branch: ${GITHUB_REF}. No deployments restarted."
exit 1
fi
mkdir -p ~/.ssh && echo "$SSH_KEY" | base64 -d > ~/.ssh/id_rsa && chmod 400 ~/.ssh/id_rsa
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no "$BASTION_HOST" "bash -s" <<EOF
rollout.sh $CONTEXT $deployments
EOF
- name: Remove Runner IP from Security Group
run: |
shred -u ~/.ssh/id_rsa || rm -f ~/.ssh/id_rsa
echo "Removing runner IP $RUNNER_IP from security group"
aws ec2 revoke-security-group-ingress --group-id $SECURITY_GROUP_ID --protocol tcp --port 22 --cidr ${RUNNER_IP}/32