Rajesh Kumar (Rajesh Kumar), Github (scmGalaxy)
2016-02-07
wget http://www.fi.muni.cz/~xcupak/download/hacking-jenkins-offline.zip # or copy from USB keys
unzip hacking-jenkins-offline.zip
cd hacking-jenkins-offline
./start.sh # Jenkins should be available a http://localhost:8080/
Use REST API to trigger build using curl
.
curl -v -X POST http://localhost:8080/job/example_job/build
Update number of executors on given node via REST API.
curl http://localhost:8080/computer/example_slave/config.xml > example_slave.xml
vi example_slave.xml
curl -X POST http://localhost:8080/computer/example_slave/config.xml -d @example_slave.xml
java -jar jenkins-cli.jar -s $JENKINS_URL help
Jenkins master does not respond through UI. It needs to be shut down before whole machine can be restarted.
Use Jenkins CLI to safely shutdown master.
safe-shutdown
commandjava -jar jenkins-cli.jar -s http://localhost:8080 safe-shutdown
Create copy of existing node using nothing but Jenkins CLI.
get-node
and create-node
.
java -jar jenkins-cli.jar -s http://localhost:8080 get-node example_slave > example_slave.xml
java -jar jenkins-cli.jar -s http://localhost:8080 create-node new_slave < example_slave.xml
java -jar jenkins-cli.jar -s http://jenkins/ groovysh
curl -d "script=myscript.groovy" http://jenkins/script
Our build is failing upon initialization. We suspect the machine running the slave does not have enough free memory.
Use example_slave's script console to find out how much free RAM it has.
free
shell command should do the trick - can you run it from Groovy?
println "free -m".execute().text
Labels are basically the only way to distinguish slaves in Jenkins. A good idea is to use feature labels, i.e. tag slaves according to what they provide (e.g. "rhel7 32b mem16g").
Use the script console to mark all Linux slaves as development machines, i.e. add "dev" label to all slaves with "linux" label.
Jenkins.instance
jenkins.model.Jenkins
to find out how to obtain the list of slaves
Jenkins.instance.getLabel("linux").nodes.each {
it.labelString += " dev"
}
j.save()
groovy
command with a script
We're running a small Jenkins instance with a small number of slaves. Occasionally, slaves crash or need to undergo maintenance. We want to make sure that we have at least one online slave at any given time.
Create a job monitoring the number of active slaves. The job should run on master every hour and send an email to you whenever there are no online slaves.
hudson.model.Computer
import jenkins.model.*
import hudson.model.*
import jenkins.model.*
import hudson.model.*
def online = 0
for(computer in Jenkins.instance.computers){
if(computer.isOnline()){
online++
}
}
return !(online<1)
Feedback: http://devconf.cz/f/152.
Slides: http://mcupak.github.io/hacking-jenkins-workshop/.