簡體   English   中英

僅當提交消息包含“[ci:deploy]”時如何運行部署階段

[英]How to run deploy stage only if commit message contains `[ci:deploy]`

我在 Jenkins 和 Github 中有一條管道。

pipeline {
    agent any
    stages {
        stage('Verify Branch') {
            steps {
                echo "$GIT_BRANCH"
            }
        }
        stage('Build') {
            steps {
                echo "build now"
            }
        }
        stage('Test') {
            steps {
                echo "test now"
                // sh '''#!/bin/bash
                //  echo "hello world" 
                //  exit 1
                // '''
            }
        }
        stage('Deploy') {
            steps {
                echo "deploy now"
            }
        }
    }
}

僅當提交消息包含[ci:deploy]時,如何告訴 Jenkins 運行部署階段?

例如,僅當我的提交是: [ci:deploy] deploy the app然后運行部署階段。

要獲取 Git 提交消息,您可以使用此處解釋的示例。 貼在這里供參考:

git log --oneline -1 ${GIT_COMMIT} # prints SHA and title line
git log --format="medium" -1 ${GIT_COMMIT} # print commit, author, date, title & commit message

除此之外,您還可以使用 Jenkins 提供的when表達式。 來自官方博客的片段

pipeline {
    agent any

    environment {
        git_commit_msg = sh(returnStdout: true, script: 'git log --format="medium" -1 ${GIT_COMMIT} | tail -1').trim()
    }

    stages {
        stage ('Check string') {
            when {
                // Use 'git_commit_msg' here to check the string you're interested in
                expression { env.git_commit_msg ==~ /[cd:deploy]/ }
            }
            steps {
                echo "Found string!"
            }
        }
    }
}

Groovy 正則表達式: https://www.regular-expressions.info/groovy.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM