Create a gist now

Instantly share code, notes, and snippets.

Embed
Best of Jenkinsfile, a collection of useful workflow scripts ready to be copied into your Jenkinsfile on a per use basis.
#!groovy
# Best of Jenkinsfile
# `Jenkinsfile` is a groovy script DSL for defining CI/CD workflows for Jenkins
node {
}
# Jenkinsfile
# Build and test a Maven project
node {
git url: 'https://github.com/user/repo.git'
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn -B -Dmaven.test.failure.ignore verify"
step([$class: 'JUnitResultArchiver', testResults:
'**/target/foobar/TEST-*.xml'])
}
# Jenkinsfile
# Verify a Maven project
node {
git url: 'https://github.com/user/repo.git'
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn -B verify"
}
stage "unit test"
node {
git "git@github.com:michaelneale/oaks-trail-ride.git"
sh "echo unit test app"
}
stage "test on supported OSes"
parallel (
windows: { node {
sh "echo building on windows now"
}},
mac: { node {
sh "echo building on mac now"
}}
node {
for (int i=0; i< 2; ++i) {
stage "Stage #"+i
print 'Hello, world $i!'
}
stage "Stage Parallel"
def branches = [:]
for (int i = 0; i < numHelloMessages.toInteger(); i++) {
branches["split${i}"] = {
stage "Stage parallel- #"+i
node('remote') {
echo 'Starting sleep'
sleep 10
echo 'Finished sleep'
}
}
}
parallel branches
}
@abayer

This comment has been minimized.

Show comment
Hide comment
@abayer

abayer Dec 1, 2015

Hi! Mind if we pull some of these examples over into https://github.com/jenkinsci/workflow-examples? Thanks!

abayer commented Dec 1, 2015

Hi! Mind if we pull some of these examples over into https://github.com/jenkinsci/workflow-examples? Thanks!

@michaelsanford

This comment has been minimized.

Show comment
Hide comment
@michaelsanford

michaelsanford Apr 26, 2016

Are we missing a closing ) from opening parallel ( in parallel-stage-2.groovy?

michaelsanford commented Apr 26, 2016

Are we missing a closing ) from opening parallel ( in parallel-stage-2.groovy?

@botchniaque

This comment has been minimized.

Show comment
Hide comment
@botchniaque

botchniaque Jun 23, 2016

For the parallel-stage.groovy I get this:

ERROR: The ‘stage’ step must not be used inside a ‘parallel’ block.
Finished: FAILURE

Does it mean I have older or newer version?

For the parallel-stage.groovy I get this:

ERROR: The ‘stage’ step must not be used inside a ‘parallel’ block.
Finished: FAILURE

Does it mean I have older or newer version?

@damiano-migme

This comment has been minimized.

Show comment
Hide comment
@damiano-migme

damiano-migme Jun 28, 2016

@botchniaque , the error you're getting is due to a restriction recently introduced. It's not possible to use a stage step inside a parallel block.

@botchniaque , the error you're getting is due to a restriction recently introduced. It's not possible to use a stage step inside a parallel block.

@wohlben

This comment has been minimized.

Show comment
Hide comment
@wohlben

wohlben Jun 29, 2016

not a native speaker, so sorry for my poor english...

I was having some trouble with the parallel execution of your example and thought i could share my solution, in case somebody else stumbles upon this.

the insane restriction of no stages inside parallel (forcing us to go back to bash scripts for most tests, making the jenkins log significantly harder to read) has already been mentioned, but it hasn't been mentioned that the variable "i" inside the curly brakes isnt evaluated until the parallel execution - making it '3' in every single branch.
i solved it by using a method to create said command

def domains = ['leap42', 'opensuse13', 'sles12']
def executions = [:]

def create_execution(String dist) {
    cmd = { 
        node {
            sh "echo ${dist}"
        }
    }
    return cmd
}

for(int i = 0; i < domains.size(); i++) {
    executions[domains[i]] = create_execution(domains[i])
    }

parallel executions

wohlben commented Jun 29, 2016

not a native speaker, so sorry for my poor english...

I was having some trouble with the parallel execution of your example and thought i could share my solution, in case somebody else stumbles upon this.

the insane restriction of no stages inside parallel (forcing us to go back to bash scripts for most tests, making the jenkins log significantly harder to read) has already been mentioned, but it hasn't been mentioned that the variable "i" inside the curly brakes isnt evaluated until the parallel execution - making it '3' in every single branch.
i solved it by using a method to create said command

def domains = ['leap42', 'opensuse13', 'sles12']
def executions = [:]

def create_execution(String dist) {
    cmd = { 
        node {
            sh "echo ${dist}"
        }
    }
    return cmd
}

for(int i = 0; i < domains.size(); i++) {
    executions[domains[i]] = create_execution(domains[i])
    }

parallel executions
@Headcult

This comment has been minimized.

Show comment
Hide comment
@Headcult

Headcult Sep 9, 2016

Thanks for this! Very useful.

Can someone tell me how to execute/run 'Jenkinsfile' when it's added as part of the project folder structure? Basically, I'm looking for a way where we can run this Jenkinsfile like how we execute docker from Dockerfile?

Headcult commented Sep 9, 2016

Thanks for this! Very useful.

Can someone tell me how to execute/run 'Jenkinsfile' when it's added as part of the project folder structure? Basically, I'm looking for a way where we can run this Jenkinsfile like how we execute docker from Dockerfile?

@SpencerMalone

This comment has been minimized.

Show comment
Hide comment
@SpencerMalone

SpencerMalone Sep 15, 2016

Are you asking how to run the Jenkinsfile on a local machine without jenkins?

Are you asking how to run the Jenkinsfile on a local machine without jenkins?

@kevinpgrant

This comment has been minimized.

Show comment
Hide comment
@kevinpgrant

kevinpgrant Nov 10, 2016

@Headcult Dockerfiles can be run at the command line with a "docker build" command, but the Jenkinsfiles are not executed in this manner

When you commit and push your Jenkinsfile to your git repo (assuming you are using git, jenkins and the git plugin) then the Jenkins slave(s) will execute the steps in the Jenkinsfile for you. You will need to have configured a job pointing to the git repo and watching for changes first. Look at the Folders plugin and run Folder Computation in Jenkins - this will create a job for every branch which has a Jenkinsfile present in your repo, it's very useful!

(bonus tip: You can even setup your Jenkinsfile to run your docker build)

@Headcult Dockerfiles can be run at the command line with a "docker build" command, but the Jenkinsfiles are not executed in this manner

When you commit and push your Jenkinsfile to your git repo (assuming you are using git, jenkins and the git plugin) then the Jenkins slave(s) will execute the steps in the Jenkinsfile for you. You will need to have configured a job pointing to the git repo and watching for changes first. Look at the Folders plugin and run Folder Computation in Jenkins - this will create a job for every branch which has a Jenkinsfile present in your repo, it's very useful!

(bonus tip: You can even setup your Jenkinsfile to run your docker build)

@hayesgm

This comment has been minimized.

Show comment
Hide comment
@hayesgm

hayesgm Nov 30, 2016

For the parallel case, reading the docs from Jenkins from "Jobs in Parallel," you should use:

node {
  for (int i=0; i< 2; ++i) {  
    stage "Stage #"+i
    print 'Hello, world $i!'
  }

  stage "Stage Parallel"
  def branches = [:]
  for (int i = 0; i < numHelloMessages.toInteger(); i++) {
    def index = i
    branches["split${i}"] = {
      stage "Stage parallel- #"+index
      node('remote') {
       echo  'Starting sleep'
       sleep 10
       echo  'Finished sleep'
      }
    }
  }
  parallel branches
}

This will solve the issue of having i=4 for each iteration of the parallel loop.

hayesgm commented Nov 30, 2016

For the parallel case, reading the docs from Jenkins from "Jobs in Parallel," you should use:

node {
  for (int i=0; i< 2; ++i) {  
    stage "Stage #"+i
    print 'Hello, world $i!'
  }

  stage "Stage Parallel"
  def branches = [:]
  for (int i = 0; i < numHelloMessages.toInteger(); i++) {
    def index = i
    branches["split${i}"] = {
      stage "Stage parallel- #"+index
      node('remote') {
       echo  'Starting sleep'
       sleep 10
       echo  'Finished sleep'
      }
    }
  }
  parallel branches
}

This will solve the issue of having i=4 for each iteration of the parallel loop.

@atul1989

This comment has been minimized.

Show comment
Hide comment
@atul1989

atul1989 Dec 29, 2016

Can you someone please help , That how can i deploy my 8 build artifact into the env server simultaneously via jenkins jobs through Deployit tool

Can you someone please help , That how can i deploy my 8 build artifact into the env server simultaneously via jenkins jobs through Deployit tool

@mattvonrocketstein

This comment has been minimized.

Show comment
Hide comment
@mattvonrocketstein

mattvonrocketstein Jan 6, 2017

Can anyone answer whether the examples with iteration actually work? JENKINS-26481 and this stack overflow indicate that loops of all kinds are broken

Can anyone answer whether the examples with iteration actually work? JENKINS-26481 and this stack overflow indicate that loops of all kinds are broken

@stevaaa

This comment has been minimized.

Show comment
Hide comment
@stevaaa

stevaaa Jan 30, 2017

Hi guys, Can you give an example to pass parameters from one job to another job? (Pipeline project)
I have been stuck with this for a while now, kindly provide suggestions.

I'm trying the below, which did not work for me:(

Project Name: test2
node() {
stage 'pass params'
paramAValue = "paramAValue"
paramBValue = "paramBValue"
build job: 'test3', parameters: [[$class: 'StringParameterValue', name: 'ParamA', value: paramAValue], [$class: 'StringParameterValue', name: 'ParamB', value: paramBValue]]
}

Project Name: test3
node() {
stage 'retrieve params'
print "${env.paramA}"
}

stevaaa commented Jan 30, 2017

Hi guys, Can you give an example to pass parameters from one job to another job? (Pipeline project)
I have been stuck with this for a while now, kindly provide suggestions.

I'm trying the below, which did not work for me:(

Project Name: test2
node() {
stage 'pass params'
paramAValue = "paramAValue"
paramBValue = "paramBValue"
build job: 'test3', parameters: [[$class: 'StringParameterValue', name: 'ParamA', value: paramAValue], [$class: 'StringParameterValue', name: 'ParamB', value: paramBValue]]
}

Project Name: test3
node() {
stage 'retrieve params'
print "${env.paramA}"
}

@grahamatgithub

This comment has been minimized.

Show comment
Hide comment
@grahamatgithub

grahamatgithub Mar 8, 2017

stage ('pass params') {
def paramAValue = "paramAValue"
...
}

etc.

Also try:
echo paramA not print "${env.paramA}"

grahamatgithub commented Mar 8, 2017

stage ('pass params') {
def paramAValue = "paramAValue"
...
}

etc.

Also try:
echo paramA not print "${env.paramA}"

@bparker98

This comment has been minimized.

Show comment
Hide comment
@bparker98

bparker98 Feb 27, 2018

The preferred method of getting job parameters is to use the "params" map.

So use echo params.paramA

See https://jenkins.io/doc/book/pipeline/syntax/#parameters

The preferred method of getting job parameters is to use the "params" map.

So use echo params.paramA

See https://jenkins.io/doc/book/pipeline/syntax/#parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment