Ruby with Bitbucket Pipelines

This guide shows you how to use Bitbucket Pipelines for building and testing a Ruby software project in a Docker container.

If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo ruby repo.
Instructions to import the demo repo

For this demo, all you need to do is import this repo and enable pipelines to get your first pipeline to run!

  1. Import this repository by visiting bitbucket.org and log in. Then:

    Import gif

  2. On the left navigation bar, click Pipelines and scroll down to view the bitbucket-pipelines.yml file. Click Enable

    Enable gif

  3. Watch your build run!

    Build gif

Note: Bitbucket Pipelines includes fifty free minutes per account, at the time of writing. You can check your team or account's minutes usage for the month by clicking your Avatar > Bitbucket settings > Plan details.




If you'd like to set it up by hand, here's how you can configure the bitbucket-pipelines.yml file to build and test a Ruby project.

Specify your Ruby version with Docker

Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file. You can easily use Ruby with Bitbucket Pipelines by using one of the official Ruby Docker images on Docker Hub. If you use the default Ruby image it will come with the bundler installed by default to help you manage your dependencies.

For instance, you can use Ruby 2.4.0 by specifying it at the beginning of your bitbucket-pipelines.yml file:

image: ruby:2.4.0
pipelines:
  default:
    - step:
        script:
          - ruby -v

If you want to use a different version of Ruby you simply need to change the tag for the Ruby Docker image. For example, here's how you would start a container with Ruby 2.3.3:

image: ruby:2.3.3

You can find a list of all supported Ruby versions and corresponding image tags at https://hub.docker.com/r/library/ruby/.

Note that the Rails Docker images have been deprecated in favor of the standard Ruby images mentioned above.

You can check your bitbucket-pipelines.yml file with our online validator.

Install dependencies

If you are using a Gemfile, you can simply run bundle install at the beginning of your script to install all the dependencies:

image: ruby:2.4.0
pipelines:
  default:
    - step:
        script:
          - bundle install

You can also install dependencies explicitly with the gem install command:

image: ruby:2.4.0
pipelines:
  default:
    - step:
        script:
          - gem install rails

Databases

Bitbucket Pipelines allows you to launch extra services during the execution of your pipeline by defining the service, and instantiating it on the appropriate step.

We've created a list of examples of how to configure your bitbucket-pipeline.yml file for your favourite database.

Test

You simply need to add to your bitbucket-pipelines.yml file the same commands that you would run locally to test your application. For instance, if you are using RSpec the following configuration would install your dependencies and then run your tests:

image: ruby:2.4.0
pipelines:
  default:
    - step:
        script:
          - bundle install
          - rspec


If you are building a Rails application it is highly likely that you will require a database to run your tests. We've created a list of examples of how to configure your bitbucket-pipeline.yml.

For example, here's how you'd configure a PostgreSQL database as part of your pipeline:

bitbucket-pipelines.yml
image: ruby:2.3.1

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          - export DATABASE_URL=postgresql://test_user:test_user_password@localhost/pipelines
          - bundle install
          - rake db:setup
          - rake db:test:prepare
          - rspec
        services:
          - postgres
 
definitions:
  services:
    postgres:
      image: postgres
      environment:
        POSTGRES_DB: pipelines
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_user_password


Remember, you can check your bitbucket-pipelines.yml file with our online validator.

Last modified on Jan 2, 2019

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.