何かやってみるブログ

興味をもったこと、趣味のこと、技術について色々書きます。

CI初心者がGitLab CIを動かしてみた[Rails]

お仕事でGitLab CIを使う機会があったので、CI/CDの知識を整理しつつCIをRailsアプリケーションで動かすまでをメモとして残します。

CI / CDとは?

コードがプッシュされるたびに(変更されるたびに)テストなどが自動で走り、自動でデプロイ出来る状態に出来る状態にするツールのことです。

GitLab CIとは

GitLabに備え付けられているCI機能です。.gitlab-ci.ymlとファイルに定義したスクリプトがGitLab Runnerによって実行されます。

docs.gitlab.com

試してみる

今回はRSpecとRubocopをGitLab CIで走らせることを目標に実装します。

Railsアプリケーションの準備

Rails 6.0.2.2 ruby 2.6.0 postgresql 12.1

$ rails new sample_pj  -d=postgresql
$ rails db:create

必要なGemをインストールする

rspec-railsfactory_bot_railsrubocopGemfileに追加してbuildしなおしてインストールします。

$ bundle install

適当に機能を作る

$ rails g scaffold Post title:string content:text
$ rails db:migrate

specの設定

$ rails generate rspec:install
$ rm -r ./test
--require spec_helper
--format documentation
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
FactoryBot.define do
  factory :post do
    title { "test" }
    content  { "test" }
  end
end

適当にspecを書く

$ rails g rspec:model Post
require 'rails_helper'

RSpec.describe Post, type: :model do
  describe "post data" do
    it("is valid"){ expect(build(:post)).to be_valid }
  end

  describe "crud" do
    describe "create a user" do
      let(:post) { build(:post) }
      it { expect{post.save}.to change { Post.count }.by(1)}
    end

    describe "delete a user" do
      let!(:post) { create(:post) }
      it { expect{post.destroy}.to change { Post.count }.by(-1) }
    end

    describe "update a user" do
      let(:post) { Post.new(title: "test", content: "test") }
      it "is updated" do
        post.update(title: "updated")
        expect(post.title).to eq "updated"
      end
    end
  end
end

rubocopの設定

$ bundle exec rubocop --auto-gen-config

今回はrubocopがCIで動くのが確認できればいいので、.rubocop.ymlの中身を.rubocop-todo.ymlのファイルの中身に置き換えます。

$ bundle exec rubocop 

f:id:s-takaya1027:20200408044639p:plain

上の画像のようにrubocopがパスすればrubocopの準備は終了です。

.gitlab-ci.ymlを準備する

設定項目は以下の記事を参考にしてください。

qiita.com

image: ruby:2.6.0

services:
  - postgres:12.1

stages:
  - test

cache:
  paths:
    - vendor/bundle

rubocop:
  stage: test

  script:
    - bundle install
    - bundle exec rubocop

rspec:
  stage: test

  before_script:
    - bundle install
  script:
    - curl -sS -L https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
    - echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
    - apt-get update -q -y
    - apt-get install -y google-chrome-stable
    - curl -sL https://deb.nodesource.com/setup_12.x | bash -
    - apt-get install -y nodejs
    - npm install -g yarn
    - yarn install
    - bundle exec rails db:setup RAILS_ENV=test
    - bundle exec rspec

database.ymlを編集する

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: postgres
・
・
・

usernamepasswordを追加します。

結果

GitLabにリポジトリを作って、GitLabにpushするとGitLab CIが走ります。 以下のようになれば成功です。

f:id:s-takaya1027:20200410030156p:plain

まだ読んでないですが、良さげな本を見つけました。

投げ銭していただける方は以下のボタンからよろしくお願いいたします。

takayaにOFUSEする