Jenkins 将输入分配给环境变量

Jenkins assign input to environment variable

尝试将 1 个阶段的用户输入分配给另一个阶段使用,想用 env 变量来做,但无法弄清楚

到目前为止的代码:

environment {
    access_key =''
}
stages {
        stage('User input for Auth'){
            input {
                message 'enter access_key'
                parameters {
                    string 'access_key_input'
                    string 'secret_key_input'
                }
            }
            environment {
                access_key = sh(script:"echo ${access_key_input}", returnStdout: true).trim()
            }
            steps{
                sh "echo ${env.access_key}"
            }
        }
        stage("Build") {
            steps { 
                sh "echo ${env.access_key}"
               }
       }
}

日志:

+ echo XXX

+ echo XXX
XXX

+ echo null
null

您可以将 input 分配给一个全局变量,然后在任何您想要的地方访问它。

def INPUT_PARAMS = null

pipeline {
    agent {
        node {
            label 'any'
        }
    }

    options {
        timestamps()
    }

    stages {
        stage('User input for Auth') {
            steps{
                script {
                    INPUT_PARAMS = input message: "enter access_key", parameters: [
                            string(description: 'Access key', defaultValue: '', name: 'access_key_input'),
                            string(description: 'Secret access key', defaultValue: '', name: 'secret_key_input') 
                        ]
                }
                sh "echo ${INPUT_PARAMS.access_key_input}"
            }
        }

        stage("Build") {
            steps { 
                sh "echo ${INPUT_PARAMS.access_key_input}"
            }
        }
    }
}

输入请求

输出

Started by user Praveen Premaratne
Replayed #152
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on devops-jenkins in /home/jenkins/workspace/Test
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (User input for Auth)
[Pipeline] script
[Pipeline] {
[Pipeline] input
[2021-07-15T22:35:33.625Z] Input requested
[2021-07-15T22:35:43.540Z] Approved by Praveen Premaratne
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
[2021-07-15T22:35:43.886Z] + echo a
[2021-07-15T22:35:43.886Z] a
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[2021-07-15T22:35:44.230Z] + echo a
[2021-07-15T22:35:44.230Z] a
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS