1- name : Lint, Build & Publish
2- run-name : Lint, Build & Publish
3-
4- permissions :
5- contents : read
6- actions : read
1+ name : Publish & Deploy
2+ run-name : Publish & Deploy
73
84on :
95 workflow_call :
106 inputs :
11- node-version :
12- description : " Node.js version to use"
7+ target_tag :
138 required : true
149 type : string
10+ description : ' The deployment target tag, e.g. prod-latest or dev-latest'
1511 target_env :
16- description : ' The deployment environment e.g. production or development'
1712 required : true
1813 type : string
19- skip_deploy :
20- description : ' Skip the deploy step (true/false)'
21- required : true
22- type : boolean
14+ description : ' The deployment environment e.g. production or development'
2315
2416jobs :
25- lint :
26- name : Run Lint
27- runs-on : ubuntu-latest
28- environment : ${{ inputs.target_env }}
29-
30- steps :
31- - name : Checkout code
32- # Checks out the repository code.
33- uses : actions/checkout@v4
34-
35- - name : Setup Node.js (Latest)
36- # Sets up NodeJS environment
37- uses : actions/setup-node@v4
38- with :
39- node-version : ${{ inputs.node-version }}
40-
41- - name : Install Dependencies
42- # Installs dependencies
43- run : npm ci
44-
45- - name : Run Linter
46- # Runs lint checks
47- run : npm run lint
48-
49- build :
50- needs : lint
17+ publish :
5118 name : Build Docker Images
5219 runs-on : ubuntu-latest
5320 environment : ${{ inputs.target_env }}
@@ -190,12 +157,73 @@ jobs:
190157
191158 docker push $IMAGE
192159
193- trigger- deploy :
194- needs : build
160+ deploy :
161+ needs : publish
195162 name : Deploy
196- if : ${{ inputs.skip_deploy == false }}
197- uses : ./.github/workflows/deploy.yml
198- secrets : inherit
199- with :
200- target_env : ${{ inputs.target_env }}
201- target_tag : ${{ needs.build.outputs.target_tag }}
163+ runs-on : ubuntu-latest
164+ environment : ${{ inputs.target_env }}
165+ steps :
166+ - name : Checkout code
167+ # Checks out the repository code.
168+ uses : actions/checkout@v4
169+
170+ - name : Set up SSH key
171+ # Sets up the SSH key for the server.
172+ run : |
173+ # Create the .ssh directory if it doesn't exist.
174+ mkdir -p ~/.ssh
175+ # Write the SSH private key to file.
176+ echo "${{ secrets.DEPLOYMENT_SSH_KEY }}" > ~/.ssh/id_rsa
177+ chmod 600 ~/.ssh/id_rsa
178+ # Add the server to known_hosts to avoid authenticity prompts.
179+ ssh-keyscan -H ${{ secrets.DEPLOYMENT_SERVER }} >> ~/.ssh/known_hosts
180+
181+ - name : Upload deployment files to server
182+ # Creates project directory and uploads required files to server
183+ run : |
184+ # create env file
185+ echo "${{ secrets.APPLICATION_ENV_FILE }}" > ./config/env/.env
186+
187+ # copy all config files over
188+ ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} \
189+ "mkdir -p /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/config /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/docker"
190+
191+ scp -r -o StrictHostKeyChecking=no ./config/* \
192+ ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/config/
193+
194+ # copy compose files
195+ 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
196+ if [ "${{ inputs.target_tag }}" = "prod-latest" ]; then
197+ 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
198+ scp -o StrictHostKeyChecking=no otel-config.yaml ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/otel-config.yaml
199+ else
200+ 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
201+ fi
202+
203+ # copy deploy script
204+ scp -o StrictHostKeyChecking=no scripts/deploy.sh ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/opt/rcb-deployments/${{ vars.PROJECT_NAME }}/deploy.sh
205+ ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} "\
206+ chmod +x /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/deploy.sh"
207+
208+ - name : Deploy to VPS
209+ # Deploys to VPS.
210+ run : |
211+ OWNER="${{ vars.GHCR_OWNER }}"
212+ APPLICATION_API_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}-api:${{ inputs.target_tag }}"
213+ APPLICATION_JOBS_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}-jobs:${{ inputs.target_tag }}"
214+ echo "Deploying to VPS..."
215+ ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} "\
216+
217+ # exports general variables
218+ export PROJECT_NAME='${{ vars.PROJECT_NAME }}' && \
219+ export GHCR_USER='${{ secrets.MACHINE_USER }}' && \
220+ export GHCR_PAT='${{ secrets.MACHINE_PAT }}' && \
221+ export APPLICATION_API_IMAGE='$APPLICATION_API_IMAGE' && \
222+ export APPLICATION_JOBS_IMAGE='$APPLICATION_JOBS_IMAGE' && \
223+
224+ # applies only to production for logging
225+ export HONEYCOMB_API_KEY='${{ secrets.HONEYCOMB_API_KEY }}' && \
226+ export HONEYCOMB_DATASET='${{ secrets.HONEYCOMB_DATASET }}' && \
227+
228+ # runs deploy script
229+ /opt/rcb-deployments/${{ vars.PROJECT_NAME }}/deploy.sh"
0 commit comments