Gilles QUERRET
Gilles QUERRET

Reputation: 583

How can I add job description in Jenkins 2 multi-branch pipeline?

I have a multi-branch pipeline job in Jenkins 2, connected to a GitHub repository (available here). Each pull request in the GitHub repository creates a new "job" in Jenkins, but the job inherits its name from the pull request number (i.e. jobs are called PR-1, PR-2, and so on) which is meaningless in a Jenkins context. Is it possible (and how) to configure the job or Jenkinsfile to add a job description to each pull request ?

Upvotes: 2

Views: 2818

Answers (1)

Gilles QUERRET
Gilles QUERRET

Reputation: 583

Here is how I was able to set the job description from the content of the pull request :

if (env.BRANCH_NAME.startsWith('PR')) {
 def resp = httpRequest url: "https://api.github.com/repos/xxx/yyy/pulls/${env.BRANCH_NAME.substring(3)}"
  def ttl = getTitle(resp)
  def itm = getItem(env.BRANCH_NAME)
  itm.setDisplayName("PR '${ttl}'")
 }
}

@NonCPS
def getItem(branchName) {
  Jenkins.instance.getItemByFullName("sonar-openedge/${branchName}")
}

@NonCPS
def getTitle(json) {
  def slurper = new groovy.json.JsonSlurper()
  def jsonObject = slurper.parseText(json.content)
  jsonObject.title
}

This allows having job description available directly from the job overview page (as in this example: https://ci.rssw.eu/job/sonar-openedge/ )

The full commit and Jenkinsfile are available here: https://github.com/Riverside-Software/sonar-openedge/commit/e2c76ca58b812e4ceac65c406f0b2aae9fbf3f5f

Upvotes: 3

Related Questions