Jenkins 。在选项阶段使用共享库

标签 jenkins jenkins-pipeline

所以我在 jenkins 中创建了一个共享库,其中有一个监听器,每次管道读取 FlowNode 时都会触发该监听器,这样我就可以在每个阶段、步骤等之前和之后运行 groovy 代码...

我可以像这样在步骤阶段调用共享库:

pipeline {
    agent any
    stages {
        stage('prepare') { 
            steps{
                prepareStepsWrapper()
            }
        }
        stage('step1') { 
            steps {
                echo 'step1'
            }
        }
        stage('step2') { 
            steps {
                echo 'step2'
            }
        }
        stage('step3') { 
            steps {
                echo 'step3'
                // fail on purpose
                sh 'notfoundexecutablelol'
            }
        }
        stage('step4') { 
            steps {
                echo 'step4'
            }
        }
    }
    post{
        always{
            println env.getEnvironment()
        }
    }
}

而且效果非常好!

使用这种方法,需要过滤掉“准备”阶段,所以我切换到选项指令:

pipeline {
    agent any
    options {
        prepareStepsWrapper()
    }
    stages {
        stage('step1') { 
            steps {
                echo 'step1'
            }
        }
        ...
    }
}

但是管道失败了

WorkflowScript: 4: Invalid option type "prepareStepsWrapper"

tl;博士;如何在选项指令中加载共享库?

最佳答案

option-stage 有什么作用?

The options directive allows configuring Pipeline-specific options from within the Pipeline itself.

您不能在options-stage 中调用共享库。此阶段不应用于执行任何逻辑,而是为管道设置配置。可以找到所有可用选项和文档 here .

您可以尝试创建一个简单地调用您的 prepareStepsWrapper() 并使用 locks 的阶段以避免其他阶段在此阶段之前执行。

关于 Jenkins 。在选项阶段使用共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56000849/

相关文章:

git - 在 Jenkins 中,有没有简单的方法可以从指定的 GIT SHA ID 进行构建?

linux - 无法在 Ubuntu Linux 上启动 Jenkins

jenkins - 用户无法在 API 组中获取资源 "services"- Jenkins 管道 EKS 部署

jenkins-pipeline - Jenkinsfile - 一种跳过整个管道的方法?

docker - 带有本地 docker 的 Jenkins 服务器

docker - Jenkins Golang 声明性管道 : Build Docker Image and Push to Docker Hub

Jenkins 管道 - mvn 命令无法正确读取 withCredentials 变量

java - Maven surefire 插件不在 xml 中包含用于通过测试的系统输出

xcode - xcode 6.1升级后如何手动修复 "ResourceRules.plist: cannot read resources"错误?

mysql - Jenkins 显示 "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"