Jenkins 声明式管道 - 根据工作区中命令运行的输出动态填充输入步骤的选择

JSh*_*use 1 groovy jenkins jenkins-pipeline

我想创建一个输入步骤来提示用户选择 git 标签。为此,我想用 . 返回的值填充下拉框git tag

这是我当前的管道:

pipeline {
    agent any
    stages {
        stage('My Stage') {
            input {
                message "Select a git tag"
                parameters {
                    choice(name: "git_tag", choices: TAGS_HERE, description: "Git tag")
                }
            }
            steps {
                echo "The selected tag is: ${git_tag}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望 TAGS_HERE 成为包含命令给出的输出的变量或方法git tags

到目前为止我已经尝试过:

  • 在上一步中将标签设置为环境变量 - 不起作用,因为由于某种原因这些变量在输入块中无法访问
  • 调用一个单独的 groovy 方法来运行命令并返回输出 - 不起作用,因为工作区丢失并且命令全部运行在/

我已经广泛搜索了解决方案,但我能找到的所有示例都通过专门使用脚本化管道步骤或使用不依赖于工作区的命令来避免这两个陷阱。

Sma*_*Tom 5

通过改进@hakamairi的答案,你可以这样做:

pipeline {
    agent any
    stages {
        stage('My Stage') {
            steps {
                script {
                    def GIT_TAGS = sh (script: 'git tag -l', returnStdout:true).trim()
                    inputResult = input(
                        message: "Select a git tag",
                        parameters: [choice(name: "git_tag", choices: "${GIT_TAGS}", description: "Git tag")]
                    )
                }
            }
        }
        stage('My other Stage'){
            steps{
                echo "The selected tag is: ${inputResult}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)