Jenkins: Cannot define variable in pipeline stage

后端 未结 5 946
南旧
南旧 2020-12-02 09:51

I\'m trying to create a declarative Jenkins pipeline script but having issues with simple variable declaration.

Here is my script:

pipeline {
   agen         


        
相关标签:
5条回答
  • 2020-12-02 10:02

    I think error is not coming from the specified line but from the first 3 lines. Try this instead :

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

    I think you had some extra lines that are not valid...

    EDIT

    From declaractive pipeline model documentation, it seems that you have to use an environment declaration block to declare your variables, e.g.:

    pipeline {
       environment {
         FOO = "foo"
       }
    
       agent none
       stages {
           stage("first") {
               sh "echo ${FOO}"
           }
       }
    }
    
    0 讨论(0)
  • 2020-12-02 10:02

    You are using a Declarative Pipeline which requires a script-step to execute Groovy code. This is a huge difference compared to the Scripted Pipeline where this is not necessary.

    The official documentation says the following:

    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}"
               }
           }
       }
    }
    
    0 讨论(0)
  • 2020-12-02 10:15

    The Declarative model for Jenkins Pipelines has a restricted subset of syntax that it allows in the stage blocks - see the syntax guide for more info. You can bypass that restriction by wrapping your steps in a script { ... } block, but as a result, you'll lose validation of syntax, parameters, etc within the script block.

    0 讨论(0)
  • 2020-12-02 10:20

    In Jenkins 2.138.3 there are two different types of pipelines.

    Declarative and Scripted pipelines.

    "Declarative pipelines is a new extension of the pipeline DSL (it is basically a pipeline script with only one step, a pipeline step with arguments (called directives), these directives should follow a specific syntax. The point of this new format is that it is more strict and therefore should be easier for those new to pipelines, allow for graphical editing and much more. scripted pipelines is the fallback for advanced requirements."

    jenkins pipeline: agent vs node?

    Here is an example of using environment and global variables in a Declarative Pipeline. From what I can tell enviroment are static after they are set.

    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
    
    0 讨论(0)
  • 2020-12-02 10:22

    Agree with @Pom12, @abayer. To complete the answer you need to add script block

    Try something like this:

    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"
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题