如何将值从Jenkins函数返回到构建阶段



我想将值从groovy函数返回给我的jenkins构建阶段,以便该值可以在其他阶段用作条件。我无法弄清楚如何实现此目标。我尝试了下面类似的东西,但这没有用。

我有类似这样的东西:

pipeline
{
  agent any
  stages
  {
       stage('Sum')
       {
         steps
         {
          output=sum()
          echo output
         }
       }
       stage('Check')
       {
         when
         {
          expression
          {
           output==5
          }
         }
         steps
         {
          echo output
         }
       }
  }
}
def sum()
{
   def a=2
   def b=3
   def c=a+b
   return c
}

上述方法不起作用。有人可以提供正确的实施。

您缺少脚本步骤。如果您想在Jenkinsfile中执行普通的凹槽,则有必要。此外,如果要稍后访问,则必须将output设置为全局变量。

def output // set as global variable
pipeline{
...
stage('Sum')
{
    steps
    {
        script
        {
            output = sum()
            echo "The sum is ${output}"
        }
    }
}
...

最新更新