gpt4 book ai didi

jenkins - 如何在管道的后部分定义变量?

转载 作者:行者123 更新时间:2023-12-04 17:31:44 24 4
gpt4 key购买 nike

我想在管道后部分的不同条件下使用一些通用值,因此我尝试了以下操作 -

1.

post {  
script {
def variable = "<some dynamic value here>"
}
failure{
script{
"<use variable here>"
}
}
success{
script{
"<use variable here>"
}
}
}

2
post {  
def variable = "<some dynamic value here>"
failure{
script{
"<use variable here>"
}
}
success{
script{
"<use variable here>"
}
}
}

但它导致编译错误。

你能建议我如何在 post 中声明一个变量吗?哪个部分可以跨条件使用?

最佳答案

您可以使用 always条件是 guaranteed to be executed before any other conditions喜欢 successfailure .如果您想存储 String值,您可以使用 env变量来存储它(环境变量总是将给定的值转换为字符串)。或者,您可以在 pipeline 之外定义一个全局变量。然后使用 always 中的预期动态值对其进行初始化状况。考虑以下示例:

def someGlobalVar

pipeline {
agent any

stages {
stage("Test") {
steps {
echo "Test"
}
}
}

post {
always {
script {
env.FOO = "bar"
someGlobalVar = 23
}
}

success {
echo "FOO is ${env.FOO}"
echo "someGlobalVar = ${someGlobalVar}"
}
}
}

输出:
Running on Jenkins in /home/wololock/.jenkins/workspace/pipeline-post-sections
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Test
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
FOO is bar
[Pipeline] echo
someGlobalVar = 23
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

关于jenkins - 如何在管道的后部分定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59030610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com