red*_*888 4 jenkins jenkins-plugins jenkins-pipeline
我想创建一个自定义步骤,如下所示:https : //jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps
该脚本如下所示:
// vars/buildPlugin.groovy
def call(body) {
// evaluate the body block, and collect configuration into the object
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
...
Run Code Online (Sandbox Code Playgroud)
我可以在脚本管道中像这样运行它:
buildPlugin {
name = 'git'
}
Run Code Online (Sandbox Code Playgroud)
这意味着在声明性管道中,我必须将其包装在脚本块中:
script {
buildPlugin {
name = 'git'
}
}
Run Code Online (Sandbox Code Playgroud)
我有很多自定义脚本和groovy类,这使事情变得混乱,不得不将它们包装在管道中的脚本块中。我可以编写无需脚本{}就可以使用声明性管道的方式来编写Groovy脚本吗?
编辑:
从管道中调用一个groovy脚本,如下所示:
myCustomStep('sldkfjlskdf')
Run Code Online (Sandbox Code Playgroud)
但是我想像在示例中一样使用哈希表:
# In myCustomStep.grooy
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
Run Code Online (Sandbox Code Playgroud)
为了立即调用它,我必须做:
myCustomStep{
param1 = 'sldkfjlskdf'
param2 = 'sdfsdfsdfdf'
}
Run Code Online (Sandbox Code Playgroud)
这样做,我Expected a step @ line....必须将其包装起来
有没有一种方法可以像哈希表方法那样获得漂亮的命名参数,而不必分步进行呢?我也尝试称它myCustomStep({param1 = 'sdfsdf'})不起作用
您也可以在没有script包装的声明性pipline中使用它
这是运作良好的示例:
脚本
//vars/shOut.groovy
def call(shellScript) {
return sh(returnStdout: true, script: shellScript).trim()
}
Run Code Online (Sandbox Code Playgroud)
詹金斯档案
@Library('modelsLib') _
pipeline {
agent { label 'master' }
stages {
stage('some stage') {
steps {
echo "hello!"
shOut 'touch xxyyzz'
sh 'ls -otr'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出
[Pipeline] {
[Pipeline] stage
[Pipeline] { (some stage)
[Pipeline] echo
hello!
[Pipeline] sh
[test-pipeline] Running shell script
+ touch xxyyzz
[Pipeline] sh
[test-pipeline] Running shell script
+ ls -otr
total 32
-rw-r--r-- 1 jenkins 0 Dec 20 17:59 xxyyzz
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1839 次 |
| 最近记录: |