notebook

都内でWEB系エンジニアやってます。

GitLab CIに入門する

GitLab CIに入門する

最近やっとGitLab-CIデビューしたのでその時の雑なメモ

container registryへのイメージ登録

rspecを実行するときにMySQLへの接続が必要になるのでそのあたりのパッケージを追加するためだけのイメージを作ってpushしてみます

  • login
docker login sample.com(registryのドメイン)
  • イメージのbuild
docker build . -t sample.com/swfz/app
  • registryへpush

↑で指定したタグ名

docker push ${tag}

イメージの登録はこれだけです

CIを回してみる

サンプルとしてRailsアプリを作ってrspecを実行してみます

  • rspec
  • カバレッジ(simplecov-rcov)
  • 構文解析(rubycritic)

その前に 名称とか概念とか

実際に触ってみたらイメージできてない概念があったので先に書いておきます

stage

パイプラインを制御するためのもので、job毎に使用できるステージを定義するものです

なのでdependenciesとセットになることが多いです

同じステージのjobは並行で実行されます

なのでstage1のjobが2つあって両方共成功してからstage2のjobが実行されるとかそいう感じのイメージですね

Configuration of your jobs with .gitlab-ci.yml | GitLab

docs.gitlab.com

artifacts

成果物のパスを指定に使います

job単体だと特定のjobから成果物をDLできるようになります

また、ここで指定した成果物が後続のjobでそのまま引き継がせることができます

後続のjobに残せるのは指定したディレクトリのみのようです

ただ、artifactに指定したディレクトリは依存するjobが複数あってそのjobの両方でartifactを指定してあってもよしなにマージしてくれるようです

これは使っててとても便利に思いました

Introduction to job artifacts | GitLab

docs.gitlab.com

実際にRspec + カバレッジレポートを出力する

SimpleCov-Rcovを入れる

  • Gemfile

coverageレポート生成用のGemを入れます

gem 'simplecov'
gem 'simplecov-rcov'
  • spec/spec_helper.rb
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails'

これだけだとSpec実行時にエラーが出てしまいます

下記を参考にモンキーパッチで対応しました

Compatibility issue with ruby 2.5 - Encoding::UndefinedConversionError: ... from ASCII-8BIT to UTF-8 · Issue #20 · fguillen/simplecov-rcov

github.com

Rspec実行

coverage以下に成果物ができます

設定

  • .gitlab-ci.yml
image: sample.gitlab.com/vagrant/sample/app
stages:
  - test
  - build_static
rspec:
  stage: test
  services:
    - mysql:5.7
  variables:
    MYSQL_DATABASE: sample_test
    MYSQL_ROOT_PASSWORD: mysql_strong_password
  before_script:
    - "echo \"install: --no-document\" > ~/.gemrc"
    - "echo \"update: --no-document\" >> ~/.gemrc"
    - bundle install --jobs=4
    - bundle exec rake db:setup RAILS_ENV=test
    - bundle exec rake db:migrate RAILS_ENV=test
  script:
    - bundle exec rspec
  artifacts:
    paths:
      - coverage/

pages:
  stage: build_static
  dependencies:
    - rspec
  script:
    - mv coverage/ public/
  artifacts:
    paths:
      - public
  only:
    - master

パイプラインの流れ

  • rspec
    • テストの実行、カバレッジレポートの生成
  • build_static
    • 成果物をpublicに移動するだけ
  • deploy(pages)
    • publicに成果物が入ったら自動で追加される模様
    • GitLab Pagesで閲覧可能になります

f:id:swfz:20190119044303p:plain
パイプライン

rubycriticも入れてみる

続いてRubycriticも入れてみます

  • gitlab-ci/rubycritic.sh
#!/bin/sh -xe

gem install rubycritic
rubycritic ./lib ./app

exit 0
  • .gitlab-ci.yml
stages:
  - test
  - build_static

rspec:
  stage: test
  services:
    - mysql:5.7
  variables:
    MYSQL_DATABASE: sample_test
    MYSQL_ROOT_PASSWORD: mysql_strong_password
  before_script:
    - "echo \"install: --no-document\" > ~/.gemrc"
    - "echo \"update: --no-document\" >> ~/.gemrc"
    - bundle install --jobs=4
    - bundle exec rake db:setup RAILS_ENV=test
    - bundle exec rake db:migrate RAILS_ENV=test
  script:
    - bundle exec rspec
  artifacts:
    paths:
      - coverage/

rubycritic:
  image: ruby2.5.1
  stage: test
  variables:
    LANG: C.UTF-8
  script:
    - gitlab-ci/rubycritic.sh
  artifacts:
    paths:
      - tmp/rubycritic/
  only:
    - master

pages:
  stage: build_static
  dependencies:
    - rspec
    - rubycritic
  script:
    - mv coverage/ public/
    - mv tmp/rubycritic/ public/rubycritic
  artifacts:
    paths:
      - public
  only:
    - master
rubycritic:
  variables:
    LANG: C.UTF-8

上記はrubycriticの実行でコケたため対応した結果です

イメージとの組み合わせで環境変数の設定が必要らしいです。。。

/usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html/line.rb:22:in `delete': invalid byte sequence in US-ASCII (ArgumentError)
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html/line.rb:22:in `render'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html/code_file.rb:42:in `block in render'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html/code_file.rb:39:in `each'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html/code_file.rb:39:in `with_index'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html/code_file.rb:39:in `render'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html_report.rb:36:in `block (2 levels) in create_directories_and_files'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html_report.rb:35:in `open'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html_report.rb:35:in `block in create_directories_and_files'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html_report.rb:33:in `each'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html_report.rb:33:in `create_directories_and_files'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/generators/html_report.rb:20:in `generate_report'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/reporter.rb:6:in `generate_report'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/commands/default.rb:29:in `report'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/commands/default.rb:19:in `execute'
    from /usr/local/bundle/gems/rubycritic-3.5.2/lib/rubycritic/cli/application.rb:20:in `execute'
    from /usr/local/bundle/gems/rubycritic-3.5.2/bin/rubycritic:10:in `<top (required)>'
    from /usr/local/bundle/bin/rubycritic:23:in `load'
    from /usr/local/bundle/bin/rubycritic:23:in `<main>'
ERROR: Job failed: exit code 1
  • 参考

ArgumentError: invalid byte sequence in US-ASCII on v2.2.0 in Docker · Issue #277 · ffaker/ffaker

github.com

f:id:swfz:20190119044353p:plain
パイプライン

build_staticではrspecの成果物とrubycriticの成果物をpublicに移動してあげているだけです

それぞれ別のディレクトリ切って移してあげるだけですね

実際の画面

サンプルなのでほとんどコード書いてないから大したデータも出てこないのですが

下記のような対応で静的ファイルが見れるようになりました

  • GitLabPagesのURL/sample/coverage/rcov/

f:id:swfz:20190119044420p:plain
Coverage

  • GitLabPagesのURL/sample/rubycritic/overview.html

f:id:swfz:20190119044445p:plain
rubycritic

まとめ

GitLab自体に慣れるまでに少し時間を使ってしまった気がしますが慣れればとても楽にjobをつくってパイプラインをつなげていけるのでこれはいいな、と思いました

なんと言っても処理の流れをイメージがしやすい!

これを機にどんどん自動化させていきたいと思います