Eric J Ma's Website

Deploy to Dokku from GitHub Actions

written by Eric J. Ma on 2023-08-27 | tags: til github actions cicd continuous integration continuous delivery dokku digitalocean deployment coding devops cost efficiency dokku server git dokku deployment


I host a Dokku server on DigitalOcean. (If you're interested in signing up for a DigitalOcean account, I have a referral link here.) I use Dokku on DigitalOcean because of cost: for a given instance size's price, I can host about 5-10x more apps on a given server than I could otherwise on competing services such as Heroku and Fly.io. That said, deploying to my Dokku server is often done manually. I recently figured out how to get out of doing that manually, by using GitHub Actions to deploy my apps to DigitalOcean on every merge to the main branch.

The GitHub Actions Workflow YAML

Dokku has an official GitHub Action for doing so. Under there, I found the most minimal and simple configuration that's needed:

name: Deploy to Dokku Server

on:
  push:
    branches:
      - main # configure this
  workflow_dispatch: # this allows a manual trigger of deploys as well

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Cloning repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Push to dokku
        uses: dokku/github-action@master
        with:
          branch: 'main' # the branch on the Dokku server to deploy to
          git_remote_url: ${{ secrets.DOKKU_GIT_REMOTE_URL}}
          ssh_private_key: ${{ secrets.DOKKU_SSH_PRIVATE_KEY }}

What's going on here?

Underneath the hood, the Dokku action is simply taking the repository and doing a git push to the Dokku server. Doing so requires the following information to be configured on GitHub Actions, as secrets (for security purposes).

Firstly, you need DOKKU_GIT_REMOTE_URL: which should be of the format ssh://dokku@<my-server-url-or-ip>:22/<my-app-name>. I mistakenly configured it as dokku@<my-server-url>:<my-app-name>, which is how git remotes are configured, but that yielded an error.

Secondly, you need DOKKU_SSH_PRIVATE_KEY: which should be the SSH private key that Dokku is configured to accept. For this, I would strongly suggest:

  1. Creating a new SSH key pair (following the excellent instructions on GitHub's docs, and then
  2. Adding the public key half of the SSH key pair to Dokku (following the instructions on the Dokku User Management page).

Both DOKKU_GIT_REMOTE_URL and DOKKU_SSH_PRIVATE_KEY need to be configured on GitHub Actions' secrets. This is done by navigating to the repository's Settings --> Secrets and Variables --> Actions, and then adding the appropriate secrets as described above.


Cite this blog post:
@article{
    ericmjl-2023-deploy-actions,
    author = {Eric J. Ma},
    title = {Deploy to Dokku from GitHub Actions},
    year = {2023},
    month = {08},
    day = {27},
    howpublished = {\url{https://ericmjl.github.io}},
    journal = {Eric J. Ma's Blog},
    url = {https://ericmjl.github.io/blog/2023/8/27/deploy-to-dokku-from-github-actions},
}
  

I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

Finally, I do free 30-minute GenAI strategy calls for teams that are looking to leverage GenAI for maximum impact. Consider booking a call on Calendly if you're interested!