Jenkins - Pipeline

About

The pipeline step allows you to define your Pipelines in a more structured way.

Pipelines are a series of steps that allow you to orchestrate the work required to build, test and deploy applications. Pipelines are defined in a file called Jenkinsfile that is stored in the root of your project’s source repository.

See Getting started

Syntax

Linter

Pipeline syntax validation (ie Linting) via HTTP POST using curl from Pipeline Dev tool

# curl (REST API)
# User
JENKINS_USER=bitwisenote-jenkins1

# Api key from "/me/configure" on my Jenkins instance
JENKINS_USER_KEY=--my secret, get your own--

# Url for my local Jenkins instance.
JENKINS_URL=http://$JENKINS_USER:[email protected]:32769 

# JENKINS_CRUMB is needed if your Jenkins master has CRSF protection enabled (which it should)
JENKINS_CRUMB=`curl "$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"`
curl -X POST -H $JENKINS_CRUMB -F "jenkinsfile=<Jenkinsfile" $JENKINS_URL/pipeline-model-converter/validate

Example

Basic

pipeline {

    agent any 
    
    environment {
        AWS_ACCESS_KEY_ID     = credentials('jenkins-aws-secret-key-id')
        AWS_SECRET_ACCESS_KEY = credentials('jenkins-aws-secret-access-key')
    }
    
    stages {
        stage('Stage 1') {
            steps {
                echo 'Hello world!' 
                script {
                    if (isUnix()) {
                        sh 'echo Unix'
                    } else {
                        bat 'echo Windows'
                    }
                }
            }
        }
    }
}

where:

  • agent instructs Jenkins to allocate an executor (on any available agent/node in the Jenkins environment) and workspace for the entire Pipeline.
  • node effectively does the same as agent
  • echo writes simple string in the console output.

Maven

pipeline {
  agent { docker 'maven:3.3.3' }
  stages {
    stage('build') {
      steps {
        sh 'mvn --version'
        sh 'mvn install'
      }
    }
  }
}

Documentation / Reference


Powered by ComboStrap