Integrate CircleCI on Bitbucket with Ruby on Rails project
Tags: rails
Published: 2021-01-13 14:22:19

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;

```rb
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
```
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?

```rb
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
```

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.
```rb
- step:
    caches:
      - bundler
```
Change `bundle install` command with a specific path.
```rb
script:
  - bundle install --path vendor/bundle
```

Make a new definition looks like
```rb
definitions:
  caches:
    bundler: vendor/bundle
```

Example full file pipeline.

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

```rb
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
```