How to set Jenkins Declarative Pipeline environmen

2020-08-03 02:57发布

问题:

I am trying to do this

    pipeline {
        agent any
        environment {
            LOCAL_BUILD_PATH=env.WORKSPACE+'/build/'
        }
        stages {
            stage('Stuff'){
                steps{
                echo LOCAL_BUILD_PATH
               }
           }
       }
   }

Result:
null/build/

How can I use Global Environments to create my environments?

回答1:

I think you should use:

steps {
  echo "${env.LOCAL_BUILD_PATH}"
}

as in "environment" step you're defining environmental variables which are later accessible by env.your-variable-name



回答2:

So this is method that I ended up using

pipeline {
    agent {
        label 'master'
    }

    stages {
        stage ("Setting Variables"){
            steps {
                script{        
                    LOCAL_BUILD_PATH = "$env.WORKSPACE/build"
                }
            }
        }

        stage('Print Varliabe'){
            steps{
                echo LOCAL_BUILD_PATH
            }
        }
    }
}         


回答3:

This a scope issue. Declare the variable, at the top and set it to null. Something like

def var = null

You should be able to set the value in a block/closure/stage and access it in another



回答4:

You can use something like this...

LOCAL_BUILD_PATH="${env.WORKSPACE}/build/"  

Remember: use "(double quote) for variable in string