Jenkins | Cookbook

Inhaltsverzeichnis [Anzeigen]

Working with VS Code

Validate Jenkins File

Install VS Code Plugin Jenkins Pipeline Linter Connector

Add configuration in .vscode/settings.json

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"jenkins.pipeline.linter.connector.crumbUrl": "<JENKINS_URL>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,
"jenkins.pipeline.linter.connector.user": "<USERNAME>",
"jenkins.pipeline.linter.connector.pass": "<PASSWORD>",
"jenkins.pipeline.linter.connector.url": "<JENKINS_URL>/pipeline-model-converter/validate",
"jenkins.pipeline.linter.connector.crumbUrl": "<JENKINS_URL>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField, "jenkins.pipeline.linter.connector.user": "<USERNAME>", "jenkins.pipeline.linter.connector.pass": "<PASSWORD>", "jenkins.pipeline.linter.connector.url": "<JENKINS_URL>/pipeline-model-converter/validate",
"jenkins.pipeline.linter.connector.crumbUrl": "<JENKINS_URL>/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,
"jenkins.pipeline.linter.connector.user": "<USERNAME>",
"jenkins.pipeline.linter.connector.pass": "<PASSWORD>",
"jenkins.pipeline.linter.connector.url": "<JENKINS_URL>/pipeline-model-converter/validate",

Replace <USERNAME>, <PASSWORD> and <JENKINS_URL> with your values, for example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"jenkins.pipeline.linter.connector.crumbUrl": "http://localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)",
"jenkins.pipeline.linter.connector.user": "admin",
"jenkins.pipeline.linter.connector.pass": "secret",
"jenkins.pipeline.linter.connector.url": "http://localhost:8080/pipeline-model-converter/validate",
"jenkins.pipeline.linter.connector.crumbUrl": "http://localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)", "jenkins.pipeline.linter.connector.user": "admin", "jenkins.pipeline.linter.connector.pass": "secret", "jenkins.pipeline.linter.connector.url": "http://localhost:8080/pipeline-model-converter/validate",
"jenkins.pipeline.linter.connector.crumbUrl": "http://localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)",
"jenkins.pipeline.linter.connector.user": "admin",
"jenkins.pipeline.linter.connector.pass": "secret",
"jenkins.pipeline.linter.connector.url": "http://localhost:8080/pipeline-model-converter/validate",

Working with Jenkins Client (CLI)

Download Client

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
wget localhost:8080/jnlpJars/jenkins-cli.jar
wget localhost:8080/jnlpJars/jenkins-cli.jar
wget localhost:8080/jnlpJars/jenkins-cli.jar

Working with Plugins

