我是jenkins/groovy的新手,目前面临以下问题……
我在jenkinsfile中定义了一个部署管道,它包括在linux下运行的两个不同环境中部署脚本。部署脚本copy_files.sh必须在特定用户下运行才能保留权限:
def my_dst = '/opt/scripts'
pipeline {
agent { label '<a generic label>' }
stages {
stage('Deployment env1') {
agent { label '<a specific label>' }
steps {
script {
echo 'Deploying scripts...'
sh '/bin/sudo su -c "${WORKSPACE}/copy_files.sh ${WORKSPACE} ${my_dst}" - <another user>'
}
}
}
stage('Deployment env2') {
agent { label '<another specific label>' }
steps {
script {
echo 'Deploying scripts...'
sh '/bin/sudo su -c "${WORKSPACE}/copy_files.sh ${WORKSPACE} ${my_dst}" - <another user>'
}
}
}
}
}我将文件应该复制到的目标路径定义为一个变量(在两个环境中my_dst相同)。虽然环境变量$WORKSPACE得到了解析,但我的变量my_dst没有得到解析,导致copy_files.sh脚本由于缺少参数而中止...如何正确引用我的命令来解析我的变量?使用硬编码的目标路径/opt/scripts运行sudo命令。
提前发送txs
发布于 2021-09-28 11:59:24
因此,您希望Groovy注入my_dst,但WORKSPACE来自shell环境,因此您将需要使用双引号(因此groovy执行模板),并转义工作区中的$,以便Groovy不会将其作为模板,而是按原样传递给shell:
sh "/bin/sudo su -c \"\${WORKSPACE}/copy_files.sh \${WORKSPACE} ${my_dst}\" - <another user>"https://stackoverflow.com/questions/69360594
复制相似问题