Thursday, May 17, 2018

Some random notes about Jenkins pipelines

First of all, there are two type of syntax:
  • Declarative
    • has a single pipeline{} block on the top level
    • it is possible to use Scripted pipeline syntax within a script{} step
  • Scripted
    • has stage('name'){} or node{} blocks on the top level
There are subtle differences:
  • Changing directories
    • in Scripted you can use the dir(){} block
    • in Declarative you cannot. One way to change directories is to change directory on every line of your commands.

Declarative pipeline

Workspace

Apparently the workspace is something that is shared within the pipeline. It doesn't matter how many different docker images you run your steps in, they will all work in the exact same directory and whatever they change will be inherited by the next stage too.

Example: Same and different agents across multiple stages modifying the same workspace

pipeline {
    agent none
    stages {
        stage ('Node build') {
            agent {
                docker {
                    image 'node:8.7.0'
                }
            }
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
        stage ('Node test') {
            agent {
                docker {
                    image 'node:8.7.0'
                }
            }
            steps {
                sh 'npm run test'
            }
        }
        stage ('Maven build') {
            agent {
                docker {
                    image 'maven:3.3.9'
                }
            }
            steps {
                sh 'mvn clean install'
            }
        }
    }
}

No comments:

Post a Comment