Specify agent in Jenkins Pipeline Shared Library - jenkins

I am trying to customize a shared Library which run a stage in a specify agent by adding the possibility to specify the agent.
def call(String agentLabel) {
pipeline {
agent { label "$agentLabel" }
stages {
stage('Hello') { steps { echo 'Hello World' } }
}
}
}
This is OK but now I want to customize the type of agent (IE. either use label or docker agent) ?
This don't work :
def call(String agentLabel, String agentDockerImage = null) {
Closure agentRunner = null
if (agentDockerImage) {
agentRunner = {
docker {
label "$agentLabel"
image "$agentDockerImage"
}
}
} else {
agentRunner = {
label "$agentLabel"
}
}
pipeline {
agent { agentRunner.call() }
stages {
stage('Hello') { steps { echo 'Hello World' } }
}
}
}
WorkflowScript: 15: Invalid agent type "call" specified. Must be one of [any, docker, dockerfile, label, none] # line 15, column 29.
agent { agentRunner.call() }
^

Related

Declarative Jenkins pipeline - How to set agent any only when condition is met?

Is it possible to specify agent 'any' only when a condition is met?
Context: I have a parallel pipeline with some common steps. The pipeline runs tests on Linux and Windows on two different agents. There are parameter defining if both, or only Linux, or only Windows should run. I want to use as little agents as possible.
With the following pipeline, the Linux tests are executed on the main agent. The Windows tests on a different agent. So if only Linux needs to run, I only use 1 agent, which is good. But if only Windows needs to run, the main agent will be blocked until the Windows agent finishes.
pipeline {
agent any // main agent
parameters {
booleanParam( name: 'RUN_WINDOWS' )
booleanParam( name: 'RUN_LINUX' )
}
stages {
stage('Common step') {
...
}
stage("Test") {
parallel {
stage('Windows') {
agent any
when {
expression { return params.RUN_WINDOWS }
}
stages {
stage('Test Windows') {
...
}
}
}
stage('Linux') { // no agent specified, so will use the main agent
when {
expression { return params.RUN_LINUX }
}
stages {
stage('Test Linux') {
...
}
}
}
}
post {
...
}
}
}
}
I'd need something like this for the Windows stage, where it only specifies an agent based on a condition:
if (params.RUN_WINDOWS && params.LINUX) { agent any }
EDIT: I'm looking for a solution without using labels.
As a workaround, instead of setting any you can set agent labels conditionally. Something like below.
def labels = "Windows"
if (params.RUN_WINDOWS && params.LINUX) {
labels = "windows || linux"
}
pipeline {
agent any // main agent
parameters {
booleanParam( name: 'RUN_WINDOWS' )
booleanParam( name: 'RUN_LINUX' )
}
stages {
stage('Common step') {
...
}
stage("Test") {
parallel {
stage('Windows') {
agent {
label "${labelSelected}"
}
when {
expression { return params.RUN_WINDOWS }
}
stages {
stage('Test Windows') {
...
}
}
}
stage('Linux') { // no agent specified, so will use the main agent
when {
expression { return params.RUN_LINUX }
}
stages {
stage('Test Linux') {
...
}
}
}
}
post {
...
}
}
}
}

Jenkins conditional step for string contains

I am trying to do a conditional step on Jenkins to see if the String Parameter contains a certain word.
I have a string for PLATFORM. The values in it can be Windows, Mac, Linux
I want to run a certain step if the value of the parameter contains Linux.
How can I do that? I downloaded the Jenkins plugin for conditional step but it doesn't have a contains clause.
You can use when directive of Jenkins to achieve the conditional steps.
Example:
pipeline {
agent any
stages {
stage ('Windows RUN') {
when {
expression { params.PLATFORM == 'Windows' }
}
steps {
echo "Hello, Windows"
}
}
stage ('Mac RUN') {
when {
expression { params.PLATFORM == 'Mac' }
}
steps {
echo "Hello, Mac"
}
}
stage ('Linux RUN') {
when {
expression { params.PLATFORM == 'Linux' }
}
steps {
echo "Hello, Linux"
}
}
}
}
Output:

Jenkins declarative pipeline expression with boolean environment variable

