Troubleshooting Common CI/CD Pipeline Issues in Rails

· CI/CD & GitHub Actions · By Zeeshan Ahmad

Common CI/CD Issues with Rails and Solutions

Overview

In this guide, we address common issues encountered in Rails CI/CD pipelines and their respective solutions, including PostgreSQL configuration, dependency installation, and CI workflow setup.

1. bin/rails db:test:prepare Failing

Problem: Rails couldn't connect to the PostgreSQL server.

Solution:

  • Ensure PostgreSQL is running as a service in the CI pipeline.
  • Update config/database.yml to use host: localhost and port: 5432.
  • Define PostgreSQL service in CI.yml with correct environment variables.
  • Add database setup steps:
bash
bin/rails db:create
bin/rails db:schema:load

2. PostgreSQL Service Failing to Start

Problem: Missing POSTGRES_PASSWORD environment variable.

Solution: Add required environment variables:

bash
services:
  postgres:
    image: postgres:13
    env:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: myapp_test

3. esbuild Not Found During npm run build

Problem: The esbuild package was not installed in the CI environment.

Solution: Install esbuild and ensure the build script is properly defined:

bash
"scripts": {
  "build": "esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets"
}

4. bundle Command Not Found

Problem: Missing bundler gem in the CI environment.

Solution: Install bundler using:

bash
gem install bundler --user-install

General CI Workflow Example

bash
name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:13
        ports:
          - 5432:5432
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_USER: postgres
          POSTGRES_DB: myapp_test

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: .ruby-version
          bundler-cache: true

      - name: Install Bundler
        run: gem install bundler --user-install

      - name: Install Ruby gems
        run: bundle install

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install npm dependencies
        run: npm install

      - name: Run build
        run: npm run build

      - name: Set up database
        run: bin/rails db:create db:schema:load

      - name: Run tests
        env:
          RAILS_ENV: test
        run: bin/rails db:test:prepare test test:system

Conclusion

These solutions address common Rails CI/CD issues such as PostgreSQL connectivity, dependency setup, and workflow configurations. By following these steps, you can resolve typical errors and streamline your CI process.

Related articles

Home · All Tools · Blog