DEV Community

Nuttee
Nuttee

Posted on

Integrate CircleCI on Bitbucket with Ruby on Rails project

First of all, I want to share my experience integrating CircleCI to the Bitbucket Cloud repository with a Rails project using Postgres as a Database to run automatically test the project.

You need to create and commit a .bitbucket-pipelines.yml in your project’s root directory.

Once you push a commit to Bitbucket, CircleCI will automatically build and test the project.

Below example is a sample build without Postgres;

image: ruby:2.6.6

pipelines:
  default:
    - parallel:
      - step:
          name: Test
          script:
            - bundle install
            - RAILS_ENV=test bundle exec rake db:setup
            - RAILS_ENV=test bundle exec rake db:test:prepare
            - RAILS_ENV=test bundle exec rake test
Enter fullscreen mode Exit fullscreen mode

From the example built above, I'm using Ruby version 2.6.6 you can adjust what version you're using instead. It's enough to let CircleCI run the test automatically.

What if you use Postgres as a Database?

image: ruby:2.6.6

pipelines:
  default:
    - parallel:
      - step:
          name: Test
          script:
            - apt-get update
            - apt-get install -y build-essential libpq-dev nodejs
            - export DATABASE_URL=postgresql://test_user:test_user_password@localhost/pipelines
            - bundle install
            - RAILS_ENV=test bundle exec rake db:setup
            - RAILS_ENV=test bundle exec rake db:test:prepare
            - RAILS_ENV=test bundle exec rake test
          services:
            - postgres

definitions:
  services:
    postgres:
      image: postgres
      environment:
        POSTGRES_DB: pipelines
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_user_password
Enter fullscreen mode Exit fullscreen mode

Above 2 samples, bundle install is still running without being cached every time it takes a bit of time to set it up.

By having a cache, we would save the building time.

Define bundler into caches step.

- step:
    caches:
      - bundler
Enter fullscreen mode Exit fullscreen mode

Change bundle install command with a specific path.

script:
  - bundle install --path vendor/bundle
Enter fullscreen mode Exit fullscreen mode

Make a new definition looks like

definitions:
  caches:
    bundler: vendor/bundle
Enter fullscreen mode Exit fullscreen mode

Example full file pipeline.

By the way, some of the other dependencies that might happen based on your project.

image: ruby:2.6.6

pipelines:
  default:
    - parallel:
      - step:
          caches:
            - bundler
          name: Test
          script:
            - apt-get update
            - apt-get install -y build-essential libpq-dev nodejs
            - export DATABASE_URL=postgresql://test_user:test_user_password@localhost/pipelines
            - bundle install --path vendor/bundle
            - RAILS_ENV=test bundle exec rake db:setup
            - RAILS_ENV=test bundle exec rake db:test:prepare
            - RAILS_ENV=test bundle exec rake test
          services:
            - postgres

definitions:
  caches:
    bundler: vendor/bundle
  services:
    postgres:
      image: postgres
      environment:
        POSTGRES_DB: pipelines
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_user_password
Enter fullscreen mode Exit fullscreen mode

Top comments (0)