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:
  1. Use the Jenkins GUI to create a new job (lets call it “MyJob")
  2. Add an “Ant” build step with buildfile “../build.xml”
  3. 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>
  4. 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:
  1. create a SSH key pair
  2. copy the SSH private key to file $HOME/.ssh/id_rsa
  3. create a Jenkins user with “admin” permission
  4. 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+.