Jenkins Slave Environment Variable Refresh - jenkins

I have a Jenkins Slave (A Windows PC) which has the JAVA_HOME environment variable. I updated the Java version on the Slave so I also updated the Environment Variable.
When I look at the system properties of this Slave via Jenkins web interface the JAVA_HOME property is reported as the old one.
Running echo %PATH% on the Windows PC shows the correct values.
Running println System.getenv("PATH") on the Jenkins Node script console for the Slave shows the old JAVA_HOME value.
I have tried to delete and add the Slave again and restarted Jenkins server. Still the old value is not updated.
Any thoughts?

This is a bug in Jenkins: https://issues.jenkins-ci.org/browse/JENKINS-27739
3 ways to solve (I have personally confirmed each):
Update Jenkins to >=1.617 (Recommended)
Restart Jenkins master
Delete Jenkins slave, then re-add it

Hudson/Jenkins does not use the slave environment variables as it supports multiple java versions so you configure the java home environment variables in the GUI.
What you need to do is go to Manage Hudson/Jenkins then choose Manage Nodes. Choose the Node you want to configure from the list and then choose Configure. Down the bottom there's a Node Properties section. Check the Tool Locations checkbox. Click on Add then Select your JDK in the dropdown list and add the new path to it.

Restart the slave, this fixed the issue on my configuration.

You can update your Jenkins version, it will solve it. Otherwise this worked for me (Jenkins 1.6, build/test node: MacOSX Mojave):
First install java 8 in your build/test node using the commands:
brew update
brew cask install caskroom/versions/java8
Then in Jenkins web panel click on your build/test node, then click on Configure and then click in the Advanced... button, this will show you multiple text fields. Then in the JavaPath field add the path to your java 8.
For me this path is like:
/Library/Java/JavaVirtualMachines/jdk1.8.X_XXX.jdk/Contents/Home/bin/java
Note: remember replace X_XXX with your java 8 version before.
Then Save.
After that disconnect the build/test node and connect it again. This should solve the problem.
Source

No restart required.
Manage Jenkins -> Manage Nodes and Clouds
Click on the desired slave. Then choose "Disconnect" from the panel.
Slave will auto re-connect. If it doesn't, click on "Launch".
This will refresh environment variables including "PATH".

Related

Jenkins pipeline Workspace got deleted automatically

