Example of Kamal deployment from Github Actions.
Add your applications .env
variables to the Github repo as a repository secret, you can find this under the repo settings => secrets and variables => actions
https://github.com/username/repo_name/settings/secrets/actions
you are going to need an ssh private key that your deployment server is aware of (add public key to servers .ssh/authorized_keys) and add the ssh private key as a repo secret
create action workflows
.github/workflows/deploy.yml
heroku style, push code trigger kamal deploy
you can extend the workflow to include testing and deploy based on test results
name: CD
on:
push:
branches:
- main
jobs:
Deploy:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
env:
DOCKER_BUILDKIT: 1
RAILS_ENV: production
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
KAMAL_REGISTRY_USERNAME: ${{ secrets.KAMAL_REGISTRY_USERNAME }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.2
bundler-cache: true
- name: Install dependencies
run: |
gem install specific_install
gem specific_install https://github.com/basecamp/kamal.git
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Run deploy command
run: kamal deploy
this this workflow you can run kamal
arbitrary commands from the github actions panel, when running the workflow manually it will ask for a command input thats executed inside the action.
.github/workflows/kamal.yml
name: KAMAL Command
on:
workflow_dispatch:
inputs:
command:
description: "KAMAL command to run"
default: "kamal app details"
jobs:
Command:
runs-on: ubuntu-latest
env:
RAILS_ENV: production
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
KAMAL_REGISTRY_USERNAME: ${{ secrets.KAMAL_REGISTRY_USERNAME }}
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.2
- name: Install dependencies
run: |
gem install specific_install
gem specific_install https://github.com/basecamp/kamal.git main
- name: Run KAMAL command
run: ${{ github.event.inputs.command }}
Thank you very much for this contribution! It worked perfectly!
The only thing I thought could be a little dangerous is that by using
gem specific_install https://github.com/basecamp/kamal.git main
you may have differences in the behavior of deploys. I just got bitten by a change in the PR basecamp/kamal#438 , because I wasn't pushing the env files before deploy...