use*_*591 0 bash shell groovy jenkins jenkins-pipeline
我正在尝试在常规步骤中再次使用 sh""" """ 在 shell 脚本中设置的 VAR_NAME 值,但出现以下错误。我只看到有关如何在 shell 中使用 groovy 变量的问题,但没有看到其他方式。提前致谢。
groovy.lang.MissingPropertyException:没有这样的属性:类的VAR_NAME:groovy.lang.Binding
pipeline {
environment {
VAR_NAME=""
}
stages {
stage('Compute') {
steps {
sh """
#!/bin/bash
set -e
set +x
VAR_NAME=10
"""
sh "echo VAR_NAME = $VAR_NAME"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您发出sh指令时,会bash创建一个新的 shell 实例(很可能)。与通常的 Unix 进程一样,它继承父进程的环境变量。bash然后您的实例正在运行您的脚本。当您的脚本设置环境变量时,环境bash会更新。一旦你的脚本结束,bash运行脚本的进程就会被销毁,它的所有环境也随之被销毁。
如果要使用该 shell 实例设置的任何内容,则需要将其引入,例如:
def script_output = sh(returnStdout: true, script: """
#!/bin/bash
set -e
set +x
VAR_NAME=10
echo \$VAR_NAME
""")
script_output = script_output.trim()
VAR_NAME = script_output
echo "VAR_NAME is ${VAR_NAME}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1653 次 |
| 最近记录: |