Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the pipeline name and description from Jenkinsfile

Tags:

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.

like image 415
HarshaB Avatar asked Jul 22 '16 17:07

HarshaB


People also ask

How do I change the pipeline name in Jenkins?

In New Jenkins, Click Rename link present in the left navigation pane, Update the job name and click save. Show activity on this post. Simply change the name in the Pipeline/Project name and hit save.

How do you write a Jenkinsfile pipeline?

To create a simple pipeline from the Jenkins interface, perform the following steps: Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

Can we change Jenkinsfile name?

On the project section of the configuration page you just have to click Add > Pipeline Jenkins and then you can choose the custom name that jenkins will look for the pipeline.

What is the name of pipeline file in Jenkins?

The definition of a Jenkins Pipeline is typically written into a text file (called a Jenkinsfile ) which in turn is checked into a project's source control repository. For more information about Pipeline and what a Jenkinsfile is, refer to the respective Pipeline and Using a Jenkinsfile sections of the User Handbook.

How do I create a pipeline file in Jenkins?

A Jenkinsfile created using the classic UI is stored by Jenkins itself (within the Jenkins home directory). If required, ensure you are logged in to Jenkins. From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click New Item at the top left. In the Enter an item name field, specify the name for your new Pipeline project.

What is a jenkinsfile in Jenkins?

As discussed in the Defining a Pipeline in SCM, a Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Consider the following Pipeline which implements a basic three-stage continuous delivery pipeline.

Why should you use Jenkins pipeline?

You can create multiple automation jobs with the help of use cases, and run them as a Jenkins pipeline. Here are the reasons why you use should use Jenkins pipeline: Jenkins pipeline is implemented as a code which allows multiple users to edit and execute the pipeline process.

How do I use environment variables in Jenkins pipeline?

Using environment variables Jenkins Pipeline exposes environment variables via the global variable env, which is available from anywhere within a Jenkinsfile. The full list of environment variables accessible from within Jenkins Pipeline is documented at $ {YOUR_JENKINS_URL}/pipeline-syntax/globals#env and includes:


3 Answers

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") 
like image 174
Jayan Avatar answered Oct 12 '22 14:10

Jayan


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.

like image 33
Joshua Avatar answered Oct 12 '22 14:10

Joshua


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()
like image 37
Mikalai Parafeniuk Avatar answered Oct 12 '22 14:10

Mikalai Parafeniuk