我是从这个帖子来到这里的在 Jenkins Pipeline 的 shell 脚本部分定义变量 https://stackoverflow.com/questions/51407976/defining-a-variable-in-shell-script-portion-of-jenkins-pipeline
我的情况如下:如果生成的文件发生更改(它们每几周或更短时间更改一次),我有一个管道正在更新一些文件并在我的存储库中生成 PR。
在管道的最后,我有一个后操作,通过电子邮件将结果发送到我们的团队连接器。
我想知道是否可以以某种方式生成一个变量并将该变量包含在我的电子邮件中。
它看起来像这样,但当然它不起作用。
#!groovy
String WasThereAnUpdate = '';
pipeline {
agent any
environment {
GRADLE_OPTS = '-Dorg.gradle.java.home=$JAVA11_HOME'
}
stages {
stage('File Update') {
steps {
sh './gradlew updateFiles -P updateEnabled'
}
}
stage('Create PR') {
steps {
withCredentials(...) {
sh '''
if [ -n \"$(git status --porcelain)\" ]; then
WasThereAnUpdate=\"With Updates\"
...
else
WasThereAnUpdate=\"Without updates\"
fi
'''
}
}
}
}
post {
success {
office365ConnectorSend(
message: "Scheduler finished: " + WasThereAnUpdate,
status: 'Success',
color: '#1A5D1C',
webhookUrl: 'https://outlook.office.com/webhook/1234'
)
}
}
}
我尝试过以不同的方式引用我的变量 ${} 等...但我很确定分配不起作用。
我知道我可能可以使用脚本块来做到这一点,但我不确定如何将脚本块放入 SH 本身中,不确定这是否可行。
感谢 MaratC 的回复https://stackoverflow.com/a/64572833/5685482 https://stackoverflow.com/a/64572833/5685482和这个文档 https://e.printstacktrace.blog/jenkins-pipeline-environment-variables-the-definitive-guide/我会这样做:
#!groovy
def date = new Date()
String newBranchName = 'protoUpdate_'+date.getTime()
pipeline {
agent any
stages {
stage('ensure a diff') {
steps {
sh 'touch oneFile.txt'
}
}
stage('AFTER') {
steps {
script {
env.STATUS2 = sh(script:'git status --porcelain', returnStdout: true).trim()
}
}
}
}
post {
success {
office365ConnectorSend(
message: "test ${env.STATUS2}",
status: 'Success',
color: '#1A5D1C',
webhookUrl: 'https://outlook.office.com/webhook/1234'
)
}
}