Jenkins Copy Artifact unable to find folder/multiProjectPipeline/branchWithSlash

爷,独闯天下 提交于 2019-12-10 21:14:46

问题


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

  • Folders plugin (6.1.0)
  • Copy Artifact plugin (1.38.1)
  • Pipeline plugin (2.5) + all dependent pipeline sub-plugins
  • Various other dependent 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:

  • playground (a folder created with the Folders plugin to group all these following items)
  • frontend (multibranch pipeline)
  • backend (multibranch pipeline)
  • configure (pipeline with a parameter called BRANCH_NAME)

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.


回答1:


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



回答2:


I created a sample project to try and recreate what you were seeing, and I was able to do so, after a fashion, except that the build that I was having trouble on was master instead of release/2017.2. Eventually, I realized that I was doing the build job incorrectly from the frontend project, and it was giving me the same error as you because I hadn't ever completed a successful build of the frontend/master branch (I had completed a successful build of the release/2017.2 branch because I didn't have it triggering the configure build initially, so it didn't give me the same error once I did configure it to trigger the configure build).

What worked was changing the build job in the frontend Jenkinsfile to this:

build job: 'playground/configure', parameters: [[$class: 'StringParameterValue', name: 'BRANCH_NAME', value: env.BRANCH_NAME]], quietPeriod: 2, wait: false

Adding in the quietPeriod gives a couple seconds of quiet time between completing the previous job (I'm not certain that this is critical, but it seems like it might be a nice fail-safe, to try and make sure there's enough time for the build to complete), but the important part is the wait: false, which instructs Jenkins that this build shouldn't wait for the triggered build to complete. Once I changed that, the frontend/master branch completed successfully, and the configure build that it triggered also completed successfully.

Hopefully this helps. I was able to get both my master and release/2017.2 branches to build properly, so I don't believe there's any intrinsic problem with the / in the project name. You can see my simple Jenkinsfiles in the referenced repo, and I used the same pipeline script as you posted in your question.



来源:https://stackoverflow.com/questions/45377411/jenkins-copy-artifact-unable-to-find-folder-multiprojectpipeline-branchwithslash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!