I'm unable to find a way to pass the GIT_COMMIT variable to the jenkins pipeline.
There are two steps, first step checks out the repo and second step creates the codeql report. To publish the report I need the recent ccommit id from the repo and pass it in the shell command.
Use something like this for checkout step:
def scm = checkout(...)
scm.each{ k, v -> env."${k}" = v }
All the env variables below will be set and can be used anywhere
GIT_BRANCH
GIT_CHECKOUT_DIR
GIT_COMMIT
GIT_PREVIOUS_COMMIT
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL
Related
I have a declarative pipeline job (this is not multi-branch pipeline job using Jenkinsfile) without parameters but some stages are conditional based on value in environment variable:
stage('deploy-release') {
when {
environment name: 'GIT_BRANCH', value: 'master'
}
steps {
sh "mvn deploy:deploy-file -B -DpomFile=pom.xml -Dfile=target/example.jar -DrepositoryId=maven-releases -Durl=${NEXUS_URL}/repository/maven-releases/"
}
}
I want to trigger the job from external system but I need to pass correct value of given environment variable. Is there some way how to do that via Jenkins Remote API?
For passing value of given environment variable, you need to define parameters with the exact same name as that of environment variable for your job by selecting "This build is parameterized".
You can refer Parameterized Build
In the Jenkins --> Manage Jenkins --> Manage Nodes --> Configure Node, under Node Properties, you can configure Environment Variables for the Node.
Is there a way to use them in a Pipeline Script?
Now I have to do something like
...
environment {
GITMIRRORS='/home/jenkins/git-mirrors'
DLC117='/progress/117_64/dlc'
}
...
As a result, I'll have those paths in a lot of Pipeline scripts. But they are defined on the Node...
Is there a way to say 'use that agent' and 'use its env variables from the main Jenkins Node configuration'?
If not, is there way to say GITMIRRORS=GetNodeEnvVariable('...', '...')
Yes, you can use them in the following ways:
echo"${GITMIRRORS}"
or you can use it in your code as:
gitMirrors = env.GITMIRRORS
How to use Jenkins Node Environment Variables in a Pipeline script?
Jenkins issue (dated back from 2017-05...)
If you can afford the change :
move any conflicting definition of the envvar from the global setup to all the node setups
or load a script or define the adequate variable value in the Jenkinsfile
references :
https://issues.jenkins-ci.org/browse/JENKINS-44425
https://issues.jenkins-ci.org/browse/JENKINS-44465
I did not find a way to do it with pure Groovy. But you can execute a command on the node and capture its output in a script block:
def GITMIRRORS = bat(script: "#echo %GITMIRRORS%", returnStdout: true).trim()
on Windows agent or
def GITMIRRORS = sh(script: "echo ${GITMIRRORS}", returnStdout: true).trim()
on Linux agent.
I need to read some vars created inside another job. Easier to be explain with pseudo code:
my job:
{
build job:"create cluster" //this job will create some vars (cluster_name)
//used this var from my job
echo "${cluster_name}"
}
The best will be with declarative pipelines but I can always use script {}
Firstly in your create cluster job you need to put that variable into environment variable. You can do it this way
//create cluster Jenkinsfile
env.CLUSTER_NAME = cluster_name
Then in your upstream job you can receive that variable using a result of build step.
def result = build job: 'create cluster'
echo result.buildVariables.CLUSTER_NAME
If I define an environment variable (eg. VersionNum) under Jenkins Global Properties, can I update the value within a pipeline script? I was hoping to use it to store version information and update according to script execution results.
What I want to do is write a pipeline script like:
node {
stage {'Stage1') {
VersionNum = '5'
}
}
that will update the global environment variable so the new value that will persist and can be used by other Jenkins jobs.
Rather than try to use the global environment variable, I read a properties file with the Pipeline Utility Steps plugin:
def props = readProperties file:"${WORKSPACE}\\BuildVersion.properties"
MajVersion = props['MAJOR_VERSION'].trim()
MinVersion = props['MINOR_VERSION'].trim()
Then if I change a value, I write it back with:
bat "(echo MAJOR_VERSION=${MajVersion} && echo MINOR_VERSION=${MinVersion}) \u003E \"%WORKSPACE%\\BuildVersion.properties\""
Jenkins version: 2.6, Linux
Problem: The parameterized build variables are not are not visible (as env variables) in the Execution step "shell script", They used to be visible in the older 1.x jenkins version.
Steps:
Create a parameterized build with a multi configuration project.
Add a parameter to the build (using This project is parameterized-> string parameter, {if that matters} ).
Add a build step "Execute shell" to the job.
Access these parameters in this shell script as env variables.
echo "++++++++++++ building $lib_name ($lib_version) ++++++++++++++"
To solve this, I have tried to create a groovy script in "Prepare an environment for the run" section. I created env variables using hardcoded values which are pased to shell script as env vars.
def map = ['lib_name':'lib1']
map['lib_version'] = 'master'
return map
But, without hardcoding, I cannot access these variable values even when using solution from
How to retrieve Jenkins build parameters using the Groovy API?
I dont know what else has to be done. Can some one please suggest?
---> Updating based on the comments on this question:
When I run the following lines in jenkins, I get exception:
def buildVariablesMap = Thread.currentThread().executable.buildVariables
buildVariablesMap.each{ k, v -> println "${k}:${v}" }
FATAL: No such property: executable for class: hudson.model.OneOffExecutor
groovy.lang.MissingPropertyException: No such property: executable for class: hudson.model.OneOffExecutor
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:66)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
I have had also similar problem. This is a solution which worked for me. I created a method which always takes and delivers exactly one build parameter which I need.
method:
String readingBuildParameters(VariableResolver varRes, String paramName){
return varRes.resolve(paramName)
}
the line how I use it in a code:
Build currentBuild = Executor.currentExecutor().currentExecutable
VariableResolver varResolver = currentBuild.getBuildVariableResolver()
df_parameter = readingBuildParameters(varResolver, "parameter_name")
BR,
Zoltan
To access the parameters in your shell script:
to evaluate them in echo: e.g. echo "${myParam}"
to use them in code: def myNewvalueParam = ${myOtherParam}
To retrieve build variables from groovy script as a build step:
def buildVariablesMap = Thread.currentThread().executable.buildVariables
println buildVariablesMap['BUILD_NUMBER']
But please note that for your custom/altered environment variables to be visible for the next steps you should use EnvInject plugin, with it you can define a step that will export new env variables as key-value pair just like properties file.
This was a bug in jenkins, probably in the credentials plugin:
https://issues.jenkins-ci.org/browse/JENKINS-35921
Thanks for all your help!