24

I am trying to do a poc of jenkins pipeline as code. I am using the Github organization folder plugin to scan Github orgs and create jobs per branch. Is there a way to explicitly define the names for the pipeline jobs that get from Jenkinsfile? I also want to add some descriptions for the jobs.

HarshaB
  • 403
  • 2
  • 5
  • 10
  • Could you show the snippets from Jenkinsfile? – Jayan Jul 26 '16 at 00:42
  • 2
    for others looking to change the job/project description rather than the build description, see https://stackoverflow.com/questions/39200948/jenkins-multibranch-change-job-description-from-groovy – Mark Sep 19 '17 at 15:57

3 Answers3

28

You need to use currentBuild like below. The node part is important

node {
    currentBuild.displayName = "$yournamevariable-$another"
    currentBuild.description = "$yourdescriptionvariable-$another"
}

Edit: Above one renames build where as Original question is about renaming jobs. Following script in pipeline will do that(this requires appropriate permissions)

item = Jenkins.instance.getItemByFullName("originalJobName")
item.setDescription("This description was changed by script")
item.save()
item.renameTo("newJobName")
Jayan
  • 18,003
  • 15
  • 89
  • 143
  • 1
    Hi Jayan, Thanks for the reply. I already tried the currentBuild.description option. It showed up as a tag under the build number in Build History section. But I am looking to set these in such a way that they are shown under the "View Configuration" option for the jobs. Is that doable? – HarshaB Jul 25 '16 at 20:11
  • Did not see a way in the plugin options. Above answer updated to use renameTo api(which you may have seen already ) – Jayan Jul 26 '16 at 01:34
  • Thank you very much Jayan. That worked like a charm. Exactly what I wanted. I had to make sure I allow those scripts to run by approving them under Jenkins->In-Process script approval. Is there a way to manually add new classes under approval here? The way I did it now was by selecting approve option for whatever classes I used in this script one at a time. It did not allow me to type any classes manually and approve. – HarshaB Jul 28 '16 at 19:34
  • That should be a new question. Also please accept/upvote : http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jayan Jul 29 '16 at 01:41
  • 8
    The original question was about a _job_, not an individual _build_. – Mark Sep 19 '17 at 15:56
  • The item.save() approach does not work for the dynamically created jobs (such as with BitBucket Branch Source): java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob so there is still no way to set description... – Akom Dec 11 '17 at 16:11
  • I get `WorkflowScript: 8: Expected to find ‘someKey "someValue"’ @ line 8, column 9. script { ^ WorkflowScript: 11: No argument for map key "save" @ line 11, column 8. item.save() ^ WorkflowScript: 8: Invalid config option "script" for agent type "node". Valid config options are [label, customWorkspace] @ line 8, column 2. script { ^` when trying this. – Magick Sep 18 '18 at 21:13
13

I'm late to the party on this one, but this question forced me in the #jenkins chat where I spent most of my day today. I would like to thank @tang^ from that chat for helping solve this in a graceful way for my situation.

To set the JOB description and JOB display name for a child in a multi-branch DECLARATIVE pipeline use the following steps block in a stage:

steps {
    script {
        if(currentBuild.rawBuild.project.displayName != 'jobName') {
            currentBuild.rawBuild.project.description = 'NEW JOB DESCRIPTION'
            currentBuild.rawBuild.project.setDisplayName('NEW JOB DISPLAY NAME')
        }
        else {
            echo 'Name change not required'
        }
    }
}

This will require that you approve the individual script calls through the Jenkins sandbox approval method, but it was far simpler than anything else I'd found across the web about renaming the actual children of the parent pipeline. The last thing to note is that this should work in a Jenkinsfile where you can use the environment variables to manipulate the job items being set.

Joshua
  • 153
  • 1
  • 7
  • 2
    `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild` – kyb Apr 05 '18 at 13:13
  • "This will require that you approve the individual script calls through the Jenkins sandbox approval method" Given the access required to modify the job, you will need to approve several classes to execute outside the sandbox. – Joshua Apr 05 '18 at 16:47
7

I tried to used code snippet from accepted answer to describe my Jenkins pipeline in Jenkinsfile. I had to wrap code snippet into function with @NonCPS annotation and use def for item variable. I have placed code snippet in root of Jenkinsfile, not in node section.

@NonCPS 
def setDescription() { 
    def item = Jenkins.instance.getItemByFullName(env.JOB_NAME) 
    item.setDescription("Some description.") 
    item.save()
}

setDescription()
Mikalai Parafeniuk
  • 1,328
  • 15
  • 10
  • `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance` – kyb Apr 05 '18 at 13:18
  • 1
    @kyb, just go to Jenkins->Manage Jenkins->In-Process Script Approval, you should notice mentioned signature pending approval. Note, that it is considered dangerous. – Mikalai Parafeniuk Apr 05 '18 at 14:41