How to set Jenkins Declarative Pipeline environments with Global Variables?

风流意气都作罢 提交于 2019-12-10 00:14:24

问题


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



来源:https://stackoverflow.com/questions/45743132/how-to-set-jenkins-declarative-pipeline-environments-with-global-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!