JenkinsにGitのPluginをインストールする方法

f:id:komeiy:20150107165649p:plain

Jenkinsをインストールした後に入れておきたいプラグインの一つであるGitです。

JenkinsにGit Pluginをインストールする

最新のバージョン情報などは公式ページで確認してください。

  1. jenkinsの管理
  2. Pluginの管理
  3. git pで検索
  4. 再起動せずにインストールを選択

githubのアクセス権を確認する

鍵の登録をせずにjenkinsユーザで認証をしようとすると、以下の通り「Permission denied 」となってしまいます。

[root@localhost .ssh]# sudo -u jenkins ssh -T git@github.com
Permission denied (publickey).

鍵の登録については以下で確認してください。

githubことはじめ<複数ユーザでpushするまでの初期設定> - ぽぽぽぽーんのネットワークとOSS

GitHubのコードをjenkinsで実行する

  1. GitHubレポジトリを作成し適当なコードを配置する
  2. jenkinsのジョブ設定でGit Pluginを有効化する
  3. Git pluginの設定欄にレポジトリと鍵の設定をする

レポジトリのURLはhttps://ではなくgit@github:としてください。設定は直感的に簡単に設定可能です。ここでは実行するとコンソールにhelloworldと表示されるだけのPythonコードを実行する例を記載しておきます。

[root@localhost jenkins]# ls workspace/
Helloworld  Helloworld-git

[root@localhost jenkins]# ls workspace/Helloworld-git/
file.py  hello.py  LICENSE  README.md

ビルドの実行した後のコンソール出力です。git configでupstreamを設定しgit fetchで差分取得をしていています。最終的にコンソールにhello worldが表示され、処理結果はSUCESSとなっています。エラーコードが1で返って来た場合にのみFailedとなります。エラー処理のさせる上で覚えておいた方がよいです。

Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/Helloworld-git
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@github.com:<username>/jenkins_cron.git # timeout=10
Fetching upstream changes from git@github.com:<username>/jenkins_cron.git
 > git --version # timeout=10
 > git fetch --tags --progress git@github.com:<username>/jenkins_cron.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 532fdfa4a0fa4f0f5f441a572eef739f081e6522 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 532fdfa4a0fa4f0f5f441a572eef739f081e6522
 > git rev-list fa47bd99555245dd6b24f1b87dae0c3faf03715e # timeout=10
[Helloworld-git] $ /bin/sh -xe /tmp/hudson4159611986054289808.sh
+ python hello.py
hello world
Finished: SUCCESS

次はjenkinsの継続的インテグレーションに関わる部分を書いておきたいと思います。


シェアして頂けると嬉しいです。
参考になったという方がいれば是非お願いしますm(_ _ )m
モチベーション維持の観点で非常に励みになります。

このエントリーをはてなブックマークに追加

Jenkins実践入門 ?ビルド・テスト・デプロイを自動化する技術 (WEB+DB PRESS plus)

Jenkins実践入門 ?ビルド・テスト・デプロイを自動化する技術 (WEB+DB PRESS plus)

開発ツール徹底攻略 (WEB+DB PRESS plus)

開発ツール徹底攻略 (WEB+DB PRESS plus)