如何在詹金斯的声明性管道中执行简单的if语句 [英] How to do simple if-statements inside a declarative pipeline in Jenkins

查看:597
本文介绍了如何在詹金斯的声明性管道中执行简单的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将脚本化管道转换为声明性管道. 想知道如何在steps {}块内执行简单的if语句.

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}")
                        }
                    }
                }
            )
        }
   }

这会导致以下异常:

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

由于不允许我在steps {}块内执行简单的if(..),因此对如何执行此操作有任何想法吗?

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

使用when {}使其成为一个完整的阶段似乎没有任何意义,因为在一个简单的阶段中会发生更多的步骤(如果存在则启动一个停止的容器,等等).

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).

做一个简单的if的最好方法是什么?

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

推荐答案

不幸的是,现在您必须将其包装在script中.因为它在这里说;

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.

这篇关于如何在詹金斯的声明性管道中执行简单的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