Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins scripted or declarative pipeline - how to set job description text

Does anyone know how to set the description text for a job defined in scripted or declarative pipeline in Jenkins ? Reason: I want to add some meaningful text (small documentation) about the job.

like image 969
Costas Avatar asked Mar 09 '19 17:03

Costas


People also ask

What's the difference between a Declarative and scripted Jenkins pipeline?

Declarative pipelines break down stages into individual stages that can contain multiple steps. Scripted pipelines use Groovy code and references to the Jenkins pipeline DSL within the stage elements without the need for steps.

How do you use parameters in Jenkins Declarative pipeline?

You just have to use params. [NAME] in places where you need to substitute the parameter. Here is an example of a stage that will be executed based on the condition that we get from the choice parameter. The parameter name is ENVIRONMENT , and we access it in the stage as params.

How is the Declarative pipeline written?

Hi, The Declarative pipeline is a new feature that is added to create the pipeline. This is basically written in a Jenkinsfile which can be stored into a source code management system such as Git.

What is declarative pipeline in Jenkins?

In pipeline as code technique, jobs are created using a script file that contains the steps to be executed by the job. In Jenkins, that scripted file is called Jenkinsfile. In this Jenkins tutorial, we will deep dive into Jenkins Declarative Pipeline with the help of Jenkins declarative pipeline examples.

How to create a scripted pipeline in Jenkins?

Jenkins Scripted Pipeline corresponds to the option “Pipeline Script” when selecting Pipeline Definition. You write your script in text field in job configuration, or copy/paste your script to it. It is a rather easier way to try your first pipeline.

What is the best way to create a Jenkins job?

Jenkins declarative pipeline should be the preferred way to create a Jenkins job as they offer a rich set of features, come with less learning curve & no prerequisite to learn a programming language like Groovy just for the sake of writing pipeline code. We can also validate the syntax of the Declarative pipeline code before running the job.

How do devops engineers and developers interact with Jenkins?

The majority of DevOps Engineers and Developers interact with Jenkins in their day-to-day activities. Jenkins is an automation tool that helps developers and DevOps to build, test and deploy applications. Nowadays, people prefer to use Jenkins pipelines to set up build, test, and deployment workflow over freestyle jobs.


2 Answers

This solution changes the description of a job item via a groovy script in jenkins. I only used it in a freestyle job, but i think it should be also working in an scripted pipeline:

import jenkins.model.*
final jenkins   = Jenkins.getInstanceOrNull()
final myJob     = jenkins.getItem("MyJobName")

description = "<h1 style=\"color:green\">The newest build has number ${env.BUILD_NUMBER}</h1>"
myJob.setDescription(description)
myJob.save()

This solution changes the description of a build: Use 'currentbuild' global variable.

I.e. declarative pipeline:

script { currentbuild.description = 'New Description' }

Works the same in scripted pipelines :)

Reference: https://opensource.triology.de/jenkins/pipeline-syntax/globals

like image 62
snukone Avatar answered Oct 20 '22 01:10

snukone


for declarative pipeline

script {
       currentBuild.description = "description env var if required :${env.ver}"
}

for scripted

currentBuild.description = "description env var if required :${env.ver}"
like image 1
Mor Lajb Avatar answered Oct 20 '22 01:10

Mor Lajb