Create aPlugin

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdir SamplePlugin
cd SamplePlugin
mkdir SamplePlugin cd SamplePlugin
mkdir SamplePlugin
cd SamplePlugin
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:"
mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:"
mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:"
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:global-configuration-plugin"
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 kB at 32 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 kB at 44 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (918 B at 18 kB/s)
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:global-configuration-plugin" [INFO] Scanning for projects... Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 kB at 32 kB/s) Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 kB at 44 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (918 B at 18 kB/s) [INFO] [INFO] ------------------< org.apache.maven:standalone-pom >------------------- [INFO] Building Maven Stub Project (No POM) 1 [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:global-configuration-plugin"
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 kB at 32 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 kB at 44 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (918 B at 18 kB/s)
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Choose archetype:
1: remote -> io.jenkins.archetypes:global-configuration-plugin (Skeleton of a Jenkins plugin with a POM and an example piece of global configuration.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Choose io.jenkins.archetypes:global-configuration-plugin version:
1: 1.2
2: 1.3
3: 1.4
4: 1.5
5: 1.6
Choose a number: 5:
[INFO] Using property: groupId = unused
Define value for property 'artifactId': com.examples.jenkins.plugins
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = io.jenkins.plugins.sample
Confirm properties configuration:
groupId: unused
artifactId: com.examples.jenkins.plugins
version: 1.0-SNAPSHOT
package: io.jenkins.plugins.sample
Y: : y
Choose archetype: 1: remote -> io.jenkins.archetypes:global-configuration-plugin (Skeleton of a Jenkins plugin with a POM and an example piece of global configuration.) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1 Choose io.jenkins.archetypes:global-configuration-plugin version: 1: 1.2 2: 1.3 3: 1.4 4: 1.5 5: 1.6 Choose a number: 5: [INFO] Using property: groupId = unused Define value for property 'artifactId': com.examples.jenkins.plugins Define value for property 'version' 1.0-SNAPSHOT: : [INFO] Using property: package = io.jenkins.plugins.sample Confirm properties configuration: groupId: unused artifactId: com.examples.jenkins.plugins version: 1.0-SNAPSHOT package: io.jenkins.plugins.sample Y: : y
Choose archetype:
1: remote -> io.jenkins.archetypes:global-configuration-plugin (Skeleton of a Jenkins plugin with a POM and an example piece of global configuration.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Choose io.jenkins.archetypes:global-configuration-plugin version:
1: 1.2
2: 1.3
3: 1.4
4: 1.5
5: 1.6
Choose a number: 5:
[INFO] Using property: groupId = unused
Define value for property 'artifactId': com.examples.jenkins.plugins
Define value for property 'version' 1.0-SNAPSHOT: :
[INFO] Using property: package = io.jenkins.plugins.sample
Confirm properties configuration:
groupId: unused
artifactId: com.examples.jenkins.plugins
version: 1.0-SNAPSHOT
package: io.jenkins.plugins.sample
 Y: : y
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: global-configuration-plugin:1.6
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: unused
[INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: io.jenkins.plugins.sample
[INFO] Parameter: packageInPathFormat, Value: io/jenkins/plugins/sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: io.jenkins.plugins.sample
[INFO] Parameter: groupId, Value: unused
[INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins
[INFO] Project created from Archetype in dir: /Users/Shared/CLOUD/Kunde.BSH/workspace/SamplePlugin_Config/com.examples.jenkins.plugins
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 45.525 s
[INFO] Finished at: 2020-03-01T17:28:27+01:00
[INFO] ------------------------------------------------------------------------
[INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: global-configuration-plugin:1.6 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: unused [INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: io.jenkins.plugins.sample [INFO] Parameter: packageInPathFormat, Value: io/jenkins/plugins/sample [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: io.jenkins.plugins.sample [INFO] Parameter: groupId, Value: unused [INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins [INFO] Project created from Archetype in dir: /Users/Shared/CLOUD/Kunde.BSH/workspace/SamplePlugin_Config/com.examples.jenkins.plugins [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 45.525 s [INFO] Finished at: 2020-03-01T17:28:27+01:00 [INFO] ------------------------------------------------------------------------
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: global-configuration-plugin:1.6
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: unused
[INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: io.jenkins.plugins.sample
[INFO] Parameter: packageInPathFormat, Value: io/jenkins/plugins/sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: io.jenkins.plugins.sample
[INFO] Parameter: groupId, Value: unused
[INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins
[INFO] Project created from Archetype in dir: /Users/Shared/CLOUD/Kunde.BSH/workspace/SamplePlugin_Config/com.examples.jenkins.plugins
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  45.525 s
[INFO] Finished at: 2020-03-01T17:28:27+01:00
[INFO] ------------------------------------------------------------------------

Verify Plugin

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd com.examples.jenkins.plugins
mvn verify
cd com.examples.jenkins.plugins mvn verify
 cd com.examples.jenkins.plugins
mvn verify

Run Plugin

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mvn hpi:run
mvn hpi:run
mvn hpi:run

Working with Groovy Scripts

Include a common groovy script in Jenkins file

1: Create a common.groovy file with function as needed

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def mycommoncode() {
}
def mycommoncode() { }
def mycommoncode() {
}

2: In the main Jenkinfile load the file and use the function as shown below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
node{
def common = load “common.groovy
common.mycommoncode()
}
node{ def common = load “common.groovy” common.mycommoncode() }
node{ 
   def common = load “common.groovy”
   common.mycommoncode()
}

Basic example of Loading Groovy scripts

File example.groovy

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def example1() {
println 'Hello from example1'
}
def example2() {
println 'Hello from example2'
}
def example1() { println 'Hello from example1' } def example2() { println 'Hello from example2' }
def example1() {
 println 'Hello from example1' 
}
def example2() {
 println 'Hello from example2'
}

The example.groovy script defines example1 and example2 functions before ending with return this. Note that return this is definitely required and one common mistake is to forget ending the Groovy script with it.Jenkinsfile

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def code node('java-agent') {
stage('Checkout') { checkout scm }
stage('Load') { code = load 'example.groovy' }
stage('Execute') { code.example1() }
}
code.example2()
def code node('java-agent') { stage('Checkout') { checkout scm } stage('Load') { code = load 'example.groovy' } stage('Execute') { code.example1() } } code.example2()
def code node('java-agent') { 
    stage('Checkout') { checkout scm } 
    stage('Load') { code = load 'example.groovy' } 
    stage('Execute') { code.example1() }
} 

code.example2()

Processing Github JSON from Groovy

In this demo, we first show how to process JSON response from Github API in Groovy.Processing JSON from Github

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String username = System.getenv('GITHUB_USERNAME')
String password = System.getenv('GITHUB_PASSWORD')
String GITHUB_API = 'https://api.github.com/repos' String repo = 'groovy'
String PR_ID = '2' // Pull request ID
String url = "GITHUBAPI/{username}/repo/pulls/{PR_ID}"
println "Querying ${url}"
def text = url.toURL().getText(requestProperties: ['Authorization': "token ${password}"])
def json = new JsonSlurper().parseText(text)
def bodyText = json.body // Check if Pull Request body has certain text if ( bodyText.find('Safari') ) {
println 'Found Safari user' }
String username = System.getenv('GITHUB_USERNAME') String password = System.getenv('GITHUB_PASSWORD') String GITHUB_API = 'https://api.github.com/repos' String repo = 'groovy' String PR_ID = '2' // Pull request ID String url = "GITHUBAPI/{username}/repo/pulls/{PR_ID}" println "Querying url"deftext=url.toURL().getText(requestProperties:[Authorization:"token{password}"]) def json = new JsonSlurper().parseText(text) def bodyText = json.body // Check if Pull Request body has certain text if ( bodyText.find('Safari') ) { println 'Found Safari user' }
String username = System.getenv('GITHUB_USERNAME') 
String password = System.getenv('GITHUB_PASSWORD') 
String GITHUB_API = 'https://api.github.com/repos' String repo = 'groovy' 
String PR_ID = '2' // Pull request ID 
String url = "${GITHUB_API}/${username}/${repo}/pulls/${PR_ID}" 

println "Querying ${url}" 

def text = url.toURL().getText(requestProperties: ['Authorization': "token ${password}"]) 
def json = new JsonSlurper().parseText(text) 

def bodyText = json.body // Check if Pull Request body has certain text if ( bodyText.find('Safari') ) {
     println 'Found Safari user' }

The equivalent bash command for retrieving JSON response from Github API is as follows:Equivalent bash command

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Groovy formatted string
String cmd = "curl -s -H \"Authorization: token password\"{url}"
// Example String
example = 'curl -s -H "Authorization: token XXX" https://api.github.com/repos/tdongsi/groovy/pulls/2'
// Groovy formatted string String cmd = "curl -s -H \"Authorization: token password\"{url}" // Example String example = 'curl -s -H "Authorization: token XXX" https://api.github.com/repos/tdongsi/groovy/pulls/2'
// Groovy formatted string 
String cmd = "curl -s -H \"Authorization: token ${password}\" ${url}" 
// Example String 
example = 'curl -s -H "Authorization: token XXX" https://api.github.com/repos/tdongsi/groovy/pulls/2'

Processing Github JSON from Jenkinsfile

Continuing the demo from the last section, we now put the Groovy code into a callable function in a script called “github.groovy”. Then, in our Jenkinsfile, we proceed to load the script and use the function to process JSON response from Github API.github.groovy

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import groovy.json.JsonSlurper
def getPrBody(String githubUsername, String githubToken, String repo, String id) {
String GITHUB_API = 'https://api.github.com/repos'
String url = "GITHUBAPI/{githubUsername}/repo/pulls/{id}"
println "Querying ${url}"
def text = url.toURL().getText(requestProperties: ['Authorization': "token ${githubToken}"])
def json = new JsonSlurper().parseText(text)
def bodyText = json.body return bodyText }
return this
import groovy.json.JsonSlurper def getPrBody(String githubUsername, String githubToken, String repo, String id) { String GITHUB_API = 'https://api.github.com/repos' String url = "GITHUBAPI/{githubUsername}/repo/pulls/{id}" println "Querying url"deftext=url.toURL().getText(requestProperties:[Authorization:"token{githubToken}"]) def json = new JsonSlurper().parseText(text) def bodyText = json.body return bodyText } return this
import groovy.json.JsonSlurper
def getPrBody(String githubUsername, String githubToken, String repo, String id) {
   String GITHUB_API = 'https://api.github.com/repos' 
   String url = "${GITHUB_API}/${githubUsername}/${repo}/pulls/${id}" 

   println "Querying ${url}"

def text = url.toURL().getText(requestProperties: ['Authorization': "token ${githubToken}"])
def json = new JsonSlurper().parseText(text) 
def bodyText = json.body return bodyText } 
return this

Jenkinsfile

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def code node('java-agent') {
stage('Checkout') { checkout scm }
stage('Load') { code = load 'github.groovy' }
stage('Execute') {
def code node('java-agent') { stage('Checkout') { checkout scm } stage('Load') { code = load 'github.groovy' } stage('Execute') {
def code node('java-agent') { 
    stage('Checkout') { checkout scm }
    stage('Load') { code = load 'github.groovy' } 
    stage('Execute') {

Best Practice