【问题标题】:How to create methods for steps in Jenkins Declarative pipeline?如何为 Jenkins 声明式管道中的步骤创建方法?
【发布时间】:2019-01-10 18:05:00
【问题描述】:

我想将stepspost 包装在一个函数中。

这很好用:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                whateverFunction()
            }
            post {
                sh 'echo test'
            }
        }
    }
}

void whateverFunction() {
    sh 'ls /'
}

但是一旦我打包步骤并在我的函数中发布它就不起作用。 (失败的错误是:steps in a stage must be in a ‘steps’ block.

pipeline {
    agent any
    stages {
        stage('Test') {
            whateverFunction()
        }
    }
}

void whateverFunction() {
    steps {
        sh 'echo test'  
    }
    post {
        sh 'echo test'
    }
}

我还尝试使用steps,然后在该步骤中调用我的函数,其中包含steps。基本上在steps 中扭曲steps,这会导致不执行任何步骤的行为。 (但显然这将是一个有效的 Jenkins 文件)

是否可以在一个阶段内拥有一个包含stepspost 的函数。或者有没有办法实现类似的功能?

【问题讨论】:

标签: jenkins groovy jenkins-declarative-pipeline


【解决方案1】:

似乎您无法在声明性管道的方法中使用post 创建类似的功能。为此,您可以尝试使用脚本化管道。

对于声明性管道,您只能在 stage (example) 内或 stages 块 (example) 之后使用 post 部分。

【讨论】:

    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2022-10-27
    • 2017-12-03
    • 2018-01-03
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    相关资源
    最近更新 更多