We have observed that the Jenkins Pipeline Workspace project folder getting deleted. We have not configured any cleanup plugin for this as well as not configured any cleanup module in the pipeline.
This behavior is random and it's deleting Old as well as new jobs.
We can see the workspace deletion traces in /var/lib/jenkins/logs/tasks under Workspace clean-up.log. Please let me know if anybody is facing the same issue and how to fix this issue? Our Jenkins version is 2.289.2
Try disabling the workspace cleanup. There are two ways to achieve this. I was facing the same issue and I've just tried the first approach and monitoring the workspaces to see if this works.
Add -Dhudson.model.WorkspaceCleanupThread.disabled=true to the Jenkins system properties.
If Jenkins is running through terminal:
java -Dhudson.model.WorkspaceCleanupThread.disabled=true -jar jenkins.war
If Jenkins is running as a Linux service:
Stop Jenkins (service jenkins stop). You will need root privileges.
Edit the /etc/defaults/jenkins file.
Add an additional line for the JAVA_ARGS or add to it if it already exists.
JAVA_ARGS="-Dhudson.model.WorkspaceCleanupThread.disabled=true"
Start Jenkins (service jenkins start).
Disable or uninstall the Workspace Cleanup plugin. (I haven't tried this)

How to control the workspace Jenkins starts the build on?

I have one Jenkins master node and 2 Jenkins slave nodes. All my job builds happen in the slave node. When I configured my slaves, I set the Remote root directory as /data/home/jenkins/jenkins-slave. Also, I give the custom workspace option as DEVELOP_BRANCH in the job configuration page of the respective job.
However, at the start of job, I get the following log information:
Building remotely on linux in workspace /data/home/jenkins/jenkins-slave/workspace/DEVELOP_BRANCH
I want to start my builds in this location.
/data/home/jenkins/jenkins-slave/DEVELOP_BRANCH
Why does the extra workspace directory come into the picture? How do I remove it? I do not have access to Jenkins master node. So, if there is a workaround that can match my requirements, it would be awesome.
Note: By node, I refer to a Linux OS computer with redHat distribution.
In project configuration, under Advanced Project Options, you can check Use custom workspace and put a path there.
If you put an absolute path, it will be used without any extra workspace/ directory. (at least that's the behavior I can see on a windows server.)

Override system environment in node config

I am trying to override TMPDIR for builds kicked off on one of my nodes.
I've added a new value in the node properties environment variables:
But when I run my job, TMPDIR is still set to the system value. ZIG does get set, however:
# Output from job running `env`
TMPDIR=/var/folders/nf/p0pz9cv93wz8m314wyk3yp_h0000gn/T/
ZIG=Zag
The same thing happens if I add the environment settings to the Jenkins master config. I can successfully use the EnvInject Plugin to change it on each job, but we have a number of jobs and I would really prefer that it be a node configuration (the node has a dedicated build drive).
How can I override TMPDIR for all builds launched on this node?
I am using Jenkins 1.624 on OS X Yosemite 10.10.5.
I had the EnvInject plugin installed, which was preventing the environment variables from being overridden (even though I wasn't using it, and it didn't work when I turned it on anyway).
I uninstalled that plugin and now my problem is gone.

Defining executables for a slave on Jenkins

I am very new to CI systems like Jenkins.
I have a master Jenkins server running on Ubuntu. I have one Ubuntu slave (managed over SSH) ,which is dedicated to Android builds, having Android SDK and others already available.
My question is how I can tell the Jenkins master the path of android SDK on the slave server?
Go to Manage Jenkins -> Manage Nodes, and open configuration page for the slave node. Add environment variables, which will then be given only to that slave.
If you define environment variables which are used by default by the SDK, then this is enough. Otherwise you will also have to edit the job to use these (I mean, like in execute shell script build steps).
I'm not familiar with the Andriod SDK specifically, but could you pass it as a parameter to the build?

How to move Jenkins from one PC to another

I am currently using Jenkins on my development PC. I installed it on my development PC, because I had limited knowledge on this tool; so I tested on it in my development PC. Now, I feel comfortable with Jenkins as my long term "partner" in the build process and would like to "move" this Jenkins to a dedicated server.
Before this I have done few builds and have the artifacts archived from each build. In particular, the build number is very important to me for version control.
How can I export all the Jenkins information from my current PC to my new server?
Following the Jenkins wiki, you'll have to:
Install a fresh Jenkins instance on the new server
Be sure the old and the new Jenkins instances are stopped
Archive all the content of the JENKINS_HOME of the old Jenkins instance
Extract the archive into the new JENKINS_HOME directory
Do not forget to change the owner of the new Jenkins files : chown -R jenkins:jenkins $JENKINS_HOME
Launch the new Jenkins instance
Do not forget to change documentation/links to your new instance of Jenkins :)
JENKINS_HOME is by default located in ~/.jenkins on a Linux installation, yet to exactly find where it is located, go on the http://your_jenkins_url/configure page and check the value of the first parameter: Home directory; this is the JENKINS_HOME.
In case your JENKINS_HOME directory is too large to copy, and all you need is to set up same jobs, Jenkins Plugins and Jenkins configurations (and don't need old Job artifacts and reports), then you can use the ThinBackup Plugin:
Install ThinBackup on both the source and the target Jenkins servers
Configure the backup directory on both (in Manage Jenkins → ThinBackup → Settings)
On the source Jenkins, go to ThinBackup → Backup Now
Copy from Jenkins source backup directory to the Jenkins target backup directory
On the target Jenkins, go to ThinBackup → Restore, and then restart the Jenkins service.
If some plugins or jobs are missing, copy the backup content directly to the target JENKINS_HOME.
If you had user authentication on the source Jenkins, and now locked out on the target Jenkins, then edit Jenkins config.xml, set <useSecurity> to false, and restart Jenkins.
This worked for me to move from Ubuntu 12.04 (Jenkins ver. 1.628) to Ubuntu 16.04 (Jenkins ver. 1.651.2). I first installed Jenkins from the repositories.
Stop both Jenkins servers
Copy JENKINS_HOME (e.g. /var/lib/jenkins) from the old server to the new one. From a console in the new server:
rsync -av username#old-server-IP:/var/lib/jenkins/ /var/lib/jenkins/
Start your new Jenkins server
You might not need this, but I had to
Manage Jenkins and Reload Configuration from Disk.
Disconnect and connect all the nodes again.
Check that in the Configure System > Jenkins Location, the Jenkins URL is correctly assigned to the new Jenkins server.
Jenkins Server Automation:
Step 1:
Set up a repository to store the Jenkins home (jobs, configurations, plugins, etc.) in a GitLab local or on GitHub private repository and keep it updated regularly by pushing any new changes to Jenkins jobs, plugins, etc.
Step 2:
Configure a Puppet host-group/role for Jenkins that can be used to spin up new Jenkins servers. Do all the basic configuration in a Puppet recipe and make sure it installs the latest version of Jenkins and sets up a separate directory/mount for JENKINS_HOME.
Step 3:
Spin up a new machine using the Jenkins-puppet configuration above. When everything is installed, grab/clone the Jenkins configuration from the Git repository to the Jenkins home direcotry and restart Jenkins.
Step 4:
Go to the Jenkins URL, Manage Jenkins → Manage Plugins and update all the plugins that require an update.
Done
You can use Docker Swarm or Kubernetes to auto-scale the slave nodes.
Sometimes we may not have access to a Jenkins machine to copy a folder directly into another Jenkins instance. So I wrote a menu driven utility which uses Jenkins REST API calls to install plugins and jobs from one Jenkins instance to another.
For plugin migration:
GET request: {SOURCE_JENKINS_SERVER}/pluginManager/api/json?depth=1 will get you the list of plugins installed with their version.
You can send a POST request with the following parameters to install these plugins.
final_url=`{DESTINATION_JENKINS_SERVER}/pluginManager/installNecessaryPlugins`
data=`<jenkins><install plugin="{PLUGIN_NAME}#latest"/></jenkins>` (where, latest will fetch the latest version of the plugin_name)
auth=`(destination_jenkins_username, destination_jenkins_password)`
header=`{crumb_field:crumb_value,"Content-Type":"application/xml”}` (where crumb_field=Jenkins-Crumb and get crumb value using API call {DESTINATION_JENKINS_SERVER}/crumbIssuer/api/json
For job migration:
You can get the list of jobs installed on {SOURCE_JENKINS_URL} using a REST call, {SOURCE_JENKINS_URL}/view/All/api/json
Then you can get each job config.xml file from the jobs on {SOURCE_JENKINS_URL} using the job URL {SOURCE_JENKINS_URL}/job/{JOB_NAME}.
Use this config.xml file to POST the content of the XML file on {DESTINATION_JENKINS_URL} and that will create a job on {DESTINATION_JENKINS_URL}.
I have created a menu-driven utility in Python which asks the user to start plugin or Jenkins migration and uses Jenkins REST API calls to do it.
You can refer the JenkinsMigration.docx from this URL
jenkinsjenkinsmigrationjenkinsrestapi
Let us say we are migrating Jenkins LTS from PC1 to PC2 (irrispective of LTS version is same of upgraded).
It is easy to use ThinBackUp Plugin for migration or Upgrade of Jenkins version.
Step1: Prepare PC1 for migration
Manage Jenkins -> ThinbackUp -> Setting
Select correct options and directory for backup
If you need a job history and artifacts need to be added then please select 'Back build results' option as well.
Go back click on Backup Now.
Note: This Thinbackup will also take Plugin Backup which is optional.
Check the ThinbackUp folder must have a folder with current date and timestamp.
(wait for couple of minutes it might take some time.)
You are ready with your back, .zip it and copy to PARTICULAR (which will be 'Backup directory') directory in PC2.
Unzip ThinbackUp zipped folder.
Stop Jenkins Service in PC1.
Step2: Install Jenkins (Install using .war file or Paste archived version) in PC2.
Create Jenkins Service using command sc create <Jenkins_PC2Servicename> binPath="<Path_to_Jenkinsexe>/jenkins.exe"
Modify JENKINS_HOME/jenkins.xml if needed in PC2.
Run windows service <Jenkins_PC2Servicename> in PC2
Manage Jenkins -> ThinbackUp -> Setting
Make sure that you PARTICULAR path from step1 as Backup Directory in ThinBackup settings.
ThinbackUp -> Restore will give you a Dropdown list, choose a right backup (identify with date and timestamp).
Wait for some minutes and you have latest backup configurations including jobs history and plugins in PC2.
In case if there are additional changes needed in JENKINS_HOME/Jenkins.xml (coming from PC1 ThinbackUp which is not needed) then this modification need to do manually.
NOTE: If you are using Database setting of SCM in your Jenkins jobs then you need to take extra care as all SCM plugins do not support to carry Database settings with the help of ThinbackUp plugin.
e.g. If you are using PTC Integrity SCM Plugin, and some Jenkins jobs are using DB using Integrity, then it will create a directory JENKINS_Home/IntegritySCM, ThinbackUp will not include this DB while taking backup.
Solution: Directly Copy this JENKINS_Home/IntegritySCM folder from PC1 to PC2.

Resources