Jenkins is an automation server. It allows for all kinds of automations. It is primarily used for Build automation, Continuous Integration, and Continuous Deployment.

  1. Install Jenkins on Ubuntu (using Vagrant)
  2. Vagrant for Jenkins on Ubuntu
  3. Jenkins Pipeline - Hello World
  4. Jenkins Pipeline: running external programs with sh or bat, returnStdout, trim
  5. Jenkins Pipeline: Send e-mail notifications
  6. Jenkins Pipeline: Add some text to the job using shortText
  7. Jenkins CLI: create node
  8. Jenkins Pipeline - set and use environment variables
  9. Triggers from Version Control Systems
  10. Report failures.
  11. Send alerts
  12. Collect test coverage data.
  13. Jenkins Pipeline BuildUser plugin
  14. Jenkins slides

Add badges

   manager.addBadge("error.gif", "Failed build")

Run external code, capture output

script {
      v = sh(script: 'echo " 42"; echo', returnStdout: true).trim()
      echo v
      echo "a${v}b"
}

bat for windows.

catch and print error in jenkins

pipeline {
   agent none
   stages {
       stage ('Catch crash') {
           agent { label 'master'}
           steps {
               echo "before crash"
               script {
                   try {
                       sh 'exit 1'
                   } catch (err) {
                       echo "exception caught, going on"
                       println err // java.lang.ClassCastException:
org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received
class hudson.AbortException
                   }
               }
               echo "after crash"
           }
       }
       stage ('Continue after crash') {
           agent { label 'master'}
           steps {
               echo "stage after crash"
           }
       }
   }
}

dir and tmp are problematic

  stages {
       stage ('Run external exe') {
           agent { label 'master'}
           steps {
               sh 'pwd'
               dir('/tmp/gabor') {
                   echo "inside"
                   sh 'pwd'
                   //sh 'sudo ./do-something.py'
               }
               sh 'pwd'
               //sh "sudo sh -c 'cd /tmp; ./do-something.py; cd -'"
           }
       }

java.io.IOException: Failed to mkdirs: /tmp@tmp/durable-e569697c
        at hudson.FilePath.mkdirs(FilePath.java:1170)
        at
org.jenkinsci.plugins.durabletask.FileMonitoringTask$FileMonitoringController.<init>(FileMonitori
ngTask.java:156)
        at
org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.<init>(BourneShellScript.java
:198)
        at
org.jenkinsci.plugins.durabletask.BourneShellScript$ShellController.<init>(BourneShellScript.java
:190)
        at
org.jenkinsci.plugins.durabletask.BourneShellScript.launchWithCookie(BourneShellScript.java:111)
        at
org.jenkinsci.plugins.durabletask.FileMonitoringTask.launch(FileMonitoringTask.java:86)
        at
org.jenkinsci.plugins.workflow.steps.durable_task.DurableTaskStep$Execution.start(DurableTaskStep
.java:182)
        at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:229)
        at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153)
        at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
        at sun.reflect.GeneratedMethodAccessor1989.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
        at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
        at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterc
eptor.java:133)
        at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:129)
        at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
        at WorkflowScript.run(WorkflowScript:16)

Jenkins / Groovy - define functions and call them with parameters

def report(status) {
   println "status=${status}"
}

and call them

report("text")

Environment variables on Linux

sh 'printenv'
sh 'env'

Input during the process

pipeline {
   agent { label 'master' }
   stages {
       stage('build') {
           steps {
               echo "Hello World!"
               //input("Continue?")
               echo "OK"
               //input(
               //    message: 'Was this successful?', parameters: [
               //   [$class: 'BooleanParameterDefinition', defaultValue: true, description: '',
name: 'Please confirm you agree with this']
               //])

       /*
               script {
                   res = input(
                       message: 'Was this successful?', parameters: [
                       [$class: 'BooleanParameterDefinition', defaultValue: false, description:
'', name: 'Apple'],
                       [$class: 'BooleanParameterDefinition', defaultValue: false, description:
'', name: 'Banana']
                   ])
                                      print(res)
               }
*/
               script {
                   values = ['Apple', 'Banana', 'Peach']
                   parameters = []
                   values.each {
                       echo it
                       parameters.add( [$class: 'BooleanParameterDefinition', defaultValue:
false, description: '', name: it ] )
                   }

                   res = input(
                       message: 'Was this successful?', parameters: parameters
                   )
                                                         print(res)
               }


               //                input(
//                    message: 'What now?', parameters: [
//                        [$class: 'AppDetectorParamaterDefinition')
//                ])
                                  //echo result
                              // python scripts/aws_instances.py --what selftest --command start
           }
       }
   }
}

git Backup

gist

List agents by name and by label

def jenkins = Jenkins.instance
def computers = jenkins.computers
computers.each {
   //  println "${it.displayName} ${it.hostName}"
}

def labels = jenkins.getLabels()
labels.each {
   println "${it.displayName}"
}

Other

echo bat(returnStdout: true, script: 'set')

build(job: 'RevertServerAutomationCloud', parameters: [
     string(name: 'VM_SNAPSHOT', value: 'CleanDb')
])

how to include one jenkinsfile in another one?

how to avoid repetititon?

stage('Revert agent 100')
         {
             steps
                     {
                     }
         }

 stage('Revert agent 102')
         {
             steps
                     {

                     }
         }

how do try - catch and repeat interact?

vSphere buildStep: [$class: 'RevertToSnapshot', snapshotName: "${params.VM_SNAPSHOT}", vm: "${params.VM_NAME}"], serverName: '192.168.1.1'

httpRequest authentication: 'df8-b86d-3272', consoleLogResponseBody: true, httpMode: 'POST', ignoreSslErrors: true, responseHandle: 'NONE', url:
"http://192.168.1.1:8080/computer/${params.AGENT_NAME}/doDisconnect?offlineMessage=bye", validResponseCodes: '100:404'

Active Choices Parameter

try {
   List<String> subnets = new ArrayList<String>()
   def subnetsRaw = "gcloud compute networks subnets list --project=${GCE_PROJECT} --network=corp-development --format=(NAME)".execute().text
   for (subnet in  subnetsRaw.split()) {
       subnets.add(subnet)
   }
   return subnets
} catch (Exception e) {
   print e
   print "There was a problem fetching the artifacts"
}

Options

   options {
       ansiColor('xterm')
       timestamps()
   }

Scripts

Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.jenkinsci.plugins.workflow.cps.EnvActionImpl keys). Administrators can decide whether to approve or reject this signature.

archiveArtifacts can be called multiple times

archiveArtifacts artifacts: 'mydata.json', onlyIfSuccessful: true
writeJSON(file: 'otherfile.log', json: data, pretty: 4)
archiveArtifacts artifacts: '*.log', onlyIfSuccessful: true

Environment variable values must either be single quoted, double quoted, or function calls.

They cannot be earlier defined environment variables or parameter values. We can however overcome this limitation by calling a function and passing the values to it.

pipeline { agent none options { ansiColor('xterm') timestamps() } parameters { string(name: 'machine', defaultValue: 'asos', description: 'Some text input') string(name: 'size', defaultValue: '23', description: 'Some number input') } environment { answer = 42 // machine_name = params.machine // -> Environment variable values must either be single quoted, double quoted, or function calls. machine_name = set_machine_name(params.machine) } stages { stage('try') { agent { label 'master' } steps { script { sh "hostname" print("params.machine=${params.machine}") print("params.size=${params.size}") print("env.answer=${env.answer}") print("env.machine_name=${env.machine_name}")

} } } stage('try-agent') { agent { label 'jenkins-simple-slave' } steps { script { sh "hostname" } } } } }

def set_machine_name(value) { return value }

Jenkins environment

Even if there is an exception in the environment section Jenkins will still run the "success" part of the post section. Same problem if there is an exception on one of the functions.

To overcome this we create an environment variable as the last step in the environment section and then we check that variable using

if (! binding.hasVariable('environment_is_set')) { error("Environment failed to set properly") }

That does not help in case there is an exception in the functions.

http_request

