What is the file path of env.properties file in jenkins? - jenkins

In job A , I am writing a value to env.properties file like below:
echo name=$name > env.properties
Is it possible to access that name in job B from the env.properties file? If yes, can you please help , how can i achieve this.

In job A, env.properties file locates in job A's workspace.
In job B, to access env.properties file in job A's workspace, you will use this path: ..\job A\env.properties
You can use Inject environment variable plugin to import that name in job B from the env.properties files
Note:
You can only use this way if both job A and job B running on same node

Related

How to use the "Extensible choice parameter" in a jenkins freestyle job located on the slave to show files

I have a freestyle job and a parameterized build.
I want to populate an Extensible Choice with all xml file names inside my workspace.
Both the job and the workspace are running on the slave.
The textbox Base Directory says that every relative path has JENKINS_HOME as root, and that is the Jenkins location on the master. Something like C:/ has the same outcome.
I have the same problem with the Active Choice Parameter.
I don't know how to get access to my workspace, with the groovy script for my parameter.
I've tried the following:
def list = []
def dir = new File("C:/<path>")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
It results in a FileNotFoundException. When I try to input a path to the master, it works fine.
I labeled the slave and the job correctly (The job will only be executed on the slave).
Does anyone has a solution?

Passing workspace url of Job A to Job B in Jenkins

I have two pipeline jobs Job A and Job B. I need to pass the workspace url of Job A (say /var/lib/jenkins/workspace/JobA) to be used by Job B. The main idea is I am trying to copy the contents of target folder which is generated due to maven build but I don't want to use Copy Artifacts Plugin or Archive Artifacts Plugin to achieve the same.
I have tried using the option "This job is parameterized" where Job A is the upstream of Job B but i am unable to so using that option.
Can anyone help to achieve the same ?
The WORKSPACE variable is an env variable from Jenkins and is pointing like / .
For eg.
If the job name is Job_A --> the workspace value will be <jenkins_path>/Job_A
For eg.
If the job name is Job_B --> the workspace value will be <jenkins_path>/Job_B
So you can't use the WORKSPACE var and expects the Job_B to point to Job_A workspace value.
The below can be used to get certain properties from the upstream job.
Jenkins - How to get and use upstream info in downstream
Even if you want to hard code it in the Job_B it will be fine(not recommended)
Also for this to work your node should be same for both the jobs
I have found a way to do the same and it is working fine.
I have made the Job B a parameterized job using "This project is parameterized" and used string parameter.
Then, in the pipeline script of Job A, i invoked the Job B by passing WORKSPACE env variable. Here is the declarative pipeline script for Job A:
pipeline {
agent any
stages
{
stage ('Build JobB')
{
steps {
build job: 'jobB', parameters: [string(name: 'UPSTREAM_WORKSPACE', value: "${env.WORKSPACE}")]
}
}
} }
Now, in Job B pipeline you can try to echo the variable UPSTREAM_WORKSPACE. This is how we can pass the workspace url and use it to copy the artifacts.

passing env. variables to downstream job after job failed

