0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2021-04-21

はじめに

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

関連記事

Jenkins - z/OS連携: (1) 概要
Jenkins - z/OS連携: (2) Pipelineの利用
Jenkins - z/OS連携: (3) Pipeline+Gitの利用
Jenkins - z/OS連携: (4) Pipeline+Git+Webhookの利用
Jenkins - z/OS連携: (5) Pipeline+Git+DBBの利用
Jenkins - z/OS連携: (6) Jenkins Pipeline利用シナリオ

全体像

image.png

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

環境情報

前回記事と同様

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

Git Server: Repositoryの作成

Repositoryの作成とアクセス・トークンの準備は前回記事と同様です。アクセス・トークンは使いまわしできるので、リポジトリだけ別のものを用意しておきます。

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

image.png

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:ノード定義

前回記事と同様

Jenkins: Pipeline定義

新規ジョブの作成からMultibranch Pipelineを選択します。
image.png

Branch SourcesのセクションでAdd Source からGitを選択し、必要な値を適宜設定します。
image.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が行われます。

Git RepositoryのSCAN時のログ
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

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

最初のジョブ実行時のログ
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

GitServer: Webhookの設定

Git Repository配下のSettingタブからHooksを選択
Add webhookボタンをクリック
image.png

以下のようにWebhookの設定を行います。
image.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

Pipeline実行時のログ
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

まとめ

今回の試した内容は以下のイメージです。
image.png

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

0
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
0
0