如何在Jenkins Pipeline中将文件参数传递给另一个构建任务?

5

如何将当前工作区的文件作为参数传递给构建作业,例如:

build job: 'other-project', parameters: [[$class: 'FileParameterValue', ????]]
4个回答

3

2

如果您要传递文件的完整路径,可以这样做:

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

1

现在您可以使用最新的文件参数插件来实现它。

这里有一个简单的例子:

test-parent管道

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))])
                    }
                }
            }
        }
    }
}

测试子管道

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

它的工作原理如下:

  1. 使用参数构建test-parent管道,从测试文件开始
  2. test-parent管道调用上传在第1步的测试文件的test-child管道
  3. test-child管道将测试文件内容打印到控制台

0

这种方法假设您在当前作业的工作空间中拥有该文件。

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))]]

                    }
                }
            }
        }
    }

这里下游/子作业的名称是“other-project”,此下游/子作业中文件类型参数的名称为“propertiesFile”。 类型FileParameterValue.FileItemImpl定义在类FileParameterValue中,在Jenkins内部用于处理FileItem,同时添加序列化支持。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接