{"id":6179,"date":"2020-03-02T07:29:23","date_gmt":"2020-03-02T06:29:23","guid":{"rendered":"https:\/\/blog.via-internet.de\/?p=6179"},"modified":"2020-03-02T07:29:23","modified_gmt":"2020-03-02T06:29:23","slug":"jenkins-cookbook-2","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2020\/03\/02\/jenkins-cookbook-2\/","title":{"rendered":"Jenkins | Cookbook"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Working with VS Code<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Validate Jenkins File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Install VS Code Plugin  <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=janjoerke.jenkins-pipeline-linter-connector\">Jenkins Pipeline Linter Connector<\/a> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add configuration in <code>.vscode\/settings.json<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"jenkins.pipeline.linter.connector.crumbUrl\": \"&lt;JENKINS_URL>\/crumbIssuer\/api\/xml?xpath=concat(\/\/crumbRequestField,\n\"jenkins.pipeline.linter.connector.user\": \"&lt;USERNAME>\",\n\"jenkins.pipeline.linter.connector.pass\": \"&lt;PASSWORD>\",\n\"jenkins.pipeline.linter.connector.url\": \"&lt;JENKINS_URL>\/pipeline-model-converter\/validate\",<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace <code>&lt;USERNAME&gt;<\/code>, <code>&lt;PASSWORD&gt;<\/code> and <code>&lt;JENKINS_URL&gt;<\/code> with your values, for example<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"jenkins.pipeline.linter.connector.crumbUrl\": \"http:\/\/localhost:8080\/crumbIssuer\/api\/xml?xpath=concat(\/\/crumbRequestField,%22:%22,\/\/crumb)\",\n\"jenkins.pipeline.linter.connector.user\": \"admin\",\n\"jenkins.pipeline.linter.connector.pass\": \"secret\",\n\"jenkins.pipeline.linter.connector.url\": \"http:\/\/localhost:8080\/pipeline-model-converter\/validate\",<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Working with Jenkins Client (CLI)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Download Client<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">wget localhost:8080\/jnlpJars\/jenkins-cli.jar<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with Plugins<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create aPlugin<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir SamplePlugin\ncd SamplePlugin<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mvn -U archetype:generate -Dfilter=\"io.jenkins.archetypes:\"<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"1\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mvn -U archetype:generate -Dfilter=\"io.jenkins.archetypes:global-configuration-plugin\"\n[INFO] Scanning for projects...\nDownloading from central: https:\/\/repo.maven.apache.org\/maven2\/org\/apache\/maven\/plugins\/maven-metadata.xml\nDownloading from central: https:\/\/repo.maven.apache.org\/maven2\/org\/codehaus\/mojo\/maven-metadata.xml\nDownloaded from central: https:\/\/repo.maven.apache.org\/maven2\/org\/apache\/maven\/plugins\/maven-metadata.xml (14 kB at 32 kB\/s)\nDownloaded from central: https:\/\/repo.maven.apache.org\/maven2\/org\/codehaus\/mojo\/maven-metadata.xml (20 kB at 44 kB\/s)\nDownloading from central: https:\/\/repo.maven.apache.org\/maven2\/org\/apache\/maven\/plugins\/maven-archetype-plugin\/maven-metadata.xml\nDownloaded from central: https:\/\/repo.maven.apache.org\/maven2\/org\/apache\/maven\/plugins\/maven-archetype-plugin\/maven-metadata.xml (918 B at 18 kB\/s)\n[INFO]\n[INFO] ------------------&lt; org.apache.maven:standalone-pom >-------------------\n[INFO] Building Maven Stub Project (No POM) 1\n[INFO] --------------------------------[ pom ]---------------------------------\n[INFO]\n[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>>\n[INFO]\n[INFO] &lt;&lt;&lt; maven-archetype-plugin:3.1.2:generate (default-cli) &lt; generate-sources @ standalone-pom &lt;&lt;&lt;\n[INFO]\n[INFO]\n[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom ---\n[INFO] Generating project in Interactive mode\n[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2,10,12\" data-enlighter-linenumbers=\"false\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Choose archetype:\n1: remote -> io.jenkins.archetypes:global-configuration-plugin (Skeleton of a Jenkins plugin with a POM and an example piece of global configuration.)\nChoose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1\nChoose io.jenkins.archetypes:global-configuration-plugin version:\n1: 1.2\n2: 1.3\n3: 1.4\n4: 1.5\n5: 1.6\nChoose a number: 5:\n[INFO] Using property: groupId = unused\nDefine value for property 'artifactId': com.examples.jenkins.plugins\nDefine value for property 'version' 1.0-SNAPSHOT: :\n[INFO] Using property: package = io.jenkins.plugins.sample\nConfirm properties configuration:\ngroupId: unused\nartifactId: com.examples.jenkins.plugins\nversion: 1.0-SNAPSHOT\npackage: io.jenkins.plugins.sample\n Y: : y<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[INFO] ----------------------------------------------------------------------------\n[INFO] Using following parameters for creating project from Archetype: global-configuration-plugin:1.6\n[INFO] ----------------------------------------------------------------------------\n[INFO] Parameter: groupId, Value: unused\n[INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins\n[INFO] Parameter: version, Value: 1.0-SNAPSHOT\n[INFO] Parameter: package, Value: io.jenkins.plugins.sample\n[INFO] Parameter: packageInPathFormat, Value: io\/jenkins\/plugins\/sample\n[INFO] Parameter: version, Value: 1.0-SNAPSHOT\n[INFO] Parameter: package, Value: io.jenkins.plugins.sample\n[INFO] Parameter: groupId, Value: unused\n[INFO] Parameter: artifactId, Value: com.examples.jenkins.plugins\n[INFO] Project created from Archetype in dir: \/Users\/Shared\/CLOUD\/Kunde.BSH\/workspace\/SamplePlugin_Config\/com.examples.jenkins.plugins\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n[INFO] Total time:  45.525 s\n[INFO] Finished at: 2020-03-01T17:28:27+01:00\n[INFO] ------------------------------------------------------------------------<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Verify Plugin<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> cd com.examples.jenkins.plugins\nmvn verify<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Run Plugin<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mvn hpi:run<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Working with Groovy Scripts<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Include a common groovy script in Jenkins file<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">1: Create a common.groovy file with function as needed<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def mycommoncode() {\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2: In the main Jenkinfile load the file and use the function as shown below<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">node{ \n   def common = load \u201ccommon.groovy\u201d\n   common.mycommoncode()\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Basic example of Loading Groovy scripts<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">File example.groovy<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def example1() {\n println 'Hello from example1' \n}\ndef example2() {\n println 'Hello from example2'\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>example.groovy<\/code>&nbsp;script defines&nbsp;<code>example1<\/code>&nbsp;and&nbsp;<code>example2<\/code>&nbsp;functions before ending with&nbsp;<code>return this<\/code>. Note that&nbsp;<code>return this<\/code>&nbsp;is definitely required and one common mistake is to forget ending the Groovy script with it.Jenkinsfile<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def code node('java-agent') { \n    stage('Checkout') { checkout scm } \n    stage('Load') { code = load 'example.groovy' } \n    stage('Execute') { code.example1() }\n} \n\ncode.example2()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Processing Github JSON from Groovy<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this demo, we first show how to process JSON response from Github API in Groovy.Processing JSON from Github<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">String username = System.getenv('GITHUB_USERNAME') \nString password = System.getenv('GITHUB_PASSWORD') \nString GITHUB_API = 'https:\/\/api.github.com\/repos' String repo = 'groovy' \nString PR_ID = '2' \/\/ Pull request ID \nString url = \"${GITHUB_API}\/${username}\/${repo}\/pulls\/${PR_ID}\" \n\nprintln \"Querying ${url}\" \n\ndef text = url.toURL().getText(requestProperties: ['Authorization': \"token ${password}\"]) \ndef json = new JsonSlurper().parseText(text) \n\ndef bodyText = json.body \/\/ Check if Pull Request body has certain text if ( bodyText.find('Safari') ) {\n     println 'Found Safari user' }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The equivalent bash command for retrieving JSON response from Github API is as follows:Equivalent bash command<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Groovy formatted string \nString cmd = \"curl -s -H \\\"Authorization: token ${password}\\\" ${url}\" \n\/\/ Example String \nexample = 'curl -s -H \"Authorization: token XXX\" https:\/\/api.github.com\/repos\/tdongsi\/groovy\/pulls\/2'<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Processing Github JSON from Jenkinsfile<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Continuing the demo from the last section, we now put the Groovy code into a callable function in a script called \u201cgithub.groovy\u201d. Then, in our Jenkinsfile, we proceed to load the script and use the function to process JSON response from Github API.github.groovy<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import groovy.json.JsonSlurper\ndef getPrBody(String githubUsername, String githubToken, String repo, String id) {\n   String GITHUB_API = 'https:\/\/api.github.com\/repos' \n   String url = \"${GITHUB_API}\/${githubUsername}\/${repo}\/pulls\/${id}\" \n\n   println \"Querying ${url}\"\n\ndef text = url.toURL().getText(requestProperties: ['Authorization': \"token ${githubToken}\"])\ndef json = new JsonSlurper().parseText(text) \ndef bodyText = json.body return bodyText } \nreturn this<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Jenkinsfile<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def code node('java-agent') { \n    stage('Checkout') { checkout scm }\n    stage('Load') { code = load 'github.groovy' } \n    stage('Execute') {<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practice<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Working with VS Code Validate Jenkins File Install VS Code Plugin Jenkins Pipeline Linter Connector Add configuration in .vscode\/settings.json Replace &lt;USERNAME&gt;, &lt;PASSWORD&gt; and &lt;JENKINS_URL&gt; with your values, for example Working with Jenkins Client (CLI) Download Client Working with Plugins Create aPlugin Verify Plugin Run Plugin Working with Groovy Scripts Include a common groovy script in Jenkins file 1: Create a common.groovy file with function as needed 2: In the main Jenkinfile load the file and use the function as shown below Basic example of Loading Groovy scripts File example.groovy The&nbsp;example.groovy&nbsp;script defines&nbsp;example1&nbsp;and&nbsp;example2&nbsp;functions before ending with&nbsp;return this. Note that&nbsp;return this&nbsp;is definitely required and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,16,40],"tags":[],"class_list":["post-6179","post","type-post","status-publish","format-standard","hentry","category-ci-cd","category-cookbook","category-jenkins"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/6179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=6179"}],"version-history":[{"count":0,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/6179\/revisions"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=6179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=6179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=6179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}