Jenkins的脚本化管道中,我们可以创建方法并调用它们。
在Jenkins声明式管道中是否也可以实现这一点?如果可以,具体如何操作?
Jenkins的脚本化管道中,我们可以创建方法并调用它们。
在Jenkins声明式管道中是否也可以实现这一点?如果可以,具体如何操作?
新版本的声明性流水线支持这一点,而以前(约2017年中期)是不可能的。您可以像从Groovy脚本中预期的那样声明函数:
pipeline {
agent any
stages {
stage('Test') {
steps {
whateverFunction()
}
}
}
}
void whateverFunction() {
sh 'ls /'
}
script { }
中。 - StephenKing/path/to/repo-shared-library/vars/sayHello.groovy:
Content:
def call(String name = 'human') {
echo "Hello, ${name}."
}
@Library('name-of-shared-library')_
pipeline {
agent any
stages {
stage('test') {
steps {
sayHello 'Joe'
}
}
}
}
输出:
[Pipeline] echo
Hello, Joe.
您可以重复使用保存在库中的现有函数。
这对我很有效。可以使用Blue Ocean GUI查看,但是当我使用Blue Ocean GUI进行编辑时,它会删除方法“def showMavenVersion(String a)”。
pipeline {
agent any
stages {
stage('build') {
agent any
steps {
script {
showMavenVersion('mvn version')
}
}
}
}
}
def showMavenVersion(String a) {
bat 'mvn -v'
echo a
}
stage('build')
移动到showMavenVersion
方法中? - MartengMap modules = [:]
pipeline {
agent any
stages {
stage('test') {
steps {
script{
modules.first = load "first.groovy"
modules.first.test1()
modules.first.test2()
}
}
}
}
}
first.groovy
def test1(){
//add code for this method
}
def test2(){
//add code for this method
}
return this
return this
语句吗?为什么需要这个语句? - YoavKlein
YAML
定义了管道,我该如何从YAML
调用 Groovy 脚本? - Mike