1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jenkins + GitHubで、RailsのCIの仕組みを整える②

Last updated at Posted at 2017-03-25

前回まで(Jenkins + GitHubで、RailsのCIの仕組みを整える① - Qiita)で、Jenkinsと必要なプラグインのインストールが終わりました。
今回は、プラグインの設定してジョブを作成します。

プラグインの設定

Git pluginの設定

Jenkinsの管理 > システムの設定 から、「Git plugin」の項目で
「Global Config user.name Value」にbotアカウント名を、「Global Config user.email Value」にメールアドレスを入力する。

GitHub pull request builderの設定

アクセストークンについて

JenkinsがGitHubとやりとりをするためには、botアカウントのアクセストークンが必要になる。
アクセストークンとは、認証に必要な情報のことで、通常はユニークなランダム英数字(32ケタなど)である。
詳しくは、アクセストークンとは?-Webサービスにおけるユーザー認証-|キャリアエヌ(career.n)を参照。

アクセストークンの取得

Jenkinsの管理 > システムの設定 から、「GitHub pull request builder」の項目で
「Create API Token」をクリック。
「Username temp」と「Password temp」を入力し、「Create Token」をクリックすると、
Jenkinsがbotアカウントからアクセストークンを取得してくれる。

システムの設定-Jenkins--1024x479.png

システムの設定created-Jenkins--1024x479.png

原理は、githubのAPIトークンを取得する - Qiitaのようにやっていると思われる。

一度保存し、再び>GitHub pull request builderの設定画面に戻る。
「Test Credentials...」をクリック。
認証が通っているか、テストする。

システムの設定test-Jenkins--1024x479.png

Jenkinsにジョブを登録する

新規ジョブ作成

「新規ジョブ作成」をクリックする

Jenkins-1-1024x479.png

ジョブ名を入力し、ジョブの種類を選択する。今回はシェル・スクリプトで書くので、「フリースタイル・プロジェクトのビルド」を選択する。

新規ジョブ作成-Jenkins-1-1-1024x479.png

GitHubリポジトリの指定

プルリク自動テスト-Config-Jenkins-github--1024x479.png

Gitリポジトリ・ブランチの指定

以下を設定する。
[Repository URL] git@github.com:mokuo/mokumoku_library.git
[Refspec] +refs/pull/:refs/remotes/origin/pr/
[Branch Specifier (blank for 'any')] ${sha1}

ソースコード管理-1024x479.png

Failed to connect to repositoryエラーが出る場合

Failed to connect to repository : Error performing command: git ls-remote -h https://github.com/mokuo/mokumoku_library.git HEAD

が出てしまう。

プルリク自動テスト-Config-Jenkins-エラー-1024x479.png

私の場合はそもそも、gitをインストールしていなかった。

$ sudo yum -y install git

これで解決した。

参考
GitHub pull request builder plugin - Jenkins - Jenkins Wiki

ビルド・トリガ

「GitHub Pull Request Builder」にチェックを入れ、
「Admin list」にリポジトリ所有者のGitHubユーザ名を入力する。

ビルド環境の設定

「rbenv build wrapper」にチェックをし、Rubyのバージョンを指定
「高度な設定」をクリック

RSpec実行-Config-Jenkins_高度な設定--1024x479.png

「RBENV_ROOT」の値を、デフォルトから変更する。今回は、「$HOME/.rbenv-jenkins」にする。

RSpec実行-Config-Jenkins_環境変数--1024x479.png

参考文献
$HOME/.rbenv-jenkins

ビルド手順の追加

「ビルド手順の追加」で、「シェルの実行」を選択し、以下のコマンドを記述する。

bundle install
rake db:create
rake db:migrate
rspec

※bin/をつけると、Permission deniedされた。

RSpec実行-Config-Jenkins_シェルスクリプト--1024x479.png

参考サイト
Amazon Linux上のJenkinsでrbenv + Bundler + Rspecを動かす - Qiita

以上で、設定は完了。

プルリクエストを送ってみる

mokuo-mokumoku_library-at-3-1024x479.png

Comparing-master...-3-%B7-mokuo-mokumoku_library-1024x479.png

BUILD FAILED (Amazon Linux AMI 2016.09 using ruby-build 20160913-13-g8ef0c34)

Rubyのビルドに失敗した。

Updated ruby-build.
Installing 2.3.0...
$ bash -c "env RBENV_ROOT\=\$HOME/.rbenv RBENV_VERSION\=2.3.0 CONFIGURE_OPTS\= RUBY_CONFIGURE_OPTS\= \$HOME/.rbenv/bin/rbenv install 2.3.0"
Downloading ruby-2.3.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
Installing ruby-2.3.0...

BUILD FAILED (Amazon Linux AMI 2016.09 using ruby-build 20160913-13-g8ef0c34)

Inspect or clean up the working tree at /tmp/ruby-build.20161104155915.17795
Results logged to /tmp/ruby-build.20161104155915.17795.log

Last 10 log lines:
config.sub already exists
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20161104155915.17795/ruby-2.3.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

AWS Linux にコンパイラが入っていないのが原因っぽい。
gccをインストールする。

$ sudo yum -y install gcc

参考
Amazon LinuxにRuby on Railsをインストールするとか。 | 俺ん家

$ bash -c "mkdir $HOME/.rbenv-jenkins/.lock"

が延々続く

$ sudo rm -r /var/lib/jenkins/.rbenv-jenkins/.lock/

ERROR: Loading command: install (LoadError) cannot load such file -- zlib

$ sudo yum -y install zlib-devel

参考サイト
AWS+Rails+Nginx+Unicornの設定 - DesignAssembler

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?