Skip to content

Instantly share code, notes, and snippets.

@juuh42dias
Created March 10, 2026 14:30
Show Gist options
  • Select an option

  • Save juuh42dias/b197ee4bc0c141fc36d11159d28ba0eb to your computer and use it in GitHub Desktop.

Select an option

Save juuh42dias/b197ee4bc0c141fc36d11159d28ba0eb to your computer and use it in GitHub Desktop.
ci-cd Github Actions with Kamal
name: CI/CD
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
RUBY_VERSION: '4.0.0'
jobs:
test:
name: Test
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: my_todo_app_test
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true
- name: Install dependencies
run: bundle install
- name: Set up database
run: bundle exec rails db:create db:schema:load
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/my_todo_app_test
RAILS_ENV: test
- name: Run tests
run: bundle exec rails test
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/my_todo_app_test
RAILS_ENV: test
- name: Run Rubocop
run: bundle exec rubocop --parallel
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true
- name: Install Kamal
run: gem install kamal
- name: Add server to known hosts
run: |
mkdir -p ~/.ssh
rm -f ~/.ssh/known_hosts
ssh-keyscan -H ${{ vars.SERVER_IP }} >> ~/.ssh/known_hosts 2>/dev/null || true
echo "Host ${{ vars.SERVER_IP }}
StrictHostKeyChecking no
UserKnownHostsFile ~/.ssh/known_hosts" >> ~/.ssh/config
- name: Set up SSH agent
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Setup Rails credentials
run: |
echo "${{ secrets.RAILS_MASTER_KEY }}" > config/master.key
- name: Deploy with Kamal
run: |
mkdir -p .kamal
echo "KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD" > .kamal/secrets
echo "RAILS_MASTER_KEY=$RAILS_MASTER_KEY" >> .kamal/secrets
echo "POSTGRES_USER=my_todo_app" >> .kamal/secrets
echo "POSTGRES_PASSWORD=$DB_PASSWORD" >> .kamal/secrets
echo "DB_PASSWORD=$DB_PASSWORD" >> .kamal/secrets
kamal accessory boot db || true
kamal deploy
env:
KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }}
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment