jenkinsjenkins-pipelinejenkins-2

Jenkins Copy Artifact unable to find folder/multiProjectPipeline/branchWithSlash


I have Jenkins LTS 2.60.2 on Windows Server 2016 and using these plugins:

See Pipeline to use artifacts from 2 projects associated by the same git branch name for more details about my setup, but to sum it up I have these items:

The frontend and backend git repos, both have a branch called master and one called release/2017.2.

The idea is to call the configure pipeline automatically after each successful build, passing the git branch name. Automatically triggering the configure pipeline works.

What doesn't work and I need your help to fix, is the step inside the configure pipeline to copy the artifacts from a multibranchPipeline/specificBranch.

If for the BRANCH_NAME parameter (or the upstream pipeline) is master it works. If BRANCH_NAME is: release/2017.2 I get this error:

ERROR: Unable to find project for artifact copy: playground/frontend/release%2f2017.2 This may be due to incorrect project name or permission settings; see help for project name in job configuration. Finished: FAILURE

The configure pipeline looks like this:

node {
  stage('Prepare') {
      def projectname = "playground/frontend/" + "${BRANCH_NAME}".replace("/", "%2f")
      step([$class: 'CopyArtifact', projectName: "${projectname}", selector: [$class: 'StatusBuildSelector', stable: false]])
  }
  stage('Archive') {
      archiveArtifacts '**'
  }
}

As you can see I already replace / with %2f (it's needed).

If I don't use the "playground" folder (all my pipelines as is, not inside a folder item), it works. If I use the folder and use the master branch, it works. It doesn't work if I use the folder and a branch name like 2017.2. What am I doing wrong? Can you help making it work? Of well if it's a bug (I searched in https://issues.jenkins-ci.org and found some bugs where a similar setup with folder doesn't work, but they have been fixed... so I really wonder...) in the copy artifact plugin, please file the bug and share the link here, so we can all monitor its progress...

Thank you.


Solution

  • I finally found the issue. The configure pipeline was failing to find a branch with a slash because the encoding was incorrect.

    So, in my question, in the configure pipeline:

    this (replace / with %2f) is wrong and generates the error:

    def projectname = "playground/frontend/" + "${BRANCH_NAME}".replace("/", "%2f")
    

    this is the proper way to encode the slash, and it works:

    def projectname = "playground/frontend/" +  URLEncoder.encode("${BRANCH_NAME}", "UTF-8").replace("+", "%20")
    

    Credits to: http://www.pipegrep.se/copy-artifacts-from-jenkins-pipeline-jobs.html

    UPDATE: actually, I investigated a bit further and added echo "${projectname}" just before step, with the previous and fixed projectname, and I noticed that the difference was %2f lowercase.

    Uppercase, like this: %2F works:

    def projectname = "playground/frontend/" + "${BRANCH_NAME}".replace("/", "%2F")
    

    So, the fixed configure pipeline looks like this (I kept my replace function, which was enough for my case):

    node {
      stage('Prepare') {
          def projectname = "playground/frontend/" + "${BRANCH_NAME}".replace("/", "%2F")
          step([$class: 'CopyArtifact', projectName: "${projectname}", selector: [$class: 'StatusBuildSelector', stable: false]])
      }
      stage('Archive') {
          archiveArtifacts '**'
      }
    }