在 Jenkins 声明管道中将 git 提交者的名称设置为环境变量

Setting git committer's name to environment variable in Jenkins declarative pipeline

在 jenkins 声明式管道中,我试图获取 git 提交者的名称,然后将其设置为 en 环境变量。获取部分工作正常,但是,无论我尝试哪种语法,它都不起作用。

例如我试过script块:

script {
    env.GIT_CULPRIT = sh('git log --format="%ae" | head -1 | awk -F"@" \'{print }\'')
    echo "culprit: ${env.GIT_CULPRIT}"
}

输出:

[Pipeline] echo culprit: null

尝试了 environment 块(在 dir 块内):

environment {
    env.GIT_CULPRIT = sh(script: 'git log --format="%ae" | head -1', returnStdout: true)
    echo "culprit: ${env.GIT_CULPRIT}"
}

输出:

WorkflowScript: 33: Missing required parameter: "name" @ line 33, column 21. environment { ^

WorkflowScript: 33: Missing required parameter: "value" @ line 33, column 21. environment { ^

来自“Jenkins Pipeline - set and use environment variables”,第二种方法应该有效。

除了:

The above only works when the environment section is inside a "stage" but would yield "null" for WORKSPACE outside the stages.

请注意,在步骤部分中,该环境变量被引用为 ${VAR},而不是 ${env.VAR}