The
Jenkins Script Console is a nice way to programmatically change the Jenkins configuration. With help of the
commandline client, such scripts can even be used within a Jenkins job:
- Use the Jenkins GUI to create a new job (lets call it “MyJob")
- Add an “Ant” build step with buildfile “../build.xml”
- Open a OS shell and create the file “build.xml” in directory “$JENKINS_HOME/jobs/MyJob":
<project name="executeCLI" default="run">
<target name="run">
<property environment="env"/>
<java jar="${env.JENKINS_HOME}/war/WEB-INF/jenkins-cli.jar" fork="true" failonerror="true">
<arg line="-s ${env.JENKINS_URL} groovy myscript.groovy"/>
</java>
</target>
</project>
- Copy the Groovy script (lets say “myscript.groovy") into the same directory
…that’s all!
Parameters
Of cource it is possible to pass parameters to the script: Just add them to the “System properties” field of the Jenkins job (e.g.
importance=serious
) as well as to the CLI call in build.xml (
groovy myscript.groovy ${importance}
)
Authentication
If your Jenkins instance requires authentication, the following steps must be performed:
- create a SSH key pair
- copy the SSH private key to file $HOME/.ssh/id_rsa
- create a Jenkins user with “admin” permission
- upload the SSH public key to
http://[JenkinsUrl]/user/[Username]/configure
Note that because of
this bug, the public key authentication only works with Jenkins Version 1.427+.