繁体   English   中英

Jenkins 管道从 git 提交中获取变量

[英]Jenkins Pipeline get variable from git commit

我有一个关于 Jenkins 管道的问题。 我想要一个从 git 提交中获取数据的变量。

例如,如果 git 提交显示“版本 1.0.0”,则 Jenkins 文件中的变量应为“1.0.0”。 如果提交是 2.0.0 那么变量应该是 2.0.0

我已经看到,使用 Jenkins 中的选项changelog ,您可以从 Git 提交中获取数据,不幸的是我不知道如何将这些数据放入变量中?

谁能帮我?

我已经看过并尝试了以下

pipeline {
    when {
        changelog '1.0.0.0'
    }
    environment {
        nicevariable = " here should be the gitcommit see changelog"
    }



    agent none
    stages {
        stage("first") {
            sh "echo ${nicevariable}"
        }
    }
}

您可以动态定义环境变量,例如:

pipeline {
    agent any
    environment {
        GIT_MESSAGE = """${sh(
            script: 'git log --no-walk --format=format:%s ${GIT_COMMIT}', 
            returnStdout: true
            )}"""
    }
    stages {
        stage('test') {
            steps {
                sh 'echo "Message: --${GIT_MESSAGE}--"'
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM