You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Pipeline中如何将文件作为参数在流水线步骤中传递

Jenkins Pipeline跨任务传递文件参数的实现方案

方案1:小文件直接编码为字符串参数传递

适合传递10MB以内的小文件,实现逻辑最简单

  • 上游流水线读取目标文件转base64编码,作为字符串参数传给下游任务
  • 下游任务接收参数后解码回写为本地文件即可正常使用

你可以修改现有代码如下,以给MQTask_Publisher传递文件为例:

pipeline {
    agent { node { label 'abc' } }
    stages {
        stage('Running for : MQ Injection') {
            steps {
                echo 'Injecting Messages in dev'
                script {
                    // 读取本地文件并转base64编码,替换为你实际的文件路径
                    def fileContent = readFile encoding: 'Base64', file: './local_file_path.txt'
                }
                build job: 'MQTask_Publisher', parameters: [
                    string(name: 'mq.host', value: mqhost), 
                    string(name: 'mq.port', value: mqport), 
                    string(name: 'mq.manager', value: mqmanager), 
                    string(name: 'mq.channel', value: mqchannel),
                    string(name: 'mq.queue', value: mqqueue),
                    string(name: 'mq.wait.seconds', value: mqwaitseconds),
                    string(name: 'fileType', value: fileType),
                    // 新增编码后的文件内容参数、文件名参数
                    string(name: 'transferFileBase64', value: fileContent),
                    string(name: 'transferFileName', value: 'target_file.txt')
                ]
            }
        } 
        stage('Running for : MQ Message validation') {
            steps {
                echo 'Validating messages created from MQ'
                build job: 'MQInjection_Validation', parameters: [
                    string(name: 'Environment', value: env), 
                    string(name: 'TRN', value: trn), 
                    string(name: 'Min Date', value: minDate), 
                    string(name: 'Max Date', value: maxDate), 
                    string(name: 'Creation time', value: time)
                ]
            }
        }
    }
}

下游任务MQTask_Publisher的流水线开头增加解码逻辑即可拿到原文件:

script {
    writeFile encoding: 'Base64', file: params.transferFileName, text: params.transferFileBase64
}

方案2:大文件通过构件归档传递

如果文件体积超过10MB,base64编码会导致参数过长,用归档构件的方式更稳妥:

  • 上游任务生成文件后,用archiveArtifacts命令归档待传递的文件
  • 下游任务开头用copyArtifacts命令从上游任务的对应构建记录拉取归档的文件

上游任务新增归档步骤示例:

stage('归档待传递文件') {
    steps {
        archiveArtifacts artifacts: './local_file_path.txt', fingerprint: true
    }
}

下游任务拉取文件步骤示例:

stage('拉取上游传递的文件') {
    steps {
        copyArtifacts filter: 'target_file.txt', projectName: '上游任务名称', selector: upstream(fallbackToLastSuccessful: true)
    }
}

内容的提问来源于stack exchange,提问作者user2452090

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:24:04