Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Jenkins2 pipeline to upload via FTP

I'm using the new Jenkins2 pipeline to build a composed project with:

  • node frontend
  • php backend

both are in different repositories hence, the need to use pipeline to sync them, compile, and prepare them to deploy. I cannot find a simple way to deploy using FTP.

My script looks something like this:

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

UPDATE 2016-12-16

To clarify what I need is a way to run something similar to "Publish via FTP" like older versions of Jenkins.

like image 436
genuinefafa Avatar asked Dec 15 '16 22:12

genuinefafa


4 Answers

Since this is one of the top links on google and the other answers don't work, I'll go ahead and add my two cents.

Here is the working pipeline stage that I'm using:

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

The magic here is

  • Set up the External Host in the main Jenkins configuration page under Publish Over FTP and make sure the names match.
  • add the new new required parameters masterNodeName and paramPublish with an empty string and null respectively.

In the publishers block, these settings match what is defined in the old style Jenkins config under transfers, so refer to that for details.

I hope that helps future folks struggling with the ftpPublisher plugin in a Pipeline.

like image 167
Marc Runkel Avatar answered Oct 22 '22 20:10

Marc Runkel


The Jenkins Publish Over FTP plugin has Pipeline support as of version 1.15.

A snippet from my Jenkinsfile that sends some files to our server:

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

I generated this code snippet using the Jenkins snippet generator found under "Pipeline Syntax". Choose "ftpPublisher: send build artifacts over FTP" in the menu at "Sample Step", enter all details in the form and press "Generate Pipeline Script".

like image 26
frankhermes Avatar answered Oct 22 '22 21:10

frankhermes


Install ncftp on the computer and run this command in Jenkins:

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

(Taken from Can I upload an entire folder using FTP? on Super User)

like image 4
Simon Forsberg Avatar answered Oct 22 '22 20:10

Simon Forsberg


I could not get get the Jenkins Publish Over FTP plugin to work at all so I decided to use shell scripts which work. Below is a snippet using lftp. If you do not have that installed, either install it or use 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
        '''
    }
}

This will only send things to FTP if there is a tag in git.

like image 2
Sardathrion - against SE abuse Avatar answered Oct 22 '22 21:10

Sardathrion - against SE abuse