Step-by-Step Guide to Deploying Rails 8 with Kamal and GitHub Actions
Posted on
If you're looking to automate the deployment of your Rails 8 app using Kamal and GitHub Actions, this guide will walk you through the process, from creating the necessary files to setting up secrets and configuring your Docker registry.
1. Set Up Your GitHub Actions Workflow
First, create a GitHub Actions workflow file inside your .github/workflows folder. Name the file something like deploy.yml.
First, create a GitHub Actions workflow file inside your .github/workflows folder. Name the file something like deploy.yml.
Here's a basic configuration that runs after the CI workflow completes successfully:
name: Deploy to Production on: workflow_run: workflows: ["CI"] types: - completed jobs: deploy: if: ${{ github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest container: image: ghcr.io/basecamp/kamal:latest steps: - name: Checkout repository uses: actions/checkout@v3 - name: Set up SSH run: | mkdir -p ~/.ssh echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa - name: Disable Git safe directory check run: git config --global --add safe.directory '*' - name: Deploy Application env: KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }} RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }} run: | kamal deploy
This workflow checks out the code, sets up SSH, and then runs the kamal deploy command.
2. Generate an SSH Key for GitHub
You’ll need to create an SSH key for secure communication between GitHub and your server:
You’ll need to create an SSH key for secure communication between GitHub and your server:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Follow the prompts to save the key. Once generated, add the public key to your server’s ~/.ssh/authorized_keys file, and add the private key to your GitHub repository as a secret (SSH_PRIVATE_KEY).
3. Add Secrets to GitHub
You'll need to add several secrets to GitHub:
You'll need to add several secrets to GitHub:
- SSH_PRIVATE_KEY: The private key generated in the previous step.
- KAMAL_REGISTRY_PASSWORD: Your Docker Hub password (assuming Docker Hub is your registry).
- RAILS_MASTER_KEY: The master key for your Rails application (found in config/master.key).
To add these secrets:
- Go to your GitHub repository.
- Click Settings > Secrets and variables > Actions.
- Click New repository secret and add each key-value pair.
If you’re not using Docker Hub as your registry, check the Kamal docs on Docker registry configuration to adapt the workflow.
4. Update .kamal/secrets File
Make sure your .kamal/secrets file references the GitHub secrets:
Make sure your .kamal/secrets file references the GitHub secrets:
KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD RAILS_MASTER_KEY=$RAILS_MASTER_KEY
5. Push!
For a full list of Rails 8 changes, refer to the Rails 8 Release Notes.