How to do simple if-statements inside a declarative pipeline in Jenkins

我的梦境 提交于 2019-12-21 06:49:12

问题


I'm trying to convert my Scripted pipeline to a Declarative Pipeline. Wondering how to do a simple if-statement inside a steps {} block.

    stage ('Deploy to Docker') {
        steps {
            parallel (
                "instance1" : {
                    environment {
                        containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim()
                    }
                    steps {
                        if (containerId.isEmpty()) {
                            docker.image('some/image').run("--name ${fullDockerImageName}")
                        }
                    }
                }
            )
        }
   }

This causes the following Exception:

WorkflowScript: 201: Expected a step @ line 201, column 29.
                           if (containerId.isEmpty()) {

Since I'm not allowed to do a simple if(..) inside a steps {} block, any idea on how to do this?

It doesn't seem to make sense to make this a full stage with a when {}, since there are more steps that happens in a simple stage (starting a stopped container if it exists, etc).

What's the best way to do a simple if?


回答1:


Unfortunately you have to wrap it within a script, for now. As it says here;

Declarative Pipelines may use all the available steps documented in the Pipeline Steps reference, which contains a comprehensive list of steps, with the addition of the steps listed below which are only supported in Declarative Pipeline.

And if you look at the step reference it simply lists all plugins which contributes pipeline steps. And as far as I can see, there is no step supporting if, then, else. So the answer is, no, right now it is not possible, but, it should be fairly simple to implement this as a step and add to a plugin.




回答2:


This should work

pipeline {
     stages {
        stage ('Main Stage') {
            steps {
                script {
                    if (true) {
                        stage ('Stage 1') {
                            sh 'echo Stage 1'
                        }
                    }
                    if (false) {
                        stage ('Stage 2') {
                            sh 'echo Stage 2'
                        }
                    }
                }
            }
        }
    }
}



回答3:


Using the Conditional BuildStep plugin you can add a when {} step to process a conditional.

The following should work, barring syntax issues with the isEmpty() check within this context.

stage ('Deploy to Docker') {
    steps {
        parallel (
            "instance1" : {
                environment {
                    containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim()
                }
                when {
                    expression {
                        return containerId.isEmpty()
                    }
                }
                step {
                    docker.image('some/image').run("--name ${fullDockerImageName}")
                }
            }
        )
    }
}

The related blog post is here.

EDIT: Sorry, the actual snytax seems to be closer to this, which doesn't have access to your needed conditional:

stage ('Deploy to Docker') {
    when {
        expression {
            return containerId.isEmpty()
        }
    }
    steps {
        parallel (
            "instance1" : {
                environment {
                    containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim()
                }
                step {
                    docker.image('some/image').run("--name ${fullDockerImageName}")
                }
            }
        )
    }
}



回答4:


I think that this is the most correct/best practice way about using if/else or control logic within your Jenkins Declarative pipeline.

https://jenkins.io/doc/book/pipeline/syntax/#when

@IronSean answer, doesn't seem like you need that plugin (anymore).



来源:https://stackoverflow.com/questions/42281200/how-to-do-simple-if-statements-inside-a-declarative-pipeline-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!