Is there any way to configure Jenkins without using GUI? - jenkins

I want to setup master and slave relationship in Jenkins without opening the jenkins link.
Is it possible to change the config.xml and form this setup?
The value of slave node will be passed as parameter and will be put in the config file.

If I understand your question correctly, you can try this following solution. Actually my current Test framework is working as CI configured just like you want. What you need to do is:
find suitable Plugin that you can use and support
set your Jenkins jobs to get configurations form your Code project e.g. from VCS
(goto Job Config > Source Code Management > Check-out Strategy must be set to 'as much as possible')
This can be done in following steps:
install Powershell/Batch plugin for Jenkins
prepare update script and put it in your code project
every time you need to configure your Jenkins job, just set properties in your script
update/commit your code project into the VCS
run your Jenkins job
As example you can use this sample batch code as configurable pre-steps:
#echo off
call %WORKSPACE%/jenkins-scripts/set-properties.bat "myRootFolder=/user/project" "rootRefDataFolder=12345" "standartVersion=1.2.3" "alwaysReload=false"
call %WORKSPACE%/jenkins-scripts/do-something-else.bat
And this one as set-properties.bat:
#echo off
echo ----------------------------------------------------------
SET PROP_NAME=base.Project.properties
echo Task: [Update %PROP_NAME%] started
echo Workspace: %WORKSPACE%
SET PROJECT_PATH=projectPath=%WORKSPACE:\=/%
SET RESULT_FOLDER=testResultFolder=report
SET PROP_PATH=%WORKSPACE%\test\resources
DEL "%PROP_PATH%\%PROP_NAME%"
echo %PROJECT_PATH%>> "%PROP_PATH%\%PROP_NAME%"
echo %RESULT_FOLDER%>> "%PROP_PATH%\%PROP_NAME%"
for %%x in (%*) do echo %%~x>> "%PROP_PATH%\%PROP_NAME%"
echo Current properties:
echo __________________________________________________________
type "%PROP_PATH%\%PROP_NAME%"
echo __________________________________________________________
echo Task: [Update %PROP_NAME%] finished
echo ----------------------------------------------------------
Just be sure to set your own paths and variables.

Related

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

Extract user information from the build

Jenkins ver. 2.73.3
I have a sample build task that is triggered by a commit to a Github repository. This is how the build information looks:
We need to write this username to a separate file and store it in a particular location. How can I achieve it?
**********Edit-1**********
Added a build step that executes a shell command to write the variable GIT_COMMITTER_NAME to a file. This fails(empty file) but if I write, say JENKINS_URL, it is written to the file:
I guess the github plugin doesn't set, by default, the variables like GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL etc.
Taking a cue from this answer, I proceeded with using the placeholders of the 'pretty option' of git show command. I added the following command in the 'Execute Shell' build step of Jenkins job:
git show -s --pretty='GIT_AUTHOR_NAME='%aN%n'GIT_AUTHOR_EMAIL='%aE%n'GIT_COMMITTER_NAME='%cN%n'GIT_COMMITTER_EMAIL='%cE >> github.properties
The output:
GIT_AUTHOR_NAME=LastName FirstName
GIT_AUTHOR_EMAIL=FirstName.LastName#company.com
GIT_COMMITTER_NAME=GitHub Enterprise
GIT_COMMITTER_EMAIL=noreply#github.company.com
Instead of echo $variable name execute env in shell, it will give you all environment variables at the time of execution and then you can pick the correct variable. (From Gitlab to Jenkins its $gitlabUserName)

How to get output of 'git describe' into Jenkins build name

What's the best (or any!) way to put the output of a command in a Jenkins build name?
I'd like to put the output of the git describe command into the build name of a Jenkins job.
I've installed the build-name-setter plugin with the hopes of setting the build name to something like:
${ENV, var="GIT_DESCRIBE"}
I just don't know how to set $GIT_DESCRIBE before the name is set! I've tried using the EnvInject plugin. The only way to dynamically set the environment variables is to use groovy script. This would work well, except my job is running on a remote slave and the groovy script is (apparently) running only on the master.
If you need the "describe" data (i.e. you can't just use the existing $GIT_BRANCH or $GIT_COMMIT environment variables), you could add an "Execute shell step" with:
echo GIT_DESCRIBE=$(git describe) > git.properties
Then after that, add an EnvInject build step which injects properties from the git.properties file.
I tried following the steps outlined by Christopher Orr but my "Execute shell" script seemed to only run after the build had started. In my case GIT_DESCRIBE was never set/injected in time for the build to use it.
After some time researching I found a solution using the "Evaluated Groovy script" step as part of the Environment Injector Plugin. The Groovy script is evaluated pre-build. The main caveat there though is that the .groovy script is not run in the $WORKSPACE. What I ended up doing was executing a shell script located in my app ($WORKSPACE) from the .groovy script and returning its output as a map with GIT_DESCRIBE.
Evaluated Groovy script
def sout = new StringBuilder()
def serr = new StringBuilder()
def proc = "$WORKSPACE/git-describe.sh".execute()
proc.waitForProcessOutput(sout, serr)
def map = [GIT_DESCRIBE: sout.toString()]
return map
git-describe.sh
#! /bin/bash
# change working directory to the current script's directory
cd "${0%/*}"
echo `git describe`
From there you should be able to reference GIT_DESCRIBE in your "Build name" macro.
${ENV, var="GIT_DESCRIBE"}
With build-name-setter plugin v1.6.7 one could do the following:
"Execute shell" build step, which does git describe >version.txt
"Update build name" build step, which takes build name from version.txt.

Need to get jenkins build number

In my Jenkins job configure section I am calling an expect script to run.I need to get the current build number value in a variable in the expect script. is it possible to get the build number in a variable?
example. in my Jenkins job i am calling an expect script sample_text.exp in which i need to get current build number in a variable Build_no
is it possible?
yes, it is possible. There is environment variable BUILD_NUMBER. What you mean by "Jenkins job configure section"? In "Execute Windows batch command" in (build section) you can do:
echo %BUILD_NUMBER%
to get this number in a different variable:
set "BUILD_NO=%BUILD_NUMBER%"
echo %BUILD_NO%
Very much possible. You can use the environment variable, BUILD_NUMBER for that in execute shell section. To get a list of all the environment variables that one can use, go to
<your_jenkins_url>/env-vars.html
For example:
http://my-jenkins-box:8080/env-vars.html
Logic that I spoke off in my below comment
if [ -z ${JOB_URL} ]; then
echo "*************************** This is not a Jenkins build ***********"
# Do your stuff here :)
else
echo "**************************** This is a jenkins build so maven project is built before this script is executed via Jenkins ***************************"
fi
Also, remember that if you are running your script with sudo, these environment variables will not be available then

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