gpt4 book ai didi

jenkins-pipeline - 声明性 Jenkins 管道中的引用变量

转载 作者:行者123 更新时间:2023-12-03 09:00:15 25 4
gpt4 key购买 nike

我正在使用下面的groovy来调用bat命令,无论我如何在bat命令中引用LOCAL_WORKSPACE,它都不会对其进行评估。我缺少什么?

错误

nuget restore $env.LOCAL_WORKSPACE "Input file does not exist: $env.LOCAL_WORKSPACE"

脚本

pipeline {
agent any
stages {
stage('Clone repo') {
steps {
deleteDir()
git branch: 'myBranch', changelog: false, credentialsId: 'myCreds', poll: false, url: 'http://myRepoURL'
}
}
stage ("Set any variables") {
steps{
script{
LOCAL_BUILD_PATH = "$env.WORKSPACE"
}
}
}
stage('Build It, yes we can') {
parallel {
stage("Build one") {
steps {
echo LOCAL_BUILD_PATH
bat 'nuget restore %LOCAL_WORKSPACE%'
}
}
}
}
}

}

最佳答案

您无法设置变量以在阶段之间共享数据。基本上每个脚本都有自己的命名空间。

您可以做的是使用环境指令,如 pipeline syntax docs 中所述。 。这些常量是全局可用的,但它们是常量,因此您无法在任何阶段更改它们。

不过您可以计算这些值。例如,我使用 sh 步骤来获取 master 上的当前提交数量,如下所示:

pipeline {

agent any

environment {
COMMITS_ON_MASTER = sh(script: "git rev-list HEAD --count", returnStdout: true).trim()
}

stages {

stage("Print commits") {
steps {
echo "There are ${env.COMMITS_ON_MASTER} commits on master"
}
}
}
}

关于jenkins-pipeline - 声明性 Jenkins 管道中的引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51213193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com