使用在 Jenkinsfile 内部构建的其他作业中创建的变量

Using variables created inside other job build inside on Jenkinsfile

我需要读取在另一个作业中创建的一些变量。用伪代码更容易解​​释: 我的工作:

{
build job:"create cluster" //this job will create some vars (cluster_name)
//used this var from my job
echo "${cluster_name}"
}

最好使用声明式管道,但我始终可以使用脚本{}

首先,在您的 create cluster 工作中,您需要将该变量放入环境变量中。你可以这样做

//create cluster Jenkinsfile
env.CLUSTER_NAME = cluster_name

然后在您的上游作业中,您可以使用 build 步骤的结果接收该变量。

def result = build job: 'create cluster'
echo result.buildVariables.CLUSTER_NAME