Github Actions
December 15, 2021 / Updated: October 14, 2023
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 Deployon:push:branches: mainjobs:build-and-deploy:name: Build and deploy Gatsby siteruns-on: ubuntu-lateststeps:- name: Checkout Codeuses: actions/checkout@v2- name: Set variablesrun: |VER=$(cat .node-version)echo "NODE_VERSION=$VER" >> $GITHUB_ENV- name: Install Node.jsuses: actions/setup-node@v1with:node-version: ${{ env.NODE_VERSION }}- name: Install Project Dependenciesrun: npm ci- name: Buildrun: npx gatsby build- name: Verify buildrun: ls -la public- name: Modify .htaccessrun: sed -i -e 's/Options -MultiViews//' public/.htaccess- name: Setup SSHrun: |mkdir -p ~/.ssh/echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_keysudo chmod 600 ~/.ssh/deploy_keyssh-keyscan -p 10922 -H "${{ secrets.HOST }}" > ~/.ssh/known_hosts- name: Rsync uploadrun: 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!