LoginSignup
1
0

More than 3 years have passed since last update.

Jenkins Declarative pipelineでSlackにGitのコミットログも通知する

Posted at

小ネタ。Groovy大好き。

Jenkinsfile内で以下のメソッドを書いておいて、 pipeline ブロック内で toSlack(channnel: "hoge-channnel", tokenCredentialId: "hoge-token") とすれば使える。

def toSlack(Map args) {
    def changeMsgs = []
    currentBuild.changeSets.collect { it.items }.flatten().each { entry ->
        def commitDate = new Date(entry.timestamp).format("yyyy/MM/dd HH:mm")
        def author = entry.class.name == "hudson.plugins.git.GitChangeSet" ? "<mailto:${entry.authorEmail}|${entry.author.toString()}>" : entry.author.toString()
        changeMsgs << "・${commitDate}-> ${entry.msg} by ${author}"
    }

    slackSend(
        channel: args?.channel,
        tokenId: args?.tokenId,
        color: "good",
        message: changeMsgs.size() > 0 ? changeMsgs.join("\n") : "・なし"
    )
}

これぐらいの行数のスクリプトだったり、他のジョブと共通化したかったりすると、毎回書くのはツラいので、実際はJenkins Shared Library化してます。

なので、 slackSendcolor の部分もビルド結果を変換してたりします。

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