简体   繁体   English

如何将文件参数传递给 jenkins 管道中的另一个构建作业?

[英]How to pass a file parameter to another build job in jenkins pipeline?

How can a file from the current workspace be passed as a parameter to a build job, eg:如何将当前工作区中的文件作为参数传递给构建作业,例如:

build job: 'other-project', parameters: [[$class: 'FileParameterValue', ????]]

You can pass the full path of file, you could do:您可以传递文件的完整路径,您可以这样做:

node('master') {
  //Read the workspace path
  String path = pwd();
  String pathFile = "${path}/exampleDir/fileExample.ext";
  //Do whatever you wish with the file path 
}

What a nightmare - there is no documentation, looked into jenkins code, etc.. Tried everything真是一场噩梦 - 没有文档,查看 jenkins 代码等。尝试了一切

Eventually found out that this doesn't currently work.最终发现这目前不起作用。 Here is the jenkins bug.这是詹金斯错误。

https://issues.jenkins-ci.org/browse/JENKINS-27413 https://issues.jenkins-ci.org/browse/JENKINS-27413

Linked to from here: http://jenkins-ci.361315.n4.nabble.com/pipeline-build-job-with-FileParameterValue-td4861199.html从这里链接到: http : //jenkins-ci.361315.n4.nabble.com/pipeline-build-job-with-FileParameterValue-td4861199.html

You need to pass in a FileParameterValue您需要传入一个 FileParameterValue

http://javadoc.jenkins.io/hudson/model/FileParameterValue.html http://javadoc.jenkins.io/hudson/model/FileParameterValue.html

This approach assumes you have the file in the current job's workspace.此方法假定您在当前作业的工作区中有该文件。

pipeline
    {
        agent any
        stages {
            stage('Pass file type param to build job') {
                steps {
                    script {
                        def propertiesFilePath = "${env.WORKSPACE}/sample.properties"
                        build job: 'other-project',
                                parameters: [[$class: "FileParameterValue", name: "propertiesFile", file: new FileParameterValue.FileItemImpl(new File(propertiesFilePath))]]

                    }
                }
            }
        }
    }

Here the name of the downstream/child job is 'other-project' and the name of the file type parameter in this downstream/child job is 'propertiesFile'.此处下游/子作业的名称为“other-project”,此下游/子作业中的文件类型参数名称为“propertiesFile”。 The type FileParameterValue.FileItemImpl is defined in the class FileParameterValue and is internally used in jenkins to handle FileItem, also adding serialization support to the same. FileParameterValue.FileItemImpl类型在类FileParameterValue 中定义,并在 jenkins 内部使用以处理 FileItem,并为其添加序列化支持。

Now you can use the latest File Parameters plugin to implement it.现在您可以使用最新的文件参数插件来实现它。

Here's a simple example:这是一个简单的例子:

test-parent pipeline测试父流水线

pipeline {
    agent any
    parameters {
        base64File(name: 'testFileParent', description: 'Upload file test')
    }
    stages {
        stage('Invoke Child Job') {
            steps {
                withFileParameter('testFileParent') {
                    script{
                        def fileContent = readFile(env.testFileParent)
                        build(job: 'test-child',
                                parameters: [base64File(name: 'testFileChild', base64: Base64.encoder.encodeToString(fileContent.bytes))])
                    }
                }
            }
        }
    }
}

test-child pipeline测试子管道

pipeline {
    agent any
    parameters {
        base64File(name: 'testFileChild', description: 'Upload file test')
    }
    stages {
        stage('Show File') {
            steps {
                withFileParameter('testFileChild') {
                    sh("cat $testFileChild")
                }
            }
        }
    }
}

It works like this:它是这样工作的:

  1. Build test-parent pipeline with parameters, start with a test file使用参数构建测试父管道,从测试文件开始
  2. test-parent pipeline invokes test-child pipeline with the test file which was uploaded at Step 1测试父管道使用在步骤 1 上传的测试文件调用测试子管道
  3. test-child pipeline prints the test file content to console test-child 管道将测试文件内容打印到控制台

暂无
暂无

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

相关问题 如何在作业中设置 Jenkins Build 参数并传递到下一个管道阶段 - How to set Jenkins Build parameter in a job and pass to next pipeline stage 如何将文件上传到 jenkins 管道作业作为构建参数 - How to upload a file to jenkins pipeline job as build parameter 使用Build Pipeline Jenkins插件将参数传递给下游作业 - Pass parameter to downstream job using Build Pipeline Jenkins plugin Jenkins 管道 - 如何读取输入参数,拆分其值并将值传递给构建作业? - Jenkins pipeline - how to read input parameter, split its value and pass the values to build job? Jenkins 带文件参数的管道作业 - Jenkins Pipeline Job with file parameter 如何将构建工件传递到 Jenkins 的另一个工作中 - How to pass build artifact into another job in Jenkins Jenkins - 无法使用“构建作业”命令访问从另一个构建管道中的一个构建管道发送的参数 - Jenkins - Unable to access the parameter sent from one build pipeline in another build pipeline using “build job” command 如何在Jenkins管道中传递选择参数来调用作业 - How to pass choice parameter to call a job inside jenkins pipeline 如何将下游参数(例如当前构建参数)传递给 Jenkins 的下游作业。 如何为相同的脚本编写管道? - How to Pass downstream parameter ( e.g Current build parameter ) to downstream job of Jenkins. How to write Scripted pipeline for the same? 通过DSL构建流程将选择参数传递给Jenkins中的另一个作业 - Pass Choice Parameter to Another Job in Jenkins with DSL Build Flow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM