在jenkins文件中设置环境变量
我正在尝试用Groovy为我的项目编写jenkins构建脚本。 问题是,我想在脚本顶部定义一些变量,并在需要作为环境变量时使用它们在jenkins文件中设置环境变量,jenkins,groovy,environment-variables,jenkins-pipeline,Jenkins,Groovy,Environment Variables,Jenkins Pipeline,我正在尝试用Groovy为我的项目编写jenkins构建脚本。 问题是,我想在脚本顶部定义一些变量,并在需要作为环境变量时使用它们 def someVariable = 'foo' pipeline{ agent any stages{ stage("build"){ environment { specialParameter = someVariable }
def someVariable = 'foo'
pipeline{
agent any
stages{
stage("build"){
environment {
specialParameter = someVariable
}
steps{
...
}
}
...
}
}
我还有一些其他步骤,它们的环境变量是不同的,而且我只想更改脚本的顶部,以便能够构建其他分支,等等。因此,我只想找到一种在环境主体中使用已定义的someVariable的方法
谢谢首先,您可以使用环境部分定义整个脚本中已知的环境变量:
pipeline {
agent any
environment {
TEST='myvalue'
}
stages{
stage("build"){
steps{
...
}
}
}
}
您还可以定义仅在一个阶段中已知的变量:
pipeline {
agent any
stages{
stage("build"){
environment {
TEST='myvalue'
}
steps{
...
}
}
}
}
但对于您的解决方案(在管道上方使用def),您只需执行以下操作:
def someVariable = 'foo'
pipeline{
agent any
stages{
stage("build"){
steps{
echo someVariable
}
}
}
}
这将输出'foo'
你可以得到更多的信息
更新:
def someVariable = 'foo'
pipeline{
agent any
stages{
stage("build"){
environment {
TEST = sh(script: "echo -n ${someVariable}", returnStdout: true)
}
steps{
sh 'echo "${TEST}"'
}
}
}
}
输出:
[test] Running shell script
+ echo foo
foo
首先,您可以使用environment部分定义整个脚本中已知的环境变量:
pipeline {
agent any
environment {
TEST='myvalue'
}
stages{
stage("build"){
steps{
...
}
}
}
}
您还可以定义仅在一个阶段中已知的变量:
pipeline {
agent any
stages{
stage("build"){
environment {
TEST='myvalue'
}
steps{
...
}
}
}
}
但对于您的解决方案(在管道上方使用def),您只需执行以下操作:
def someVariable = 'foo'
pipeline{
agent any
stages{
stage("build"){
steps{
echo someVariable
}
}
}
}
这将输出'foo'
你可以得到更多的信息
更新:
def someVariable = 'foo'
pipeline{
agent any
stages{
stage("build"){
environment {
TEST = sh(script: "echo -n ${someVariable}", returnStdout: true)
}
steps{
sh 'echo "${TEST}"'
}
}
}
}
输出:
[test] Running shell script
+ echo foo
foo
刚刚找到了使用已定义环境变量的另一种方法
def getsomeVariable (){
return 'foo'
}
pipeline{
agent any
stages{
stage("build"){
environment {
specialParameter = getsomeVariable()
}
steps{
...
}
}
...
}
}
刚刚找到了使用已定义环境变量的另一种方法
def getsomeVariable (){
return 'foo'
}
pipeline{
agent any
stages{
stage("build"){
environment {
specialParameter = getsomeVariable()
}
steps{
...
}
}
...
}
}
也许我的问题模棱两可,我想你没有对我的问题给予足够的关注,我问题的最后一句明确了我想要什么。我想在“环境体”中使用声明的变量,而不是在“步骤”中,只要看看我的代码就可以了。也许我的问题不明确,我想你没有对我的问题给予足够的重视,我问题的最后一句话说明了我想要什么。我想在“环境体”中使用声明的变量,而不是在“步骤”中,只需查看我的代码。