Jenkins:无法在管道阶段定义变量

Jenkins: Cannot define variable in pipeline stage

我正在尝试创建声明性 Jenkins 管道脚本,但在简单变量声明方面遇到问题。

这是我的脚本:

pipeline {
   agent none
   stages {
       stage("first") {
           def foo = "foo" // fails with "WorkflowScript: 5: Expected a step @ line 5, column 13."
           sh "echo ${foo}"
       }
   }
}

但是,我得到这个错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Expected a step @ line 5, column 13.
           def foo = "foo"
           ^

我正在使用 Jenkins 2.7.4 和 Pipeline 2.4。

我认为错误不是来自指定的行,而是来自前 3 行。试试这个:

node {
   stage("first") {
     def foo = "foo"
     sh "echo ${foo}"
   }
}

我认为您有一些无效的额外行...

从 declaractive pipeline model documentation 看来,您似乎必须使用 environment 声明块来声明您的变量,例如:

pipeline {
   environment {
     FOO = "foo"
   }

   agent none
   stages {
       stage("first") {
           sh "echo ${FOO}"
       }
   }
}

Jenkins Pipelines 的声明式模型在 stage 块 - see the syntax guide for more info 中允许使用受限的语法子集。您可以通过将步骤包装在 script { ... } 块中来绕过该限制,但结果是,您将失去对 script 块中语法、参数等的验证。

同意, 。要完成您需要添加脚本块的答案

尝试这样的事情:

pipeline {
    agent any
    environment {
        ENV_NAME = "${env.BRANCH_NAME}"
    }

    // ----------------

    stages {
        stage('Build Container') {
            steps {
                echo 'Building Container..'

                script {
                    if (ENVIRONMENT_NAME == 'development') {
                        ENV_NAME = 'Development'
                    } else if (ENVIRONMENT_NAME == 'release') {
                        ENV_NAME = 'Production'
                    }
                }
                echo 'Building Branch: ' + env.BRANCH_NAME
                echo 'Build Number: ' + env.BUILD_NUMBER
                echo 'Building Environment: ' + ENV_NAME

                echo "Running your service with environemnt ${ENV_NAME} now"
            }
        }
    }
}

在 Jenkins 2.138.3 中有两种不同类型的管道。

声明式和脚本式管道。

”声明式管道是管道DSL的新扩展(它基本上是一个只有一个步骤的管道脚本,一个带有参数的管道步骤(称为指令),这些指令应该遵循特定的语法。这个点新格式更严格,因此对于管道新手来说应该更容易,允许图形编辑等等。 脚本化管道是高级要求的后备。"

这是在声明式管道中使用环境变量和全局变量的示例。据我所知,环境在设置后是静态的。

def  browser = 'Unknown'

pipeline {
    agent any
    environment {
    //Use Pipeline Utility Steps plugin to read information from pom.xml into env variables
    IMAGE = readMavenPom().getArtifactId()
    VERSION = readMavenPom().getVersion()


    }
    stages {
        stage('Example') {
            steps {
                script {
                    browser = sh(returnStdout: true, script: 'echo Chrome')
                }
            }
        }
        stage('SNAPSHOT') {
                when {
                    expression { 
                        return !env.JOB_NAME.equals("PROD") && !env.VERSION.contains("RELEASE")
                    }
                }
                steps {
                    echo "SNAPSHOT"
                    echo "${browser}"
                }
            }
            stage('RELEASE') {
                when {
                    expression { 
                        return !env.JOB_NAME.equals("TEST") && !env.VERSION.contains("RELEASE")
                    }
                }
                steps {
                    echo "RELEASE"
                    echo "${browser}"
                }
            }
    }//end of stages 
}//end of pipeline

您正在使用 Declarative Pipeline which requires a script-step to execute Groovy code. This is a huge difference compared to the Scripted Pipeline,但没有必要。

official documentation 表示如下:

The script step takes a block of Scripted Pipeline and executes that in the Declarative Pipeline.

pipeline {
   agent none
   stages {
       stage("first") {
           script {
               def foo = "foo" 
               sh "echo ${foo}"
           }
       }
   }
}

你可以定义全局变量,但是使用这个变量时必须写在脚本块中。

def foo="foo"
pipeline {
agent none
stages {
   stage("first") {
      script{
          sh "echo ${foo}"
      }
    }
  }
}

试试这个声明式管道,它的工作原理

pipeline {
   agent any
    stages {
      stage("first") {
        steps{
          script {
           def foo = "foo" 
           sh "echo ${foo}"
              }
            }
          }
        }
       }