-
Notifications
You must be signed in to change notification settings - Fork 9
209 lines (180 loc) · 8.86 KB
/
publish-and-deploy.yml
File metadata and controls
209 lines (180 loc) · 8.86 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
name: Publish & Deploy
run-name: Publish & Deploy
on:
workflow_call:
inputs:
target_env:
required: true
type: string
description: 'The deployment environment e.g. production or development'
jobs:
publish:
name: Publish Docker Images
runs-on: ubuntu-latest
environment: ${{ inputs.target_env }}
outputs:
# Exposes the TARGET_TAG output from the 'vars' step.
target_tag: ${{ steps.vars.outputs.TARGET_TAG }}
steps:
- name: Checkout code
# Checks out the repository code.
uses: actions/checkout@v4
- name: Set up Docker Buildx
# Prepares Docker Buildx for building multi-platform images.
uses: docker/setup-buildx-action@v3
- name: Log in to GHCR
# Logs into GitHub Container Registry using provided credentials.
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ secrets.MACHINE_USER }}
password: ${{ secrets.MACHINE_PAT }}
- name: Determine deployment variables
id: vars
# Sets Docker image tags for production or development based on branch name.
run: |
BRANCH_NAME=${GITHUB_REF##*/}
if [ "$BRANCH_NAME" = "main" ]; then
echo "TARGET_TAG=prod-latest" >> $GITHUB_OUTPUT
echo "BACKUP_TAG=prod-previous" >> $GITHUB_OUTPUT
elif [ "$BRANCH_NAME" = "development" ]; then
echo "TARGET_TAG=dev-latest" >> $GITHUB_OUTPUT
echo "BACKUP_TAG=dev-previous" >> $GITHUB_OUTPUT
else
echo "Unsupported branch: $BRANCH_NAME"
exit 1
fi
- name: Backup existing API image
# Pulls the current image using the target tag and backup it with a previous tag if it exists.
run: |
OWNER="${{ github.repository_owner }}"
BASE="${{ github.event.repository.name }}-api"
IMAGE="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.TARGET_TAG }}"
BACKUP="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.BACKUP_TAG }}"
echo "Backing up API image if it exists: $IMAGE → $BACKUP"
if docker pull "$IMAGE"; then
docker tag "$IMAGE" "$BACKUP"
docker push "$BACKUP"
else
echo "No API image to back up."
fi
- name: Backup existing Jobs image
# Pulls the current image using the target tag and backup it with a previous tag if it exists.
run: |
OWNER="${{ github.repository_owner }}"
BASE="${{ github.event.repository.name }}-jobs"
IMAGE="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.TARGET_TAG }}"
BACKUP="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.BACKUP_TAG }}"
echo "Backing up Jobs image if it exists: $IMAGE → $BACKUP"
if docker pull "$IMAGE"; then
docker tag "$IMAGE" "$BACKUP"
docker push "$BACKUP"
else
echo "No Jobs image to back up."
fi
- name: Cleanup Old API Image Digests
# Cleans up old api images
uses: actions/delete-package-versions@v5
continue-on-error: true
with:
package-name: ${{ github.event.repository.name }}-api
package-type: container
owner: ${{ github.repository_owner }}
ignore-versions: "^(prod-latest|prod-previous|dev-latest|dev-previous)$" # keeps important tags (latest and previous)
token: ${{ secrets.MACHINE_PAT }}
- name: Cleanup Old Jobs Image Digests
# Cleans up old jobs images
uses: actions/delete-package-versions@v5
continue-on-error: true
with:
package-name: ${{ github.event.repository.name }}-jobs
package-type: container
owner: ${{ github.repository_owner }}
ignore-versions: "^(prod-latest|prod-previous|dev-latest|dev-previous)$" # keeps important tags (latest and previous)
token: ${{ secrets.MACHINE_PAT }}
- name: Build & push API image
# Builds a new Docker image with the target tag and pushes it to GHCR.
run: |
OWNER=${{ github.repository_owner }}
BASE=${{ github.event.repository.name }}-api
TAG=${{ steps.vars.outputs.TARGET_TAG }}
IMAGE=ghcr.io/$OWNER/$BASE:$TAG
echo "Building image with tag $IMAGE"
docker build \
--file docker/api/Dockerfile \
--tag $IMAGE \
.
docker push $IMAGE
- name: Build & push Jobs image
# Builds a new Docker image with the target tag and pushes it to GHCR.
run: |
OWNER=${{ github.repository_owner }}
BASE=${{ github.event.repository.name }}-jobs
TAG=${{ steps.vars.outputs.TARGET_TAG }}
IMAGE=ghcr.io/$OWNER/$BASE:$TAG
echo "Building image with tag $IMAGE"
docker build \
--file docker/jobs/Dockerfile \
--tag $IMAGE \
.
docker push $IMAGE
deploy:
needs: publish
name: Deploy
runs-on: ubuntu-latest
environment: ${{ inputs.target_env }}
steps:
- name: Checkout code
# Checks out the repository code.
uses: actions/checkout@v4
- name: Set up SSH key
# Sets up the SSH key for the server.
run: |
# Create the .ssh directory if it doesn't exist.
mkdir -p ~/.ssh
# Write the SSH private key to file.
echo "${{ secrets.DEPLOYMENT_SSH_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
# Add the server to known_hosts to avoid authenticity prompts.
ssh-keyscan -H ${{ secrets.DEPLOYMENT_SERVER }} >> ~/.ssh/known_hosts
- name: Upload deployment files to server
# Creates project directory and uploads required files to server
run: |
# create env file
echo "${{ secrets.APPLICATION_ENV_FILE }}" > ./config/env/.env
# copy all config files over
ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} \
"mkdir -p /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/config /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/docker"
scp -r -o StrictHostKeyChecking=no ./config/* \
${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/config/
# copy compose files
scp -o StrictHostKeyChecking=no docker/docker-compose.yml ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/docker/docker-compose.yml
if [ "${{ needs.publish.outputs.target_tag }}" = "prod-latest" ]; then
scp -o StrictHostKeyChecking=no docker/docker-compose.prod.yml ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/docker/docker-compose.override.yml
scp -o StrictHostKeyChecking=no otel-config.yaml ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/otel-config.yaml
else
scp -o StrictHostKeyChecking=no docker/docker-compose.dev.yml ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/docker/docker-compose.override.yml
fi
# copy deploy script
scp -o StrictHostKeyChecking=no scripts/deploy.sh ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/deploy.sh
ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} "\
chmod +x /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/deploy.sh"
- name: Deploy to VPS
# Deploys to VPS.
run: |
OWNER="${{ vars.GHCR_OWNER }}"
APPLICATION_API_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}-api:${{ needs.publish.outputs.target_tag }}"
APPLICATION_JOBS_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}-jobs:${{ needs.publish.outputs.target_tag }}"
echo "Deploying to VPS..."
ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} "\
# exports general variables
export PROJECT_NAME='${{ vars.PROJECT_NAME }}' && \
export GHCR_USER='${{ secrets.MACHINE_USER }}' && \
export GHCR_PAT='${{ secrets.MACHINE_PAT }}' && \
export APPLICATION_API_IMAGE='$APPLICATION_API_IMAGE' && \
export APPLICATION_JOBS_IMAGE='$APPLICATION_JOBS_IMAGE' && \
# applies only to production for logging
export HONEYCOMB_API_KEY='${{ secrets.HONEYCOMB_API_KEY }}' && \
export HONEYCOMB_DATASET='${{ secrets.HONEYCOMB_DATASET }}' && \
# runs deploy script
/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/deploy.sh"