Dokku + Github Actions: automatically deploy app with push to repository

I love Heroku Github integration because it can automatically deploy my Python or Node.js app after a commit or PR is merged to master/main branch. But Dokku doesn't have this integration. In this article, I'll show you how to integrate Github CI to deploy your app instantly after a commit. I'm using it for a production deployment of my pet projects.

I hope you know what Dokku is, why is it cool and how to create a Dokku app using CLI on your server.

How does it work?

The integration flow is simple:

  1. You push to main branch
  2. Github Actions server connects to your server via ssh
  3. Updates internal Dokku app code repo and triggers rebuild script

So you need to specify the proper Github Actions workflow and set up ssh private key to allow GH actions server to connect to your server where your app is deployed.

Dokku + Github actions workflow

Take a look at this snippet from my Django-Telegram-bot integration: it deploys a demo telegram bot to a production server. I'm using Dokku integration made by a Github user idoberko2.

name: CI

on:
  push:
    branches:
      - main

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
          fetch-depth: 0

    - id: deploy
      name: Deploy to dokku
      uses: idoberko2/dokku-deploy-github-action@v1
      with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
          dokku-host: 'YOUR SERVER IP OR URL'
          app-name: 'NAME OF YOUR DOKKU APP'
          git-push-flags: '--force'

The only thing you need to change is the dokku-host (IP address or DNS name of your server) and app-name (the name of your Dokku app you created using Dokku CLI).

Set private SSH key in Github repo secrets.

Go to your Github repo page, click Settings and Secrets section. Then "New repository secret". Create SSH_PRIVATE_KEY variable with the value of your ssh private key which is allowed to connect to your production or staging server. You can basically copy-paste the output from cat ~/.ssh/id_rsa here.

As result, you will have your secret variable like this:

💥 Boom, It's done

Now try to push something to your Main branch and see the magic.


I hope this small article is useful somehow. Follow me on Twitter for more Dokku guides for future CTOs.

django-telegram-bot/dokku.yml at main · ohld/django-telegram-bot
My sexy Django + python-telegram-bot + Celery + Redis + Postgres + Dokku + GitHub Actions template - django-telegram-bot/dokku.yml at main · ohld/django-telegram-bot
-