Jenkins - kamialie/knowledge_corner GitHub Wiki

  • Check
    • Dark theme
    • Full stage view on overview page
    • Jenkins runner VScode extention

Jenkins versioning:

  • weekly - increments minor part (2.345 -> 2.346)
  • LTS (Long term support) - every 12 months one weekly is chosen as LTS, adds patch part for fixes (2.236.1 -> 2.346.2)

Best practices

Create a stage to list environment and tool dependencies (git, docker version, etc).

Shared library

All functions have to be in the directory called vars. Function named call() is called automaticaly, however, other functions in the file are accessible via dot notation <file_name>.<function_name>(<parameters>).

# function_name.groovy, must match the name of the step to call in the pipeline
def call() { # requirement, has to be named call()
    node { # make it available as pipeline step?
    }
}

Pass a map of values:

# f.groovy
def call(Map config=[:]) {
    sh "$config.foo"
    sh "$config.bar"
}

pipeline {
    ...
    steps {
       f foo: "one", bar: "two"
    }
}

Library development

host/pipeline-model-converter/validate - linter endpoint on Jenkins master

$ curl -X POST -F "jenkinsfile=<./Jenkinsfile>" <endpoint>
Jenkinsfile successfuly validated

There is also a VScode extention "Jenkins Pipeline linter", just specify Jenkins server endpoint in configuration. There will be a new task available "Validate Jenkinsfile".

Unit tests framework

Groovy

Install and run groovy console:

$ brew install groovy
$ groovyConsole

Optionally typed language. String, int, float (for big numbers use BigDecimal type), Boolean. Use or don't use semicolons (no difference).

String[] words = ["one", "two"]
for(String word: words) {
    println(word)
}
// shorthand
words.each{word -> println(x)}
words.each{println(it)}

CPS

Pipeline script is transfored to special form (serialized) that can be persisted on disk and survive Jenkins restart. This uses Continuation-Passing Style (CPS), which implies some limitations. Library functions can not use closures, or must designated explicitly that they aren't CPS compliant (NonCPS annotataion).

@NonCPS
def call() {}

References

⚠️ **GitHub.com Fallback** ⚠️