简体   繁体   English

如何在继承自管道共享库的声明性 jenkins 管道中定义其他参数?

[英]How can I define additional parameters in a declarative jenkins pipeline who inherit from a pipeline shared library?

I'm looking to see if it's possible for something like this: How can I define additional parameters in jenkinsfile who inherit from a pipeline shared lib?我想看看这样的事情是否可能: 如何在从管道共享库继承的 jenkinsfile 中定义其他参数?

But in a declarative pipeline, I've tried solutions similar to that of the post above but with no luck.但是在声明性管道中,我尝试了与上述帖子类似的解决方案,但没有运气。

I need to be able to declare a shared library of build parameters which I can then use in multiple declared pipelines.我需要能够声明一个构建参数的共享库,然后我可以在多个声明的管道中使用它。

Something like this:像这样的东西:

pipeline {  
agent {
    label 'slave'
}

parameters { // Build parameters
    string(defaultValue: 'test', description: 'SCM branch', name: 'UUT_BRANCH', trim: false)
    # DEFININED IN SHARED LIBRARY 
}

I wondered if anyone could provide any input?我想知道是否有人可以提供任何意见? Many thanks.非常感谢。

I never used parameters which are inherited from a shared lib.我从未使用过从共享库继承的参数。 But this is how it works with a function declared in the library:但这就是它与库中声明的函数的工作方式:

Inside the library there is a groovy file containing the function:在库里面有一个包含函数的 groovy 文件:

def call(String name = 'human') {
    echo "Hello, ${name}!"
}

After configuring the library as a shared lib in jenkins, you can use the function in the declarative pipeline like this:在 jenkins 中将该库配置为共享库后,您可以像这样在声明性管道中使用该函数:

stage('useSharedLib'){
    steps {
        sayHello 'Stranger'
      }
    }

Maybe this will help you with implementing env vars也许这会帮助你实现环境变量

This is how I have solved mine need for shared parameter defined in library while individual jobs define their own custom parameters.这就是我如何解决我对库中定义的共享参数的需求,同时各个作业定义自己的自定义参数。 This is how mine Jenkinsfile looks like:这是我的Jenkinsfile样子:

#!groovy


@Library('my-library@master') _


properties([
    parameters([
        string(name: 'PARAM_A', defaultValue: '1', description: 'Aaa'),
        string(name: 'PARAM_B', defaultValue: '2', description: 'Bbb'),
        string(name: 'PARAM_C', defaultValue: '3', description: 'Ccc'),
    ] + runTest.commonJobParams())
])


runTest(
    params: params,
)

and this is how vars/runTest.groovy in my library looks like:这就是我的库中vars/runTest.groovy样子:

def commonJobParams() {
    return [
        string(
            name: 'GOLDEN',
            defaultValue: '999',
            description: 'Description of param from library',
        ),
    ]
}

def call(Map config) {
    pipeline {
        agent {
            ...
        }
        stages {
            stage('Test') {
                steps {
                    echo "Hello ${PARAM_A}"
                    echo "Hello ${PARAM_B}"
                    echo "Hello ${PARAM_C}"
                    echo "Hello ${GOLDEN}"
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM