在詹金斯Pipelie中将数组作为参数传递

巴斯克·皮耶托

我有一个共享库,可以接受我设置的参数以将文件压缩到tar中。jenkinspipline看起来像这样。

  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阶段传递数组名

萨米特·库马尔·帕特尔

是的,有可能,从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
   }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从詹金斯传递MAVEN配置文件参数

来自分类Dev

通过詹金斯传递参数到shell脚本的问题

来自分类Dev

詹金斯传递Active Choices参数的值

来自分类Dev

从詹金斯传递MAVEN配置文件参数

来自分类Dev

詹金斯版本作为管道变量?

来自分类Dev

詹金斯参数化矩阵作业

来自分类Dev

如何阅读詹金斯参数?

来自分类Dev

詹金斯分组

来自分类Dev

在Powershell中将数组作为参数传递?

来自分类Dev

在C ++中将数组作为函数参数传递

来自分类Dev

在TypeScript中将数组作为参数传递

来自分类Dev

在golang中将数组作为参数传递

来自分类Dev

在sqlite Swift中将数组作为参数传递

来自分类Dev

无法在Java中将数组作为参数传递

来自分类Dev

在Python中将数组作为参数传递

来自分类Dev

在TypeScript中将数组作为参数传递

来自分类Dev

在C中将多维数组作为参数传递

来自分类Dev

在Kendo UI中将数组作为参数传递

来自分类Dev

在php中将数组作为参数传递

来自分类Dev

在PowerShell中将数组作为参数传递

来自分类Dev

在C中将数组作为参数传递

来自分类Dev

在 PHP 中将数组作为函数的参数传递

来自分类Dev

在 Thymeleaf 片段中将数组作为参数传递

来自分类Dev

以令牌形式在詹金斯中传递git hub凭证

来自分类Dev

如何在詹金斯中找到按参数构建?

来自分类Dev

具有动态参数的詹金斯管道

来自分类Dev

如何创建参数化的詹金斯职位?

来自分类Dev

詹金斯::动态字符串参数

来自分类Dev

詹金斯奴隶比詹金斯大师慢