Jenkins Pipeline Syntax: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 131: Line 131:


====input====
====input====
{{External|https://jenkins.io/doc/pipeline/steps/pipeline-input-step/}
{{External|https://jenkins.io/doc/pipeline/steps/pipeline-input-step/}]
 
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 <code>org.jenkinsci.plugins.workflow.steps.FlowInterruptedException</code>, which produces "gray" pipelines.
<syntaxhighlight lang='groovy'>
</syntaxhighlight>


=Obtaining the Current Pipeline Build Number=
=Obtaining the Current Pipeline Build Number=

Revision as of 17:42, 6 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

https://jenkins.io/doc/pipeline/steps/

node

https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-node-allocate-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

https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-sh-shell-script

Shell Script.

ws

https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-ws-allocate-workspace

Allocate workspace.

build

https://jenkins.io/doc/pipeline/steps/pipeline-build-step/

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

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#-dir-change-current-directory

Change current directory.

echo

error

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#error-error-signal

readFile

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace

Read a file from the workspace.

def versionFile = readFile("${stage.WORKSPACE}/terraform/my-module/VERSION")

stash

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#stash-stash-some-files-to-be-used-later-in-the-build

input

{{External|https://jenkins.io/doc/pipeline/steps/pipeline-input-step/}]

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.

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.