Jenkins - z/OS連携: (4) Pipeline+Git+Webhookの利用

# はじめに
Jenkinsを活用してメインフレーム・アプリケーションの管理をどのように行えるのかを探っていく連載記事です。
今回は、前回実施したPipeline+Gitの拡張版です。Git Repositoryの更新をトリガーにPipelineを動かすということをやってみます。

## 関連記事
[Jenkins - z/OS連携: (1) 概要](https://qiita.com/tomotagwork/items/d2c90c1f29e07d3b7c19)
[Jenkins - z/OS連携: (2) Pipelineの利用](https://qiita.com/tomotagwork/items/6d0481d58cdd10e44def)
[Jenkins - z/OS連携: (3) Pipeline+Gitの利用](https://qiita.com/tomotagwork/items/16b38599be3f8a134580)
[Jenkins - z/OS連携: (4) Pipeline+Git+Webhookの利用](https://qiita.com/tomotagwork/items/25eb40e56f1a5125e4fc)
[Jenkins - z/OS連携: (5) Pipeline+Git+DBBの利用](https://qiita.com/tomotagwork/items/0b25bbb9a72e37f4d98c)
[Jenkins - z/OS連携: (6) Jenkins Pipeline利用シナリオ](https://qiita.com/tomotagwork/items/9d2b3f697f31f23fd682)

# 全体像
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/19ad7b10-c11a-24ca-2b9c-0908110c243f.png)

基本的なコンポーネントは前回記事と同様ですが、Webhookという機能を使うので、経路としてGitServer(GitHub)からJenkinsサーバーへのhttpのリクエストが流れるパスが追加されることになります。
この記事では前回と異なる部分を中心に記載します。

# 環境情報
[前回記事](https://qiita.com/tomotagwork/items/16b38599be3f8a134580#%E7%92%B0%E5%A2%83%E6%83%85%E5%A0%B1)と同様

# Gitの更新をトリガーにPipelineを実行するシナリオ

## Git Server: Repositoryの作成
Repositoryの作成とアクセス・トークンの準備は[前回記事](https://qiita.com/tomotagwork/items/16b38599be3f8a134580#git-server%E3%81%AE%E6%BA%96%E5%82%99)と同様です。アクセス・トークンは使いまわしできるので、リポジトリだけ別のものを用意しておきます。

TOMOTAG/wazi-sandbox01-pipeline03 というリポジトリを作成し、直下にJenkinsfileというPipeline Scriptと、test.txtというテキストファイルを配置しておきます。中身は基本、前回のものと同様です。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/d852a71f-56e5-f808-3d0d-afd48032898c.png)

```groovy:Jenkinsfile
pipeline {
   agent {label 'wazi-test01'}

   stages {
      stage('Hello') {
         steps {
            echo 'Hello World'
         }
      }
      stage('Hello2'){
         steps {
             sh """
               echo 'Hello World2'
               hostname
               pwd
             """
         }
      }
      stage('Hello3'){
         steps {
            sh """
               echo 'Hello World3'
               ls -la
               date > test2.txt
               cat test.txt >> test2.txt
               ls -la
               cat test2.txt
            """
         }
      }
   }
}
```

## Jenkins:ノード定義
[前回記事](https://qiita.com/tomotagwork/items/16b38599be3f8a134580#jenkins-%E3%83%8E%E3%83%BC%E3%83%89%E6%A7%8B%E6%88%90)と同様

## Jenkins: Pipeline定義

新規ジョブの作成からMultibranch Pipelineを選択します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/47341824-cf8e-63d9-0c80-99c8634d4498.png)

Branch SourcesのセクションでAdd Source からGitを選択し、必要な値を適宜設定します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/ba97dca0-13c0-de7d-749f-c8fcf4b4a182.png)

- **Branch Sources**
    - **Add Source**: Gitを選択します。
    - **プロジェクト・リポジトリ**: Pipeline scriptを配置したリポジトリのURLを指定します(https)。
    - **認証情報**: パスワードにアクセス・トークンを設定したものを指定します。
- **Build Configuration**
    - **Mode**: by Jenkinsfileを指定します。
    - **Script Path**: Pipeline Scriptを記載したファイルのパスを指定します。
- **Scan Multibranch Pipeline Triggers**
    - **Scan by webhook**: チェックを入れます。
    - **Trigger Token**: 任意の文字列を指定します(GitServerから使用)。

Pipeline定義を保存すると、そのタイミングでGit RepositoryのSCANが行われます。

<details><summary><font color="blue">Git RepositoryのSCAN時のログ</font></summary><div>

```
Started
[Mon Mar 15 02:42:29 UTC 2021] Starting branch indexing...
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_ASKPASS to set credentials GitHub Enterprise Token
 > git ls-remote https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
 > git config remote.origin.url https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
Fetching & pruning origin...
Listing remote references...
 > git config --get remote.origin.url # timeout=10
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_ASKPASS to set credentials GitHub Enterprise Token
 > git ls-remote -h https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
Fetching upstream changes from origin
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials GitHub Enterprise Token
 > git fetch --tags --progress --prune origin +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking branches...
  Checking branch master
      ‘Jenkinsfile’ found
    Met criteria
Changes detected: master (14458b810af011f2b714fea0a460f48f40eb8410 → a146c9ea53a4bf5426afdc5cbe1fd2f421f2aa61)
Scheduled build for branch: master
Processed 1 branches
[Mon Mar 15 02:42:35 UTC 2021] Finished branch indexing. Indexing took 5.5 sec
Finished: SUCCESS
```

</div></details>

また、最初のジョブ実行が走ります。

<details><summary><font color="blue">最初のジョブ実行時のログ</font></summary><div>

```
Branch indexing
 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
 > git config remote.origin.url https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials GitHub Enterprise Token
 > git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
Obtained Jenkinsfile from 14458b810af011f2b714fea0a460f48f40eb8410
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on wazi-test01 in /u/JENKIN2/agent/wazi-test01/workspace/pipeline_helloworld3_master-OKMBIVINSMOMMP6D26N7L2YGGFZJKHRQJIR54UO3A4UJDR736ROA
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential TOMOTAG
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
 > git init /u/JENKIN2/agent/wazi-test01/workspace/pipeline_helloworld3_master-OKMBIVINSMOMMP6D26N7L2YGGFZJKHRQJIR54UO3A4UJDR736ROA # timeout=10
Fetching upstream changes from https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
 > git --version # timeout=10
 > git --version # 'git version 2.14.4_zos_b09'
using GIT_ASKPASS to set credentials GitHub Enterprise Token
Using name charset 'IBM1047'
Using password charset 'IBM1047'
 > git fetch --no-tags --progress -- https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Fetching without tags
 > git config remote.origin.url https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
Fetching upstream changes from https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
using GIT_ASKPASS to set credentials GitHub Enterprise Token
Using name charset 'IBM1047'
Using password charset 'IBM1047'
 > git fetch --no-tags --progress -- https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking out Revision 14458b810af011f2b714fea0a460f48f40eb8410 (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 14458b810af011f2b714fea0a460f48f40eb8410 # timeout=10
Commit message: "update pipeline"
First time build. Skipping changelog.
 > git --version # timeout=10
 > git --version # 'git version 2.14.4_zos_b09'
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello2)
[Pipeline] sh
+ echo Hello World2 
Hello World2
+ hostname 
S0W1.DAL-EBIS.IHOST.COM
+ pwd 
/u/JENKIN2/agent/wazi-test01/workspace/pipeline_helloworld3_master-OKMBIVINSMOMMP6D26N7L2YGGFZJKHRQJIR54UO3A4UJDR736ROA
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello3)
[Pipeline] sh
+ echo Hello World3 
Hello World3
+ ls -la 
total 66
drwxr-xr-x   3 JENKIN2  SYS1        8192 Mar 15 02:36 .
drwxr-xr-x   8 JENKIN2  SYS1        8192 Mar 15 02:36 ..
drwxr-xr-x   5 JENKIN2  SYS1        8192 Mar 15 02:36 .git
-rw-r--r--   1 JENKIN2  SYS1         600 Mar 15 02:36 Jenkinsfile
-rw-r--r--   1 JENKIN2  SYS1          14 Mar 15 02:36 test.txt
+ date 
+ 1> test2.txt 
+ cat test.txt 
+ 1>> test2.txt 
+ ls -la 
total 68
drwxr-xr-x   3 JENKIN2  SYS1        8192 Mar 15 02:38 .
drwxr-xr-x   8 JENKIN2  SYS1        8192 Mar 15 02:36 ..
drwxr-xr-x   5 JENKIN2  SYS1        8192 Mar 15 02:36 .git
-rw-r--r--   1 JENKIN2  SYS1         600 Mar 15 02:36 Jenkinsfile
-rw-r--r--   1 JENKIN2  SYS1          14 Mar 15 02:36 test.txt
-rw-r--r--   1 JENKIN2  SYS1          40 Mar 15 02:38 test2.txt
+ cat test2.txt 
Mon Mar 15 02:38:13  2021
test
test
test[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
```

</div></details>

## GitServer: Webhookの設定

Git Repository配下のSettingタブからHooksを選択
Add webhookボタンをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/edafb9f0-1069-030b-b809-472d4e46928a.png)

以下のようにWebhookの設定を行います。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/ec76ee37-9e28-84f7-f0a0-0e08008a310e.png)

- **Payload URL**: `[JENKINS_URL]/multibranch-webhook-trigger/invoke?token=[token]`という書式でURLを指定します。[JENKINS_URL]はJenkins Serverにアクセスする際のURLです。[token]はPipeline定義で指定したTrigger Tokenです。
- **SSL Verification**: 証明書のチェックはDisabledにしておきます。

これで、このGit Repositoryの変更をトリガーにPipelineが自動で動くようになります。

## Pipelineの実行

PC上のGit Clientから今回準備したRepositoryに変更をPushしてみます。(例えばここではVSCodeからtest.txtの中身を変更してcommit -> pushしてみます)。

Jenkins Dashboardを確認すると、自動的にPipelineが実行されているのが確認できます。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/4377a7ff-4f7c-e99c-4bb3-55bfe295f019.png)

<details><summary><font color="blue">Pipeline実行時のログ</font></summary><div>

```
Branch indexing
 > git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
 > git config remote.origin.url https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
Fetching origin...
Fetching upstream changes from origin
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials GitHub Enterprise Token
 > git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/* # timeout=10
Seen branch in repository origin/master
Seen 1 remote branch
Obtained Jenkinsfile from aec8ed82d8c81ac608771bbe3b0491b25be087e8
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on wazi-test01 in /u/JENKIN2/agent/wazi-test01/workspace/pipeline_helloworld3_master-OKMBIVINSMOMMP6D26N7L2YGGFZJKHRQJIR54UO3A4UJDR736ROA
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential TOMOTAG
Fetching changes from the remote Git repository
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching without tags
 > git config remote.origin.url https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git # timeout=10
Fetching upstream changes from https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git
 > git --version # timeout=10
 > git --version # 'git version 2.14.4_zos_b09'
using GIT_ASKPASS to set credentials GitHub Enterprise Token
Using name charset 'IBM1047'
Using password charset 'IBM1047'
 > git fetch --no-tags --progress -- https://github.xxx.xxx/TOMOTAG/wazi-sandbox01-pipeline03.git +refs/heads/*:refs/remotes/origin/* # timeout=10
Checking out Revision aec8ed82d8c81ac608771bbe3b0491b25be087e8 (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f aec8ed82d8c81ac608771bbe3b0491b25be087e8 # timeout=10
Commit message: "test03"
 > git rev-list --no-walk a146c9ea53a4bf5426afdc5cbe1fd2f421f2aa61 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello2)
[Pipeline] sh
+ echo Hello World2 
Hello World2
+ hostname 
S0W1.DAL-EBIS.IHOST.COM
+ pwd 
/u/JENKIN2/agent/wazi-test01/workspace/pipeline_helloworld3_master-OKMBIVINSMOMMP6D26N7L2YGGFZJKHRQJIR54UO3A4UJDR736ROA
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Hello3)
[Pipeline] sh
+ echo Hello World3 
Hello World3
+ ls -la 
total 68
drwxr-xr-x   3 JENKIN2  SYS1        8192 Mar 15 06:14 .
drwxr-xr-x   8 JENKIN2  SYS1        8192 Mar 15 02:36 ..
drwxr-xr-x   5 JENKIN2  SYS1        8192 Mar 15 06:15 .git
-rw-r--r--   1 JENKIN2  SYS1         600 Mar 15 02:36 Jenkinsfile
-rw-r--r--   1 JENKIN2  SYS1          20 Mar 15 06:14 test.txt
-rw-r--r--   1 JENKIN2  SYS1          43 Mar 15 02:43 test2.txt
+ date 
+ 1> test2.txt 
+ cat test.txt 
+ 1>> test2.txt 
+ ls -la 
total 68
drwxr-xr-x   3 JENKIN2  SYS1        8192 Mar 15 06:14 .
drwxr-xr-x   8 JENKIN2  SYS1        8192 Mar 15 02:36 ..
drwxr-xr-x   5 JENKIN2  SYS1        8192 Mar 15 06:15 .git
-rw-r--r--   1 JENKIN2  SYS1         600 Mar 15 02:36 Jenkinsfile
-rw-r--r--   1 JENKIN2  SYS1          20 Mar 15 06:14 test.txt
-rw-r--r--   1 JENKIN2  SYS1          46 Mar 15 06:15 test2.txt
+ cat test2.txt 
Mon Mar 15 06:15:27  2021
test03
test03
test03[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
```

</div></details>

# まとめ
今回の試した内容は以下のイメージです。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/706d6e90-eeba-cca0-7b9d-f974a7735a7b.png)

前回はJenkins Dashboardから手動でPipelineを実行するシナリオでしたが、今回は、Git Repositoryの更新をトリガーに自動でPipelineが動くところを確認しました。