我有一个共享库,它接受我设置的将文件压缩为tar的参数。詹金斯皮林是这样的。
stage("Package"){
steps{
compress_files("arg1", "arg2")
}
}
共享库compress_file看起来像这个
#!/usr/bin/env groovy
// Process any number of arguments.
def call(String... args) {
sh label: 'Create Directory to store tar files.', returnStdout: true,
script: """ mkdir -p "$WORKSPACE/${env.PROJECT_NAME}" """
args.each {
sh label: 'Creating project directory.', returnStdout: true,
script: """ mkdir -p "$WORKSPACE/${env.PROJECT_NAME}" """
sh label: 'Coping contents to project directory.', returnStdout: true,
script: """ cp -rv ${it} "$WORKSPACE/${env.PROJECT_NAME}/." """
}
sh label: 'Compressing project directory to a tar file.', returnStdout: true,
script: """ tar -czf "${env.PROJECT_NAME}.tar.gz" "${env.PROJECT_NAME}" """
sh label: 'Remove the Project directory..', returnStdout: true,
script: """ rm -rf "$WORKSPACE/${env.PROJECT_NAME}" """
}
新的要求是使用数组,而不是更新参数值。我们如何或可以在jenkinsfile阶段中传递arrayname
是的,可以从Jenkinsfile中定义stage((内部或stage(
在声明性管道中:
def files = ["arg1", "arg2"] as String[]
pipeline {
agent any
stages {
stage("Package") {
steps {
// script is optional
script {
// you can manipulate the variable value of files here
}
compress_files(files)
}
}
}
}
在脚本管道中:
node() {
//You can define the value here as well
// def files = ["arg1", "arg2"] as String[]
stage("Package"){
def files = ["arg1", "arg2"] as String[]
compress_files(files)
}
}
在共享库中,方法将类似
// var/compress_files.groovy
def call(String[] args) {
args.each {
// retrive the value from ${it} and proceed with your logic
}
}
或
def call(String... args) {
args.each {
// retrive the value from ${it} and proceed with your logic
}
}