Skip to content

Jenkins 在 Pipeline 中上传文件到 FTP

根据 Stack overflow 上的回答,Publish Over FTP 插件自 1.15 版本开始支持 Pipeline

1. 安装 Publish Over FTP 插件

首先要在 Manage Jenkins → Manage Plugins 中安装 Publish Over FTP 插件。

2. 配置 FTP Server

之后需要在 Manage Jenkins → Configure System 页面的 Publish Over FTP 区域添加 FTP Server,其中的 Name 在后面的 Pipeline 脚本中会用到。

3. Pipeline 脚本

最后在脚本中通过 ftpPublisher 方法执行上传操作。

关于脚本的写法可以在 流水线语法 页面选择 ftpPublisher: Send build artifacts over FTP 示例自动生成脚本。

需要注意的有以下几点:

  • masterNodeNameparamPublish 参数是必须的,自动生成的脚本中不知道为什么不含这两个参数;
  • publishers → configName 值为第二步中设置的 FTP Server Name
  • remoteDirectory 指定上传的远端目录
  • sourceFiles 指定需要上传的文件
  • removePrefix 设置为需要过滤掉的文件前缀
    • 下面的示例中会自动将 OctopusSoa/OctopusApi/target/ 目录下的所有文件上传到 FTP 服务器的 server 目录下。
    • 如果 removePrefix 为空,则会上传到 server/OctopusSoa/OctopusApi/target/ 目录下。
groovy
stage('Upload') {
    steps {
        ftpPublisher    alwaysPublishFromMaster: false,
                        continueOnError: false,
                        failOnError: false,
                        masterNodeName: '',
                        paramPublish: null,
                        publishers: [[
                            configName: 'SOA-D',
                            transfers: [[
                                asciiMode: false,
                                cleanRemote: false,
                                excludes: '',
                                flatten: false,
                                makeEmptyDirs: false,
                                noDefaultExcludes: false,
                                patternSeparator: '[, ]+',
                                remoteDirectory: 'server',
                                remoteDirectorySDF: false,
                                removePrefix: 'OctopusSoa/OctopusApi/target',
                                sourceFiles: 'OctopusSoa/OctopusApi/target/*.jar'
                            ]],
                            usePromotionTimestamp: false,
                            useWorkspaceInPromotion: false,
                            verbose: false
                        ]]
    }
}

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.