Jenkins 在sh脚本中使用环境变量
我试图在Jenkins 在sh脚本中使用环境变量,jenkins,jenkins-pipeline,terraform,jenkins-groovy,Jenkins,Jenkins Pipeline,Terraform,Jenkins Groovy,我试图在sh脚本中访问env,但无法访问它们。我想将env的值附加到sh脚本中。因为我想运行一个特定的terraform模块,所以我想在terraform apply和terraform output前面附加值 pipeline { agent any parameters { choice( choices: 'first\nsecond\n', description: 'number',
sh
脚本中访问env
,但无法访问它们。我想将env
的值附加到sh脚本中。因为我想运行一个特定的terraform模块,所以我想在terraform apply和terraform output前面附加值
pipeline {
agent any
parameters {
choice(
choices: 'first\nsecond\n',
description: 'number',
name: 'name'
)
}
stages {
stage("set env variable"){
steps{
script{
if ( params.name== 'first'){
env.output = "first_dns"
env.module = "module.first"
}
else if (params.name == 'second'){
env.output = "second_dns"
env.module = "module.second"
}
}
}
}
stage('Deployment') {
steps {
script {
sh '''#!/bin/bash
terraform apply -target=${env.module} -auto-approve
terraform output {env.output}
'''
}
}
}
}
}
}
问题是Jenkins正在注入环境变量,但您需要像在普通shell脚本中那样访问它们。 由于使用单引号,变量将在shell脚本的运行时进行计算,它将无法找到这些变量。 这应该起作用:
stage('Deployment') {
steps {
script {
sh '''#!/bin/bash
echo ${module}
echo ${output}
'''
}
}
}
或者,如果您使用双引号,那么您所写的内容也会起作用。
这样,jenkins将在执行之前替换这些值
stage('Deployment') {
steps {
script {
sh """#!/bin/bash
echo ${env.module}
echo ${env.output}
"""
}
}
}