Operations 4 min read

Jenkinsfile Pipeline Development Tools: Snippet Generator, Declarative Syntax Generator, Global Variables, and Common Pipeline Methods

This guide introduces Jenkinsfile pipeline development utilities, covering a snippet generator, declarative syntax generator, essential global variables, and frequently used pipeline methods such as JSON handling, credential usage, source checkout, HTML reporting, interactive input, build user identification, and HTTP requests.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Jenkinsfile Pipeline Development Tools: Snippet Generator, Declarative Syntax Generator, Global Variables, and Common Pipeline Methods

This chapter focuses on developing Jenkinsfile pipelines using various built‑in tools, aimed at anyone interested in DevOps.

1. Snippet Generator – Allows you to generate pipeline steps based on selected parameters; some steps require installing corresponding plugins.

2. Declarative Syntax Generator – Helps users unfamiliar with declarative syntax to generate Jenkinsfile fragments, which can be directly copied into the pipeline script.

3. Global Variables – Jenkins provides a set of built‑in environment variables useful for email notifications, tagging, and other tasks.

BUILD_NUMBER          // Build number
BUILD_ID              // Build ID
BUILD_DISPLAY_NAME    // Display name of the build
JOB_NAME              // Project name
EXECUTOR_NUMBER       // Executor number
NODE_NAME             // Node name
WORKSPACE             // Workspace directory
JENKINS_HOME          // Jenkins home directory
JENKINS_URL           // Jenkins URL
BUILD_URL             // Build URL
JOB_URL               // Job URL

Commonly used currentBuild variables:

result                // Build result
currentResult         // Current result
displayName           // Build name (e.g., #111)
description           // Build description
duration              // Duration

4. Common Pipeline Methods

JSON Handling

def response = readJSON text: "${scanResult}"
println(scanResult)
import groovy.json.*
@NonCPS
def GetJson(text){
    def prettyJson = JsonOutput.prettyPrint(text)
    new JsonSlurperClassic().parseText(prettyJson)
}

Using Credentials

withCredentials([string(credentialsId: "xxxxx", variable: "sonarToken")]) {
    println(sonarToken)
}

Checkout Code

// Git
checkout([$class: 'GitSCM', branches: [[name: "brnachName"]],
    doGenerateSubmoduleConfigurations: false,
    extensions: [], submoduleCfg: [],
    userRemoteConfigs: [[credentialsId: "${credentialsId}",
    url: "${srcUrl}"]]])
// SVN
checkout([$class: 'SubversionSCM', additionalCredentials: [],
    filterChangelog: false, ignoreDirPropChanges: false,
    locations: [[credentialsId: "${credentialsId}",
    depthOption: 'infinity', ignoreExternalsOption: true,
    remote: "${svnUrl}"]], workspaceUpdater: [$class: 'CheckoutUpdater']])

Publish HTML Report

publishHTML([allowMissing: false,
    alwaysLinkToLastBuild: false,
    keepAll: true,
    reportDir: './report/',
    reportFiles: "a.html, b.html",
    reportName: 'InterfaceTestReport',
    reportTitles: 'HTML'])

Interactive Input

def result = input message: '选择xxxxx',
    ok: '提交',
    parameters: [extendedChoice(
        description: 'xxxxx',
        name: 'failePositiveCases',
        type: 'PT_CHECKBOX',
        value: "1,2,3",
        visibleItemCount: 99)]
println(result)

Build User Information

wrap([$class: 'BuildUser']){
    echo "full name is $BUILD_USER"
    echo "user id is $BUILD_USER_ID"
    echo "user email is $BUILD_USER_EMAIL"
}

Send HTTP Request

ApiUrl = "http://xxxxxx/api/project_branches/list?project=${projectName}"
Result = httpRequest authentication: 'xxxxxxxxx',
    quiet: true,
    contentType: 'APPLICATION_JSON',
    url: "${ApiUrl}"

The article also encourages viewers to watch video tutorials for a faster start and invites them to join the discussion group via QR code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdAutomationDevOpsPipelineGroovyJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.