Set the pipeline name and description from Jenkinsfile

前端 未结 3 1539
清酒与你
清酒与你 2021-02-12 14:32

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 explicitl

相关标签:
3条回答
  • 2021-02-12 15:10

    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.

    0 讨论(0)
  • 2021-02-12 15:12

    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")
    
    0 讨论(0)
  • 2021-02-12 15:16

    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()
    
    0 讨论(0)
提交回复
热议问题