1
2

More than 3 years have passed since last update.

Jenkins Pipeline 便利なプラグイン

Posted at

zipを作成したい

Pipeline Utility Stepsをインストール後、script{} 内で以下のように使えば簡単にzipが作成されます

Jenkinsfile
pipeline {
   agent any
   stages {
      stage("Artifact") {
         steps {
            script {
               // Hoge というディレクトリを Fuga.zip としてzip化します
               zip archive: true, dir: 'Hoge', glob: '', zipFile: 'Fuga.zip'
            }
         }
      }
   }
}

ファイル検索したい

同じくPipeline Utility Stepsを使って以下のようにファイル検索もできます。(この例は全ての.mdファイルを取得しています。)

Jenkinsfile
pipeline {
   agent any
   stages {
      stage("FindFiles") {
         steps {
            script {
               def files = findFiles(glob: '**/*.md') 
               files.each { file -> 
                 echo "${file.name}"
                 echo "${file.lastModified}"
               }
            }
         }
      }
   }
}

ビルド実行時のユーザー名を取得したい

build user varsをインストール後、script{} 内で以下のように使えばユーザIDやユーザ名が取得できます。

Jenkinsfile
pipeline {
   agent any
   stages {
      stage("BuildUserName") {
         steps {
            script {
              wrap([$class: 'BuildUser']) {
                def user = env.BUILD_USER_ID
              }
              echo user
            }
         }
      }
   }
}

ビルドが終わったらSlackで通知したい

Slack側の設定

Slackで通知したいチャンネルを選択し、その他 > アプリを追加する > Jenkins CI をインストールします。

スクリーンショット 2021-05-08 12.38.00.png
スクリーンショット 2021-05-08 12.42.47.png

Slackに追加するとトークンが取得できるのでコピーしておきます。

Jenkins側の設定

1.Slack Notificationをインストール
2.Jenkinsの管理 > Manage Credentials から認証情報を追加

  • 種類: Secret text
  • スコープ: グローバル
  • Secret: 先ほど取得したトークン
  • ID: 空でOK
  • 説明: お好きな説明を

スクリーンショット 2021-05-08 12.57.56.png

3.Jenkinsの管理 > システムの設定 > Slack に以下を設定

  • WorkSpace: SlackのWorkSpace
  • Credential: 先ほど追加した認証情報
  • Default channel: 通知したいチャンネル

スクリーンショット 2021-05-08 12.59.55.png

あとは以下のようなPipelineをかけば、ビルドが終わったらSlackで通知されます

Jenkinsfile
pipeline {
   agent any
   stages {
      stage("Build") {
         steps {
            echo "Build"
         }
      }
   }
   post {
      success {
         slackSend  color: 'good',message: "build success."
      }
      failure {
         slackSend  color: 'danger', message: "build failure."
      }
   }

}

build user varsプラグインと組み合わせて、通知するときにメンションにビルドを実行したユーザを指定することもできます。(Jenkinsのユーザー名をSlackのメンションと同じにしている場合)

Jenkinsfile
pipeline {
   agent any
   stages {
      stage("Build") {
         steps {
            echo "Build"
         }
      }
   }
   post {
      success {
         script {
            wrap([$class: 'BuildUser']) {
               slackSend  color: 'good',message: "<@${env.BUILD_USER}> build success."
            }
         }
      }
      failure {
         slackSend  color: 'danger', message: "build failure."
      }
   }

}

※Slackとの連携方法はボットを作る方法もあり、今はそちらがメインのようです。
ボットから連携する場合はslackUserIdFromEmailでEmailからSlackのUserIdが取れるようです

There's two ways to authenticate with slack using this plugin.
Using the "Jenkins CI" app written by Slack, it's what is known as a 'legacy app' written directly into the slack code base and not maintained anymore.
Creating your own custom "Slack app" and installing it to your workspace.

ビルド名を変更したい

デフォルトの状態だとビルド番号のみしか表示されないので結構見ずらいです。

スクリーンショット 2021-05-08 13.54.28.png

Build Name and Description Setterをインストール後、以下のようにするとビルド名を編集できます。

Jenkinsfile
pipeline {
   agent any
   stages {
      stage("BuildName") {
         steps {
            script {
               // 『#ビルド番号 HOGE』 と表示されます。
               buildName "#${env.BUILD_NUMBER} HOGE"
            }
         }
      }
   }
}

スクリーンショット 2021-05-08 14.41.19.png

Jenkinsのビルド履歴画面の方も変更されています。見やすい!
ビルド時のタグ名とか表示させるともっとわかりやすいですね。

スクリーンショット 2021-05-08 14.42.00.png

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