Jenkins Pipeline SSH Publisher环境变量跨阶段步骤传递
文章目录
最近在编写Jenkins流水线,需要在其它阶段计算环境变量,然后在下一阶段使用到的sshPublisher用到该变量。 经调试很多次都没成功,最后发现关键在于命令外围需要用双引号括起来。以下是具体例子。
environment {
DOCKER_ENV_CMD=''
DOCKER_HOST_CMD=''
DOCKER_RUN_CMD=''
}
stage ('预处理') {
steps {
script {
def dockerRunCmd = sh(script: "echo -e \"docker run -d -p $docker_node_port:$docker_app_port -v /etc/localtime:/etc/localtime -v $app_log_path:/app/logs ${DOCKER_ENV_CMD} ${DOCKER_HOST_CMD} --name $docker_container_name $docker_image_url:$docker_image_version\"", returnStdout: true).trim()
DOCKER_RUN_CMD = dockerRunCmd
echo "=================================="
echo " DOCKER_RUN_CMD => ${DOCKER_RUN_CMD}"
echo "=================================="
}
}
}
stage ('重启应用') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'test-app', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: """sudo docker rm -f $docker_container_name
sudo ${DOCKER_RUN_CMD}""", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
文章作者 HiOFD
上次更新 2024-04-08