1
2

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 3 years have passed since last update.

GithubとJenkinsの連携

Posted at

概要

GithubのWebhooksを用いてJenkinsと連携をさせる方法を記述します。
尚、各ソフトウェアのインストール手順は割愛します。

環境

  • Windows10
  • Jenkins
  • Gitbash
  • ngrok
  • JDK1.8.0_172
  • Maven

Jenkins

JDK

Jenkins -> Jenkinsの管理 -> Global Tool Configuration -> JDK -> インストール済みJDK

項目 内容
名前 任意の文字列 (例:JDK)
JAVA_HOME JDKがインストールされているパス
(例:C:\Program Files\Java\jdk1.8.0_172)
自動インストール チェック無

Git

Jenkins -> Jenkinsの管理 -> Global Tool Configuration -> Git -> インストール済みGit

項目 内容
名前 任意の文字列 (例:Git)
Git実行形式へのパス git.exeがインストールされているパス
(例:C:\Users\ユーザー名\AppData\Local\Programs\Git\cmd\git.exe)
自動インストール チェック無

Maven

Jenkins -> Jenkinsの管理 -> Global Tool Configuration -> Maven -> インストール済みMaven

項目 内容
名前 任意の文字列 (例:Maven)
Git実行形式へのパス Mavenがインストールされているパス
(例:C:\apache-maven-3.6.3)
自動インストール チェック無

APIトークン

Jenkins -> Jenkinsの管理 -> ユーザーの管理 -> 任意のユーザーID -> 設定 -> APIトークン

項目 内容
Default Name 任意の文字列 (例:JenkinsApiToken)

Default Nameに任意の文字列を入力後、「Generate」ボタンをクリックします。

新規ジョブの作成

Jenkins -> 新規ジョブ作成

項目 内容
Enter an item name 任意の文字列 (例:testjob)

Enter an item nameにジョブ名を入力後、「フリースタイル・プロジェクトのビルド」を選択後「OK」ボタンをクリックします。

ジョブの設定

General

General -> Github Project

項目 内容
Github project チェック有
Project url 連携させるGithubのリポジトリURL
(例:https://github.com/ユーザー名/リポジトリ―名.git/)

ソースコード管理

ソースコード管理 -> Git

項目 内容
リポジトリURL 連携させるGithubのリポジトリURL
(例:https://github.com/ユーザー名/リポジトリ―名.git/)
ビルドするブランチ 任意のブランチ名 (例:*/master)

ビルド・トリガ

ビルド・トリガ -> リモートからビルド (例: スクリプトから)

項目 内容
認証トークン 任意の文字列 (例:JenkinsApiToken)

ビルド

ビルド -> ビルド手順の追加 -> Mavenの呼び出し

項目 内容
使用するMaven 「Jenkins -> Jenkinsの管理 -> Global Tool Configuration -> Maven -> インストール済みMaven」で設定した名前を指定
使用するMaven Maven名
ゴール test

ngrok

ngrokとは?

ngrokとは、localhostで動いているサーバーを、LANの外からアクセスできるようにできるツールです。今回はローカル環境で動いているJenkinsを外部 (Github) からアクセスさせたいため、ngrokを使用しています。

ngrokの起動

ngrokをダウンロードしたディレクトリに移動して、下記コマンドで起動。

ngrok.exe http 8080
ngrok by @inconshreveable
(Ctrl+C to quit)
Session Status                online
Session Expires               7 hours, 59 minutes
Version                       2.3.35
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://aaabbbcccddd.ngrok.io -> http://localhost:8080
Forwarding                    https://eeefffgggfff.ngrok.io -> http://localhost:8080
Connections                   ttl     opn     rt1     rt5     p50     p90
0       0       0.00    0.00    0.00    0.00                     

ngrokの動作確認

ブラウザから http://aaabbbcccddd.ngrok.io へアクセスすると、vagrantの8080番ポートにアクセスが可能となります。また、ngrokのアドレスは、再起動すると毎回変わります。

Github

Webhook

連携させるGithubのリポジトリ -> Settings -> Webhooks -> Add webhook

項目 内容
Payload URL 任意のPayload URL
Content type application/x-www-form-urlencoded
Secret 空白
Which events would you like to trigger this webhook? Just the push event.
Active チェック有

Payload URLのフォーマット

http://[JENKINS_USERID]:[API_TOKEN]@[JENKINS_HOST]/job/[JOB_NAME]/buildWithParameters?token=[TOKEN_NAME]

http://jenkins_userid:aaaaabbbbbcccccddddd@http://aaabbbcccddd.ngrok.io/testjob/build?token=JenkinsApiToken

上記、全て設定後、「Add webhook」ボタンをクリックします。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?