Yesterday I moved the deployment for this site from a local script to GitHub actions. Overall it was a really pleasant experience because GitHub made it really easy to run scripts inside e.g. a Node.js environment, which I need to build the site.
One thing that was a bit tricky for me, was figuring out how to git push
to my Dokku server from GitHub Actions. I needed to authenticate GitHub Actions for SSH access to my Dokku server.
I ended up generating a new SSH key for GitHub Actions and saving the private key to my repository's secrets (find them in the repo settings) as DOKKU_SSH_KEY
.
Then I added a few lines to my main.yml
file:
YAML
name: CIon:push:branches: [master]jobs:build:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v2- name: Setup Node.js environmentuses: actions/setup-node@v1.4.2with:node-version: 12.x- name: Add SSH keyenv:SSH_AUTH_SOCK: /tmp/ssh_agent.sockrun: |mkdir -p /home/runner/.ssh# Replace example.com with the hostname of the machine# you're SSH-ing intossh-keyscan example.com >> /home/runner/.ssh/known_hosts# DOKKU_SSH_KEY is the name of the repository secretecho "${{ secrets.DOKKU_SSH_KEY }}" > /home/runner/.ssh/github_actionschmod 600 /home/runner/.ssh/github_actionsssh-agent -a $SSH_AUTH_SOCK > /dev/nullssh-add /home/runner/.ssh/github_actions- name: Install dependenciesrun: yarn- name: Build and deployenv:SSH_AUTH_SOCK: /tmp/ssh_agent.sockrun: |git config --global user.email "actions@github.com"git config --global user.name "GitHub actions"yarn build --env productiongit remote add dokku dokku@example.com:app-namegit add dist --forcegit commit -m "Deploy"git push dokku master -f
And that's all it took to get Github Actions authenticated with my Dokku server. I hope this was helpful to you, thanks for reading!