I have a jenkins upstream job, which on failure will trigger the downstream job, but I need to pass the same env. variable of upstream job in the downstream job.
I am using powershell to set the env. variables like $env:tag=$(git describe) i.e generating my git tag. How can I send this env. variable in the downstream job, without using the git url in the downstream job.
And the downstream job should only get triggered on failure of upstream job.
I have used triggered parameterized build plugin by storing the env. variable in a txt file (echo "$env:tag=$(git describe)" > env.txt) but this also not working.
I have tried few other plugins but nothing working out.
usecase: JobA wants to pass its env. variable set in the powershell ($ENV:tag) to JobB. Therefore in the configuration of JobA the predefined parameters textfield is used to resolve the name but does not work as expected. Instead the whole string "$ENV:tag" is passed instead of "env-value". Here how I configured it:
– JobA-------
predefined parameters: gittag=$ENV:tag =>pass parameter on to JobB
– JobB--------
echo gittag =>output: echo $ENV:tag => $ENV:tag
You can use Parameterized Trigger Plugin.
Here is the link for more details: https://wiki.jenkins.io/display/JENKINS/Parameterized+Trigger+Plugin
This appears to be a known issue with this plugin.
If the downstream job is only triggered when the upstream job fails, then do not make the downstream job a parameterized job. Instead, write the variables needed from the upstream job to a property file in the downstream job's workspace.
echo Var1=value>../Downstream_Job_Folder/downstream.properties
echo Var2=value>>../Downstream_Job_Folder/downstream.properties
In the downstream job, use the EnvInject Plugin as your first build step, or write a script to read the vars from the file.
PASSING ENV. VARIABLES FROM UPSTREAM TO DOWNSTREAM JOB IN JENKINS
. write the variables needed from the upstream job to a property( IN OUR CASE
env.properties file) file in the downstream job's workspace.
echo Var1=value>../Downstream_Job_Folder/downstream.properties
echo Var2=value>>../Downstream_Job_Folder/downstream.properties
---------- IN MY CASE -------------
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$commit_id=$(git rev-parse --short HEAD)
$tag=$(git describe)
echo var1=$commit_id > "C:\Program Files
(x86)\Jenkins\workspace\powershell_2\env.properties"
echo var2=$tag >> "C:\Program Files
(x86)\Jenkins\workspace\powershell_2\env.properties"
echo $commit_id
echo $tag # TO CROSS-CHECK
. In the downstream job, use the EnvInject Plugin as your first build step, AND
- give Properties File Path - env.properties
. Then choose the second build step as windows powershell , and for windows powershell to
read the env.properties file , use below commands
- $AppProps = convertfrom-stringdata (get-content env.properties -raw)
$AppProps.'var1' # will print the var1 value
$AppProps.'var2' # will print the var2 value
$env:commit_id=$AppProps.'var1'
$env:tag=$AppProps.'var2'
echo git:$env:tag
echo git:$env:commit_id # this will print the desired output
----------In case of linux -------------------
. IN THE UPSTREAM JOB
- echo "name=##" > /home/jenkins/jenkins/workspace/sh_tst_2/env.properties
- echo "age=22" >> /home/jenkins/jenkins/workspace/sh_tst_2/env.properties
. In the downstream job, use the EnvInject Plugin as your first build step, AND
- give Properties File Path - env.properties
. Then choose the second build step as LINUX SHELL
echo $name
echo $age # WILL GIVE U THE OUTPUT OF THE ENV. VAR FROM UPSTREAM JOB

Dynamic Properties file in Jenkins for injecting Env variables

So this is my entire Use case:
I have a parameterized build job which accepts file parameters. After the build I need to send a mail with that file and the size of the file. For this, I'm trying to add the name and size of the file as an Env variable using EnvInject Plugin.
But EnvInject is in the Build Environment step. The file parameter gets stored in the Workspace of the build only in the Build step, not in Build environment. So, there will be an error like File not found.
Due to which, I'm trying a crooked way of defining a properties file somewhere on my local system.I'm mentioning this properties file in "Properties File Path" of Inject Environment variables. In the build step I'm adding FOO=BAR and other values in the file so that I can use those values as my env variables down the line, like when I configure my e-mail template in Post Build Actions.
Can this process be done easily? I was initially making the properties file in JENKINS_HOME. I just got to know that I'm not allowed to do that as in master-agent architecture, JENKINS_HOME will be different and build will fail.
PS-1. The workspace gets deleted after every build
2. Any other plugins which can be used? If possible, please suggest without installing some new plugin as I'm not Jenkins admin
One way of solving this problem is by creating 3 different jobs as follows -
job 1 --> should call below 2 jobs(job 2 & job 3)
job 2:
Build --> Trigger/call builds on other projects --> job 2(block until the triggerred projects finish their builds)
select"Build on the same node" from Add Parameters.
job 3:
Build --> Trigger/call builds on other projects --> job 2(block until the triggerred projects finish their builds)
select"Build on the same node" from Add Parameters.
Job 2 (Create this job as follows):
Execution API --> using "GET_FILE" option you can download the required details on to the current working directory of your job.
Execute shell -->
now within "Execute Shell", download "consoleText" using wget command.
process the "consoleText" using unix command prepare a key-value pairs and store it under /tmp folder. i.e. "/tmp/env.prop"
Job 3 (Create this job as follows):
Bindings:
select "Inject environment variable to the build process" and under 'Properties File Path' enter "/tmp/env.prop"
now you can use the variable which you created in Job2 in the current job without any issue.
please note that it is important to select "Build on the same node" in Job 1, because this will preserve the data and it allows other jobs to access this information.
Let me know if its not clear.

Updating jenkins job variable

Parameterized variable are not getting updated in jenkins
I m using the conditional build-step plugin to update the jenkins job parameter by executing shell script its showing me the new value of variable as well but its not get reflected.
You can try the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file.
Create a property for the email list in the env.properties file:
echo "variable=`value`"> env.properties
It will create the properties file in the job workspace directory.
env.properties
In shell script:
"$variable"
If I understand correctly, you are trying to change the value of a pre-defined parameter
from within a script that is run by the job.
This will not work, because of "scope" (or "call-stack"),
as a process (your script) cannot change the environment of a parent process (your Jenkins job).

Resources