2016-12-20 25 views
5

Jak wywołać globalne zmienne środowiskowe w Jenkinsfile?
Na przykład, jeśli mam zmienną -Globalne zmienne środowiskowe Jenkins w Jenkinsfile

name:credentialsId 
value:xxxx-xxxx-xxxxx-xxxxxxxxx 

Jak używać go w groovy skryptu?

Próbowałem ${credentialsId}, ale to nie zadziałało. To właśnie daje błąd:

java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, ........ 

Odpowiedz

26

W Jenkinsfile, masz "Working with the Environment", który wspomina:

The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env,

Składnia jest ${env.xxx} jak w:

node { 
    echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}" 
} 

Patrz także " Managing the Environment ".

How can I pass the Global variables to the Jenkinsfile?
When I say Global variables - I mean in

Jenkins -> Manage Jenkins -> Configure System -> Global properties -> Environment variables 

Patrz "Setting environment variables"

Setting an environment variable within a Jenkins Pipeline can be done with the withEnv step, which allows overriding specified environment variables for a given block of Pipeline Script, for example:

Jenkinsfile (Pipeline Script)

node { 
    /* .. snip .. */ 
    withEnv(["NAME=value"]) { 
     ... your job 
    } 
} 
-1

Innym składni $ ENV: xxxx

node { 
echo "Running $ENV.BUILD_ID on $ENV.JENKINS_URL" } 

ten pracował dla mnie

+0

to jest w jakiś sposób podobny do sugestii VonC. – dildeepak

+0

To zadziałało dla mnie, dlatego zasugerowałem w ten sposób również, chociaż obie są prawie takie same różnice w składni tylko –

+0

Ale chcę zdefiniować moją własną globalną zmienną środowiskową w Jenkinach zamiast definiować ją w moim pliku Jenkins. – ezlee

0

Odnosząc się do env w zakresie Groovy, wystarczy użyć env.VARIABLE_NAME, na przykład przekazać BUILD_NUMBER z upstream pracy do wyzwalania pracy:

stage ('Starting job') { 
    build job: 'TriggerTest', parameters: [ 
     [$class: 'StringParameterValue', name: 'upstream_build_number', value: env.BUILD_NUMBER] 
    ] 
}