Ruby と Bitbucket Pipelines
このガイドでは、Bitbucket Pipelines を使用して、Docker コンテナで Ruby ソフトウェア プロジェクトをビルドおよびテストする方法について説明します。
実際に動作するパイプラインを持つデモ リポジトリをインポートしたい場合、デモ用の ruby リポジトリをご参照ください。
プロジェクトを手動でセットアップしたい場合に、bitbucket-pipelines.yml ファイルを構成して Ruby プロジェクトをビルドおよびテストする方法を次に示します。
Docker で Ruby のバージョンを指定する
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.
たとえば、bitbucket-pipelines.yml ファイルの先頭で指定して、Ruby 2.4.0 を使用できます。
image: ruby:2.4.0
pipelines:
default:
- step:
script:
- ruby -vRuby の異なるバージョンを使用したい場合、Ruby Docker イメージのタグを変更します。次の例は、Ruby 2.3.3 のコンテナを開始します。
image: ruby:2.3.3You 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.
アトラシアンのオンライン バリデーターでbitbucket-pipelines.ymlファイルを確認できます。
依存関係をインストールする
Gemfile を使用している場合、スクリプトの最初で bundle install を実行することで、すべての依存関係を簡単にインストールできます。
image: ruby:2.4.0
pipelines:
default:
- step:
script:
- bundle installgem install コマンドで依存関係を明示的にインストールすることもできます。
image: ruby:2.4.0
pipelines:
default:
- step:
script:
- gem install railsデータベース
Bitbucket Pipelines では、サービスを定義して適切な段階でインスタンス化することで、パイプラインの実行中に追加のサービスを起動できます。
We've created a list of examples of how to configure your bitbucket-pipeline.yml file for your favourite database.
テスト
ローカルでアプリケーションをテストするために使用したコマンドと同じものを、bitbucket-pipelines.yml ファイルに追加します。たとえば RSpec を使用している場合、次のように構成することで、依存関係をインストールしてテストを実行できます。
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.
たとえば、パイプラインの一部として PostgreSQL データベースを構成する方法は次のようになります。
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_passwordbitbucket-pipelines.ymlファイルはアトラシアンのオンライン バリデーターで確認できることを覚えておいてください。
この内容はお役に立ちましたか?