ruby on rails

Testing Ruby on Rails Application with Bitbucket Pipelines

6733 VIEWS

·

Bitbucket Pipelines is the new way to test, build and deploy your application directly inside your source repository. You don’t need to share your code with other services anymore. Bitbucket Pipelines allows you to focus on your code and deliver to cloud platforms like AWS, Heroku and Azure, as well as your own server. In this article, we will configure Pipelines to run tests on Ruby on Rails API. (We’ll not address building and deploying to cloud platforms here.)

Activating Pipelines

The first step is enabling Bitbucket Pipelines in your repository. To do this, click on the Pipelines icon in the left bar, as shown in the image below.

Now, choose Ruby as the language template. This will generate the first version of the configuration file of Pipelines.

To see the Pipeline in action, just commit the configuration file without making any changes.

When you commit your file, the Pipeline will immediately execute all the steps defined in the configuration file. Below, you can see the Pipeline main page. On the right, you can monitor the log showing the execution of each step. You can also click on “View configuration” to go back to the configuration file page showing the last version edited.

Then, you can click on “Edit” and make your changes to the configuration file. (Be aware that any update in this file or any commit in your project will run the Pipeline if it was activated.)

Editing Pipelines configuration

To test our Ruby on Rails application, we can use the ruby-postgresql image to avoid installing and configuring Ruby and PostgreSQL from zero. (For this example, we will use this image available in Docker Hub.) As you can see, any image in Docker Hub can be used, even your own image. (This is very helpful if you already have the image to build your application.)

The first line of the configuration is the definition of the image that will be used to build our application. (Notice that we can define the steps by environments, which is relevant with deployment to staging and production.) Then, we add the steps inside the script key. Here, you will add all the commands that you use locally to test your application, starting with the installation of dependencies.

We need to include some additional definitions to use PostgreSQL. The first is to export the language to use UTF-8. Second, we need to start the PostgreSQL service. To finish, we must create a new user with privileges to create a database (which I’ve called “root,”) and create the test database.

After this is complete, we can add the Rake commands to set up and run the tests. Below, you can see the full initial configuration:

image: spittet/ruby-postgresql

pipelines:
 default:
   - step:
       deployment: test
       script:
         - bundle install
         - export LC_ALL=C.UTF-8
         - export LANG=en_US.UTF-8
         - export LANGUAGE=en_US.UTF-8
         - /etc/init.d/postgresql start
         - sudo -u postgres sh -c 'createuser root --createdb'
         - sudo -u root sh -c 'createdb index_content_api_test'
         - rake db:create RAILS_ENV=test
         - rake db:migrate RAILS_ENV=test
         - rspec

To improve our flow, we can add the cache to the bundler and decrease the time to run the steps. To do this, include the key caches inside the steps and add the definitions below the pipeline configurations. The complete configuration will look like this:

image: spittet/ruby-postgresql

pipelines:
 default:
   - step:
       deployment: test
       caches:
         - bundler
       script:
         - bundle install
         - export LC_ALL=C.UTF-8
         - export LANG=en_US.UTF-8
         - export LANGUAGE=en_US.UTF-8
         - /etc/init.d/postgresql start
         - sudo -u postgres sh -c 'createuser root --createdb'
         - sudo -u root sh -c 'createdb index_content_api_test'
         - rake db:create RAILS_ENV=test
         - rake db:migrate RAILS_ENV=test
         - rspec

definitions:
 caches:
   bundler: ./vendor

That’s it! If every step was executed correctly, you’ll see the green “Successful” label.

Conclusion

Bitbucket offers 50 minutes to run Pipelines per month with their free plan. This will probably not be enough to configure your Pipeline (because you’ll need to run to check each change of configuration). However, if you choose the Standard plan, you will have 500 minutes to run Pipelines. Most other services start at a price of $29, and if you already use Bitbucket, it could be a better option.

Bitbucket Pipelines is a cheap and integrated alternative to building and deploying in place, and eliminates the need for other continuous integration services to access your code.


Brena Monteiro is a Tech Lead passionate about mentoring new developers. A professional who has experience in the hire, mentoring, and leader development teams, and building scalable APIs and integrates it with partners and cloud services. Enthusiastic about architectural improvement using cloud services.


Discussion

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar