Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the trigger information in Jenkins programmatically

I need to add the next build time scheduled in a build email notification after a build in Jenkins.

The trigger can be "Build periodically" or "Poll SCM", or anything with schedule time.

I know the trigger info is in the config.xml file e.g.

<triggers>
    <hudson.triggers.SCMTrigger>
      <spec>8 */2 * * 1-5</spec>
      <ignorePostCommitHooks>false</ignorePostCommitHooks>
    </hudson.triggers.SCMTrigger>
  </triggers>

and I also know how to get the trigger type and spec with custom scripting from the config.xml file, and calculate the next build time.

I wonder if Jenkins has the API to expose this information out-of-the-box. I have done the search, but not found anything.

like image 988
Heinz Avatar asked May 11 '17 14:05

Heinz


People also ask

How do you trigger builds remotely eg from scripts?

Create a remote Jenkins build trigger in three stepsCreate a Jenkins build job and enable the Trigger builds remotely checkbox. Provide an authentication token; This can be any text string of your choice. Invoke the Jenkins build URL to remotely trigger the build job.

Which command is used to trigger build remotely in Jenkins?

Configure a job to trigger from remote in Jenkins You can create a new FreeStyle job or you can use the previous one. Move to configuration -> Build Triggers sections and check the “Trigger builds remotely(e.g., from scripts)” option and paste the token name there.


1 Answers

I realise you probably no longer need help with this, but I just had to solve the same problem, so here is a script you can use in the Jenkins console to output all trigger configurations:

#!groovy

Jenkins.instance.getAllItems().each { it ->
  if (!(it instanceof jenkins.triggers.SCMTriggerItem)) {
    return
  }

  def itTrigger = (jenkins.triggers.SCMTriggerItem)it
  def triggers = itTrigger.getSCMTrigger()

  println("Job ${it.name}:")

  triggers.each { t->
    println("\t${t.getSpec()}")
    println("\t${t.isIgnorePostCommitHooks()}")
  }
}

This will output all your jobs that use SCM configuration, along with their specification (cron-like expression regarding when to run) and whether post-commit hooks are set to be ignored.

You can modify this script to get the data as JSON like this:

#!groovy
import groovy.json.*

def result = [:]

Jenkins.instance.getAllItems().each { it ->
  if (!(it instanceof jenkins.triggers.SCMTriggerItem)) {
    return
  }

  def itTrigger = (jenkins.triggers.SCMTriggerItem)it
  def triggers = itTrigger.getSCMTrigger()

  triggers.each { t->
    def builder = new JsonBuilder()
    result[it.name] = builder {
      spec "${t.getSpec()}"
      ignorePostCommitHooks "${t.isIgnorePostCommitHooks()}"
    }
  }
}

return new JsonBuilder(result).toPrettyString()

And then you can use the Jenkins Script Console web API to get this from an HTTP client.

For example, in curl, you can do this by saving your script as a text file and then running:

curl --data-urlencode "script=$(<./script.groovy)" <YOUR SERVER>/scriptText

If Jenkins is using basic authentication, you can supply that with the -u <USERNAME>:<PASSWORD> argument.

Ultimately, the request will result in something like this:

{
    "Build Project 1": {
        "spec": "H/30 * * * *",
        "ignorePostCommitHooks": "false"
    },
    "Test Something": {
        "spec": "@hourly",
        "ignorePostCommitHooks": "false"
    },
    "Deploy ABC": {
        "spec": "H/20 * * * *",
        "ignorePostCommitHooks": "false"
    }
}

You should be able to tailor these examples to fit your specific use case. It seems you won't need to access this remotely but just from a job, but I also included the remoting part as it might come in handy for someone else.

like image 149
JohnSomeone Avatar answered Sep 19 '22 22:09

JohnSomeone