I'm using Jenkins declarative pipeline and I want to make a conditional step depending on an environment variable, which is set according the existence of a file.
So I just want to make something like that : if Dockerfile exist, perform next stage, else don't.
To perform this I tried :
pipeline {
// ...
stage {
stage('Docker') {
environment {
IS_DOCKERFILE = fileExists 'Dockerfile'
}
when {
environment name: 'IS_DOCKERFILE', value: true
}
stage('Build') {
// ...
}
}
}
}
Or :
pipeline {
// ...
stage {
stage('Docker') {
environment {
IS_DOCKERFILE = fileExists 'Dockerfile'
}
when {
expression {
env.IS_DOCKERFILE == true
}
}
stage('Build') {
// ...
}
}
}
}
In both cases, the Dockerfile exist and it is in the workspace. I also tried with strings ("true") but everytime, the pipeline continue without executing the stage 'Build'.
Any suggestions ?
This is because the exprsssion:
IS_DOCKERFILE = fileExists 'Dockerfile'
Creates the environment variable with boolean value as string:
$ set
IS_DOCKERFILE='false'
So the solution would be to use .toBoolean() like this:
environment {
IS_DOCKERFILE = fileExists 'Dockerfile'
}
stages {
stage("build docker image") {
when {
expression {
env.IS_DOCKERFILE.toBoolean()
}
}
steps {
echo 'fileExists'
}
}
stage("build libraries") {
when {
expression {
!env.IS_DOCKERFILE.toBoolean()
}
}
steps {
echo 'fileNotExists'
}
}
}
As #Sergey already posted, the problem is that you're comparing a string to a boolean. See fileExists: Verify if file exists in workspace.
Besides his answer, you can compare directly to a string:
environment {
IS_DOCKERFILE = fileExists 'Dockerfile'
}
stages {
stage("build docker image") {
when {
expression {IS_DOCKERFILE == 'true'}
}
steps {
echo 'fileExists'
}
}
stage("build libraries") {
when {
expression {IS_DOCKERFILE == 'false'}
}
steps {
echo 'fileNotExists'
}
}
}

How can I have one pipeline executed even if the other has failed in jenkins

I have the following (part of a) pipeline
stages {
stage('bootstrap') {
parallel {
stage("Linux") {
agent { label 'linux' }
steps {
sh 'bootstrap.sh'
}
}
stage("Windows") {
agent { label 'Windows' }
steps {
bat 'bootstrap.bat'
}
}
}
}
stage('devenv') {
parallel {
stage('Linux') {
agent { label 'linux' }
steps {
sh 'devenv.sh'
}
}
stage('Windows') {
agent { label 'Windows' }
steps {
bat 'devenv.bat'
}
}
}
}
}
post {
always {
echo "Done"
}
}
The problem is that when bootstrap.bat fails on windows, the devenv step is now considered failed, and the linux devenv won't continue. I would like to have the results of the linux pipeline even if the windows one fails early.
An option would be to separate the stages so that linux full pipeline is on one branch of the parallel execution, and windows is on the other, but maybe there's a trick I am not aware of, because I tried it and it does not seem to be acceptable syntax.
Edit
Suggested fix does not work. This is the pipeline
pipeline {
agent none
parallel {
stage('Linux') {
agent { label 'linux' }
stages {
stage('bootstrap') {
sh "ls"
}
stage('devenv') {
sh "ls"
}
}
}
stage('windows') {
agent { label 'Windows' }
stages {
stage('bootstrap') {
bat 'dir'
}
stage('devenv') {
bat 'dir'
}
}
}
}
}
This is the error message
WorkflowScript: 8: Undefined section "parallel" # line 8, column 5.
parallel {
^
WorkflowScript: 1: Missing required section "stages" # line 1, column 1.
pipeline {
^

Using variable in another stage in declarative Jenkinsfile

I'm writing a declarative Jenkinsfile which looks like this. In the stage "build" I define the variable customImage which I would like to use in the stage "Push".
Unfortunately I cannot get this to work.
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
def commitHash = GIT_COMMIT.take(7)
echo "Building Docker image for commit hash: " + commitHash
def customImage = docker.build("myimage:${commitHash}")
}
}
}
stage("Push") {
steps {
echo "Pushing Docker image to registry..."
script {
docker.withRegistry(REGISTRY_SERVER, REGISTRY_CREDENTIALS) {
$customImage.push()
}
}
}
}
}
}
You just have to define the variable at a scope, where you can access it later, i.e.
def customImage
pipeline {
agent any
stages {
stage("Build") {
steps {
script {
def commitHash = GIT_COMMIT.take(7)
echo "Building Docker image for commit hash: " + commitHash
customImage = docker.build("myimage:${commitHash}")
}
}
}
stage("Push") {
steps {
echo "Pushing Docker image to registry..."
script {
docker.withRegistry(REGISTRY_SERVER, REGISTRY_CREDENTIALS) {
customImage.push()
}
}
}
}
}
}

Resources