pimentel.page

Github Actions

December 15, 2021 / Updated: October 14, 2023

Cat in a bay, walking on a banister

Photo Credit: Timothy Meinberg

UPDATE:

The GitHub Workflow has been modified to use rsync instead of SCP. I have modified the yaml in this post to reflect those changes

As you probably already know, the source code of this blog is posted on GitHub.

In order to improve my development workflow I searched for a way to deploy new blog contents into the hosting provider using GitHub actions, and I found this blog post with some workflow code I could tune for my use case:

## Based on https://www.andrewvillazon.com/automatically-deploying-with-github-actions/
name: Build and Deploy
on:
push:
branches: main
jobs:
build-and-deploy:
name: Build and deploy Gatsby site
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Set variables
run: |
VER=$(cat .node-version)
echo "NODE_VERSION=$VER" >> $GITHUB_ENV
- name: Install Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install Project Dependencies
run: npm ci
- name: Build
run: npx gatsby build
- name: Verify build
run: ls -la public
- name: Modify .htaccess
run: sed -i -e 's/Options -MultiViews//' public/.htaccess
- name: Setup SSH
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key
sudo chmod 600 ~/.ssh/deploy_key
ssh-keyscan -p 10922 -H "${{ secrets.HOST }}" > ~/.ssh/known_hosts
- name: Rsync upload
run: rsync -av -e "ssh -i ~/.ssh/deploy_key -l ${{ secrets.DEPLOY_USER }} -p 10922" public/ ${{ secrets.DEPLOY_USER }}@${{ secrets.HOST }}:html/

This workflow builds the project using the node version of the file .node-version of the repository and gatsby build and then uses rsync to upload the public directory contents to the hosting provider document root. Pretty neat!

Tags: bloggithub-actions
Previous: Kibana Enhanced TablesNext: Static Site Generators