Streamlining Development with CI/CD in Rails

Total Blog Views: 78

Blog Status: publish

Created By: swaz_ahmed Created at: 06-14-2024

Tags: CI/CD

In today's fast-paced software development world, ensuring that your code is reliable, secure, and consistently delivered is crucial. Continuous Integration (CI) and Continuous Deployment/Delivery (CD) are practices that help achieve this by automating testing and deployment processes. For Ruby on Rails applications, integrating CI/CD can significantly enhance productivity and code quality. In this blog, we'll explore the fundamentals of CI/CD in Rails and guide you through setting up a CI/CD pipeline.

What is CI/CD?

Continuous Integration (CI) is a development practice where developers frequently integrate their code into a shared repository. Each integration is verified by an automated build and tests, allowing teams to detect problems early.

Continuous Deployment (CD) extends CI by automatically deploying the tested code to production. Continuous Delivery, a variant of CD, ensures the code is always in a deployable state but may require a manual step to deploy.

Benefits of CI/CD in Rails

  • Early Bug Detection: Automated tests run on every commit, catching bugs early in the development cycle.
  • Faster Delivery: Automating the deployment process speeds up the release cycle.
  • Consistency: Automation ensures the same steps are followed every time, reducing human error.
  • Improved Collaboration: CI encourages frequent integration, making it easier to collaborate and merge changes.

Setting Up CI/CD for a Rails Application

Step 1: Setting Up Your Rails Project

First create a new rails app:

 

rails new myapp
cd myapp

Initialize a Git repository and push it to GitHub:

 

git init 
git remote add origin https://github.com/yourusername/myapp.git 
git add . 
git commit -m "Initial commit"
git push -u origin master 

 

Step 2: Configuring GitHub Actions for CI

Create a .github/workflows directory in your project root and add a configuration file, ci.yml, for GitHub Actions:

 

name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      db:
        image: postgres:latest
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
          POSTGRES_DB: myapp_test
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-ruby@v1
      with:
        ruby-version: '3.0'
    - name: Install dependencies
      run: |
        gem install bundler
        bundle install
    - name: Set up database
      env:
        RAILS_ENV: test
        DATABASE_URL: postgres://postgres:password@localhost:5432/myapp_test
      run: |
        bin/rails db:create
        bin/rails db:migrate
    - name: Run tests
      run: bin/rails test 

 

This configuration sets up a workflow to run tests on every push and pull request to the master branch.

Step 3: Setting Up Continuous Deployment to Heroku

To deploy your application to Heroku, you'll need to set up another GitHub Actions workflow. Create a file deploy.yml in the .github/workflows directory:

 

name: CD

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Setup Ruby
      uses: actions/setup-ruby@v1
      with:
        ruby-version: '3.0'
    - name: Install dependencies
      run: |
        gem install bundler
        bundle install
    - name: Deploy to Heroku
      env:
        HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
        HEROKU_APP_NAME: your-heroku-app-name
      run: |
        git remote add heroku https://git.heroku.com/${{ env.HEROKU_APP_NAME }}.git
        git push heroku master

 

 

You'll need to add your Heroku API key to your repository's secrets:

  1. Go to your repository on GitHub.
  2. Click on Settings.
  3. Navigate to Secrets and click on New repository secret.
  4. Add a secret with the name HEROKU_API_KEY and your Heroku API key as the value.

Step 4: Deploying and Monitoring

Once your workflows are set up, every push to the master branch will trigger the CI workflow to run tests. If the tests pass, the CD workflow will deploy the application to Heroku.

Summary

Implementing CI/CD in your Rails application can make a huge difference in your development process. It helps maintain code quality and speeds up delivery. Tools like GitHub Actions and Heroku make it easy to set up an efficient CI/CD pipeline.

With automated testing and deployment, you can spend more time on what really matters: creating awesome features and enhancing your app.


swaz_ahmed

I am swaz_ahmed blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads