Jenkins: Cannot define variable in pipeline stage

后端 未结 5 966
南旧
南旧 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: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"
                }
            }
        }
    }
    

提交回复
热议问题