Reputation: 73
I have an existing bash script that just sets about 100 variables, e.g. paths, version numbers etc. that we source as part as the first part of all our build jobs. I'm moving some of these build jobs to jenkins pipelines and am trying to find if there is a way to source the variables from that file inside the jenkins environment stage so it can be used throughout the build pipeline's subsequent steps.
I feel like I'm missing something as I'm not really familiar with groovy. i think maybe using System.getenv, but that requires they are set, and I think sourcing it in a sh script block doesnt' persist.
Below is what I am wroking with but doesn't work (though gives a conceptual idea.)
any advice would be appreciated!
#!/usr/bin/env groovy
pipeline {
agent {label "${params.VM_to_use}"}
environment {
TARGET_VM = "${params.VM_to_use}"
//I'd like to be able to access vars globally or if better way I'm up for it.
}
stages {
stage ('Get vars in preperation for subsequent build steps...'){
steps {
sh '''
source /net/machine1/globalvars/functions/my_global_vars.sh
# product_123_version number in that file.
'''
script {
currentBuild.displayName = " ${env.product_123_version} - ${TARGET_VM}"
currentBuild.displayName = System.getenv("TESTVAR")
}
}
}
stage ('Build Step 2...'){
steps {
sh '''
# pass version to script...
/build_step_2.sh ${env.product_123_version}
'''
}
}
}
}
Upvotes: 4
Views: 5351
Reputation: 1984
There are two ways to load ( or source file in jenkins):-
The file should contains (loadFile.sh or loadFile.groovy) the data as below:-
// Comments should start like this one
env.Location='Pune'
env.Day='Friday'
env.Job_UI='Jenkins'
You can use the below code with some modifications:-
stages {
stage('Load') {
agent { node { label 'master' } }
steps {
script {
// From the below two lines of code, you can use the one suits your need better
// def localenv = fileLoader.fromGit ("./Vars/loadFile.sh", "GITURL", 'BRANCH_NAME', 'Credentials', '')
load '/u/users/admin/loadFile.sh'
}
}
}
stage('Print') {
agent { node { label 'master' } }
steps {
script {
echo "Location :- ${env.Location}"
echo "Day :- ${env.Day}"
echo "Job :- ${env.Job_UI}"
}
}
}
}
}
Upvotes: 2