response = httpRequest discovery_url
println response
config_str = response.getContent()
for (item in config.sources) {
  item.value
  item.key

Sending e-mail problem fixed

https://stackoverflow.com/questions/20188456/how-to-change-the-security-type-from-ssl-to-tls-in-jenkins

* Sending e-mail In Manage Jenkins -> Configure System in the Extended mail section set the SMTP: smtp.office365.com Domain name: @company.com Advanced: Use SMTP Authentication: + User Name: cicdserver@company.com Password: SMTP port: 587 E-mail notification section: SMTP server: smtp.office365.com Default user e-mail suffix: @company.com Advanced User Name: cicdserver@company.com Password: SMTP port: 587

Shut down Jenkins (via the Windows services) Open the file: C:\Program Files (x86)\Jenkins\jenkins.xml and change the arguments line to be: -Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -Dmail.smtp.starttls.enable=true -jar "%BASE%\jenkins.war" --httpPort=8080 --webroot="%BASE%\war" (specifically add: -Dmail.smtp.starttls.enable=true ) Then start Jenkins again.

Client was not authenticated to send anonymous mail Error sending to the following VALID addresses

examples/jenkins/variables.Jenkinsfile

pipeline {
   agent none
   stages {
       stage('try') {
           agent { label 'master' }
           steps {
               script {
                   //some_strange_name = null
                   //print(some_strange_name)
                   if (true) {
                       print("creating variable")
                       some_strange_name = 1
                   }
                   print(some_strange_name)

                   if (binding.hasVariable('some_strange_name')) {
                       print("has some_strange_name")
                       print(some_strange_name)
                   } else {
                       print("DOES NOT have some_strange_name")
                   }
               }
           }
       }
       stage('try again') {
           agent { label 'master' }
           steps {
               script {
                   if (binding.hasVariable('some_strange_name')) {
                       print("has some_strange_name")
                       print(some_strange_name)
                   } else {
                       print("DOES NOT have some_strange_name")
                   }
               }
           }
       }
   }
}

Parallel stages

examples/jenkins/parallel.Jenkinsfile

pipeline {
   agent { label 'master' }
   stages {
       stage('before') {
           steps {
               println("before")
           }
       }
       stage('para') {
           parallel {
               stage('apple') {
                   steps {
                       println("apple 1")
                       sleep(20 * Math.random())
                       println("apple 2")
                   }
               }
               stage('banana') {
                   steps {
                       println("banana 1")
                       sleep(20 * Math.random())
                       println("banana 2")
                   }
               }
               stage('peach') {
                   steps {
                       println("peach 1")
                       sleep(20 * Math.random())
                       println("peach 2")
                   }
               }
           }
       }
       stage('after') {
           steps {
               println("after")
           }
       }
   }
}

Skip steps

Jenkins pipeline stop early with success How to indicate that a job is successful pipeline conditional step stage

examples/jenkins/skip_step.Jenkinsfile

pipeline {
   agent none
   options {
       ansiColor('xterm')
       timestamps()
   }
   parameters {
       booleanParam(defaultValue: false, description: 'Checkbox', name: 'yesno')
   }
   stages {
       stage('first') {
           agent { label 'master' }
           steps {
               script {
                   println("first before")
                   println(params.yesno)
                   done = true
                   return
                   println("first after")
               }
           }
       }
       stage('second') {
           agent { label 'master' }
           when {
               expression {
                  return ! done;
               }
           }
           steps {
               script {
                   println("second")
               }
           }
       }
   }
}

examples/jenkins/worker_job.Jenkinsfile

import java.text.SimpleDateFormat
pipeline {
   agent none
   options {
       ansiColor('xterm')
       timestamps()
   }
   parameters {
       string(name: 'machine', defaultValue: '', description: 'Name of the machine')
       string(name: 'size',   defaultValue: '23', description: 'The size')
       choice(choices: ['Mercury', 'Venus', 'Earth', 'Mars'], description:  'Pick a planet', name: 'planet')
   //}
   stages {
       stage('try') {
           agent { label 'master' }
           steps {
               script {
                   sh "hostname"

                   def data = readJSON text: '{}'
                   data.name = "test-529" as String
                   data.date = new java.text.SimpleDateFormat('yyyyMMddHHmmss').format(new Date())
                   writeJSON(file: 'mydata.json', json: data, pretty: 4)
                   archiveArtifacts artifacts: 'mydata.json', onlyIfSuccessful: true

                   //error("Fail after first artifact")

                   writeJSON(file: 'otherfile.log', json: data, pretty: 4)
                   archiveArtifacts artifacts: '*.log', onlyIfSuccessful: true
               }
           }
       }
   }
}

jenkins sh commnad fails - jenkins stops

examples/jenkins/sh_fails.jenkinsfile

pipeline {
   agent { label 'master' }
   stages {
       stage('only') {
           steps {
               println("one")
               sh "ls -l"
               println("two")
               sh "ls -l no_such"
               println("three")
           }
       }
   }
}