Jonathan Bennett

Deploy Kamal with Github Actions

Automatically deploying a Kamal application when you push to Github can be a simple 3 step process:

  1. Setup secrets
  2. Add a deploy workflow
  3. Profit

1. Setup Secrets

You are likely going to need a new personal access token for DockerHub and an SSH key for deployment.

Docker Hub

In you Docker Hub, click your profile picture in the top right corner, and navigate to the Account Settings screen, then the Personal access tokens screen. Make a new token, giving it a name for this project with read and wrote access. Take note of this new token.

SSH

In your terminal run ssh-keygen -t ed25519 -C "PROJECT_NAME-deploy". You will want to avoid adding a passphrase to the key.

The public key can be added to the server by running ssh-copy-id -i FILENAME.pub root@IP_ADDRESS.

Github

From your Github repository, navigate to the Settings > Secrets and variables > Actions screen.

In the Repository secrets section, add a New repository secret. Name one KAMAL_REGISTRY_PASSWORD and give it the value from Docker Hub, and create a second one with the value from the private SSH key you generated.

Deploy Workflow

A basic workflow file is needed:

  1. Checkout the latest version of the source code
  2. Install Ruby/Kamal
  3. Add the private SSH key
  4. Deploy via Kamal
name: deploy.yml
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    env:
      KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
    steps:
      - uses: actions/checkout@v2
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.4.7
          bundler-cache: true
      - uses: webfactory/ssh-agent@v0.7.0
        with:
          ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
      - run: |
          bundle exec kamal deploy

Now when you push to the main branch, Github will automatically build and deploy your project.