Ruby with Bitbucket Pipelines

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.

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

It is not yet possible to use multiple containers in a running pipeline, but you can use or create a Docker image that contains both Ruby and the required database. You will find below some examples of images that you can re-use or adapt to meet your requirements.

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. You can use one of the examples in the Databases section to create your own Dockerfile with the right version of Ruby for your application. For instance you can use the Dockerfile below to create a Docker image with Ruby 2.3.1 and PostgreSQL that you can push back to Docker Hub as shown in the example repository.

Dockerfile
FROM ruby:2.3.1
RUN apt-get update \
  && apt-get install -y postgresql postgresql-contrib \
  && apt-get install sudo \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# This Dockerfile doesn't need to have an entrypoint and a command
# as Bitbucket Pipelines will overwrite it with a bash script.

To use that image with a Rails project you will need to start and configure the database as part of your pipeline script:

bitbucket-pipelines.yml
image: spittet/ruby-postgresql

pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          - /etc/init.d/postgresql start
          - sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password 'mypassword';"
          - bundle install
          - rake db:setup
          - rake db:test:prepare
          - rspec

Was this helpful?

Thanks for your feedback!

Why was this unhelpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport