使用 Jenkins2 管道通过 FTP 上传

using Jenkins2 pipeline to upload via FTP

我正在使用新的 Jenkins2 管道构建一个组合项目:

两者都在不同的存储库中,因此需要使用管道来同步它们、编译它们并准备它们进行部署。我找不到使用 FTP.

进行部署的简单方法

我的脚本看起来像这样:

node {
    // uncomment these 2 lines and edit the name 'node-4.4.5' according to what you choose in configuration
    def nodeHome = tool name: 'NodeJS 7.2.1', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
    env.PATH = "${nodeHome}/bin:${env.PATH}"

    stage("front") {
        dir('front') { // switch to subdir
            git url: ...             
            sh "npm install"

            sh "npm run build --prod"

            sh "cp -R * ../dist"
        }
    }

    stage("back") {
        dir('back') {
            git url: ...

            sh 'curl -sS https://getcomposer.org/installer | php'
            sh 'php composer.phar install'

            sh "cp -R * ../dist"
        }
    }
    stage("upload via ftp") {
        // IM NOT SURE WHAT TO DO HERE
    }
}

更新 2016-12-16

为了阐明我需要的是 运行 类似于 "Publish via FTP" 的旧版本 Jenkins。

所以你的问题是如何使用Linux命令行通过ftp上传文件? 我认为是 already solved here

$ curl -T my-local-file.txt ftp://ftp.example.com --user user:secret

在计算机上安装 ncftp 并在 Jenkins 中 运行 此命令:

ncftpput -R -v -u "ftp-username" ftp.website.com ftp-upload-path local-path/*

(摘自超级用户 Can I upload an entire folder using FTP?

Jenkins Publish Over FTP 插件从 1.15 版本开始支持管道。

我的 Jenkinsfile 中的一个片段,它向我们的服务器发送了一些文件:

stage('Upload')
{
    ftpPublisher alwaysPublishFromMaster: true, continueOnError: false, failOnError: false, publishers: [
        [configName: 'YOUR_CONFIG_HERE', transfers: [
            [asciiMode: false, cleanRemote: false, excludes: '', flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "YOUR_DIRECTORY_HERE", remoteDirectorySDF: false, removePrefix: '', sourceFiles: '**.exe, **.txt']
        ], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true]
    ]
}

我使用“管道语法”下的 Jenkins 片段生成器生成了此代码片段。在“示例步骤”的菜单中选择“ftpPublisher:通过 FTP 发送构建工件”,在表单中输入所有详细信息,然后按“生成管道脚本”。

我无法让 Jenkins Publish Over FTP 插件正常工作,所以我决定使用有效的 shell 脚本。下面是使用 lftp 的片段。如果您没有安装它,请安装它或使用 vanilla ftp.

stage('FTP') {
    steps {
        sh '''if git describe --exact-match --tags HEAD; then
            lftp ftp://USER:PWD@FTP -e "cd PATH; mput *.exe; bye"
        else
            exit 0
        fi
        '''
    }
}

如果 git 中有标签,这只会将内容发送到 FTP。

由于这是 google 上最热门的链接之一,而其他答案无效,我将继续并添加我的两分钱。

这是我正在使用的工作管道阶段:

stage ('Deploy') {
  steps {
    ftpPublisher alwaysPublishFromMaster: true,
                 continueOnError: false,
                 failOnError: false,
                 masterNodeName: '',
                 paramPublish: null,
                 publishers: [[configName: 'External Host', transfers: [[asciiMode: false, cleanRemote: true, excludes: '', flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'public', sourceFiles: 'public/*,public/**/*']], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false]]
  }

这里的神奇之处在于

  • 在主 Jenkins 配置页面的“发布”下设置外部主机 FTP 并确保名称匹配。
  • 分别使用空字符串和 null 添加新的必需参数 masterNodeName 和 paramPublish。

在 publishers 块中,这些设置与旧式 Jenkins 配置中 transfers 下的定义相匹配,因此请参阅它了解详细信息。

我希望这能帮助未来在管道中使用 ftpPublisher 插件的人。

注意:我添加这个答案是为了总结对@frankhermes 的答案的许多有用评论。不幸的是,我无法将它们添加到他的回答中,因为“编辑队列已满”。


Publish Over FTP Jenkins 插件从 1.15 版本开始支持流水线。

要创建管道,您可以使用 Jenkins 片段生成器。为此

  1. 进入你的管道项目
  2. 在左侧菜单中,单击管道语法
  3. select ftpPublisher:从下拉列表
  4. 发送构建工件到 FTP
  5. 像在经典 Jenkins 作业中那样配置所有细节
  6. 生成管道脚本

这会为您生成管道代码。

但是,由于缺少参数,我在使用这段代码时遇到了错误。我必须添加 masterNodeName: ''paramPublish: [parameterName:""] 作为额外的顶级参数。

插件的管道语法记录在 https://www.jenkins.io/doc/pipeline/steps/publish-over-ftp/

这里有一个完整的示例,说明了使用插件版本 1.16 上传构建工件(jar 文件)的方法:

          steps {
            ftpPublisher alwaysPublishFromMaster: false, 
                    masterNodeName: '',
                    paramPublish: [ parameterName: "" ],
                    continueOnError: false, 
                    failOnError: false, 
                    publishers: 
                    [ [
                        configName: 'download.dev.localnet (Local Download Server)', 
                        transfers: 
                        [
                            [
                                asciiMode: false, 
                                cleanRemote: false, 
                                excludes: '**/*-tests.jar, **/*-javadoc.jar, **/*.lite*, **/*.common*', 
                                flatten: true, 
                                makeEmptyDirs: false, 
                                noDefaultExcludes: false, 
                                patternSeparator: '[, ]+', 
                                remoteDirectory: '/utilities/project/snapshots', 
                                remoteDirectorySDF: false, 
                                removePrefix: '', 
                                sourceFiles: '**/target/*-SNAPSHOT.jar'
                             ] 
                     ], 
                     usePromotionTimestamp: false, 
                     useWorkspaceInPromotion: false, 
                     verbose: true
                 ] ]      
            }       

@MarcRunkel 的回答中的另一个有用说明:

Publish Over FTP 下的主 Jenkins 配置页面中设置 External Host 然后确保使用configName 参数中的确切名称。