Jenkins Pipeline Syntax: Difference between revisions
Line 144: | Line 144: | ||
{{External|https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#-timeout-enforce-time-limit}} | {{External|https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#-timeout-enforce-time-limit}} | ||
Upon timeout, an <code>org.jenkinsci.plugins.workflow.steps.FlowInterruptedException</code> is thrown. | Upon timeout, an <code>org.jenkinsci.plugins.workflow.steps.FlowInterruptedException</code> is thrown '''from the closure that is being executed, and not from the timeout() invocation'''. The code shown below prints "A", "B", "D": | ||
<syntaxhighlight lang='groovy'> | |||
timeout(time: 5, unit: 'SECONDS') { | |||
echo "A" | |||
try { | |||
try { | |||
echo "B" | |||
} | |||
catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) { | doSometing(); // this step takes a very long time and will time out | ||
echo "C" | |||
} | |||
catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) { | |||
echo "D" | |||
// you can catch the exception ... nor not, in which case the pipeline will be aborted | |||
} | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 17:50, 13 December 2019
External
Internal
Scripted Pipeline
node('some-worker-label') {
echo 'Pipeline logic starts'
stage('Build') {
// ...
}
stage('Test') {
// ...
}
stage('Deploy') {
// ...
}
Parallel Stages
stage("tests") {
parallel(
"unit tests": {
// run unit tests
},
"coverage tests": {
// run coverage tests
}
)
}
Declarative Pipeline
pipeline {
agent any
options {
skipStagesAfterUnstable()
}
stages {
stage('Build') {
steps {
sh 'make'
}
}
stage('Test'){
steps {
sh 'make check'
junit 'reports/**/*.xml'
}
}
stage('Deploy') {
steps {
sh 'make publish'
}
}
}
}
Pipeline Steps
node
Allocates an executor or a node, typically a worker, and run the enclosed code in the context of the workspace of that worker. Node may take a label name, computer name or an expression.
The labels are declared on workers when they are defined in the master configuration, in their respective "clouds".
sh
Shell Script.
ws
Allocate workspace.
build
This is how a main pipeline launches in execution a subordinate pipeline.
This is how we may be able to return the result: https://support.cloudbees.com/hc/en-us/articles/218554077-How-to-set-current-build-result-in-Pipeline
Basic Steps
These basic steps are used invoking on stage.
. In a Jenkinsfile, and inside a stage, invoke on this.
or simply invoking directly, without qualifying.
dir
Change current directory.
echo
error
readFile
Read a file from the workspace.
def versionFile = readFile("${stage.WORKSPACE}/terraform/my-module/VERSION")
stash
input
In its basic form, renders a "Proceed"/"Abort" input box with a custom message. Selecting "Proceed" passes the control to the next step in the pipeline. Selecting "Abort" throws a org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
, which produces "gray" pipelines.
input(
id: 'Proceed1',
message: 'If the manual test is successful, select \'Proceed\'. Otherwise, you can abort the pipeline.'
)
timeout
Upon timeout, an org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
is thrown from the closure that is being executed, and not from the timeout() invocation. The code shown below prints "A", "B", "D":
timeout(time: 5, unit: 'SECONDS') {
echo "A"
try {
echo "B"
doSometing(); // this step takes a very long time and will time out
echo "C"
}
catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
echo "D"
// you can catch the exception ... nor not, in which case the pipeline will be aborted
}
}
Obtaining the Current Pipeline Build Number
def buildNumber = currentBuild.rawBuild.getNumber()
BUILD_TAG
It is an environment variable that contains job name, branch name, build number.
FlowInterruptedException
throw new FlowInterruptedException(Result.ABORTED)