Github actions building and deploying
Github recently introduced actions that allow workflows to be automated. This CI/CD was really helpful on allowing me to push github updates and having it auto build and deploy on my Digital Ocean droplet.
The way I have the CI/CD process set up right now is:
1) On any push to main branch, github actions workflow main.yml runs
2) Github sets up Node 15 and installs the package dependencies in prod mode
3) It then runs yarn run build to build the Gatsby production files to be served
4) Once that is completed, now we need to push the built files onto the server hosting the files. To do that we first need to get the keys to ssh into the server.
- This is done by creating a key file from the github secrets, and giving it the correct permissions to be used.
5) We finally use remote sync to upload the built files onto the server (using the ssh credentials)
Unfortunately, code-input doesn't highlight YML files correctly.
1# This is a basic workflow to help you get started with Actions
2
3name: Update Website
4
5# Controls when the action will run.
6on:
7 # Triggers the workflow on push events but only for the main branch
8 push:
9 branches: [main]
10
11 # Allows you to run this workflow manually from the Actions tab
12 workflow_dispatch:
13
14# A workflow run is made up of one or more jobs that can run sequentially or in parallel
15jobs:
16 # This workflow contains a single job called "build"
17 build:
18 # The type of runner that the job will run on
19 runs-on: ubuntu-latest
20
21 # Steps represent a sequence of tasks that will be executed as part of the job
22 steps:
23 # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
24 - uses: actions/checkout@v2
25 # Set Node version
26 - name: Set Node.js
27 uses: actions/setup-node@master
28 with:
29 node-version: 15.x
30 # Install package dependencies
31 - name: Install dependencies
32 run: yarn install --prod --pure-lockfile
33 # Build the site
34 - name: Build
35 run: yarn run build
36 # Sync the public folder to the server
37 - name: Sync to Deploy
38 env:
39 dest: ${{ secrets.server_path }}
40 run: |
41 echo "${{secrets.deploy_key}}" > deploy_key
42 chmod 600 ./deploy_key
43 rsync -chav --delete \
44 -e 'ssh -i ./deploy_key -o StrictHostKeyChecking=no' \
45 --exclude /deploy_key \
46 --exclude /.git/ \
47 --exclude /.github/ \
48 --exclude /node_modules/ \
49 ./public/* ${{env.dest}}
50
Make sure to put this file in /.github/workflows/main.yml for it to auto run on github.