Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins : Copy artifacts from Multibranch pipeline

I am new to Jenkin's and I have 4 repo's in Bitbucket say A,B,C,D. I have to fetch the A,B & C repos, build them using gradle build which will generate wars. Now I have to copy those wars in D\warsFolder I have created Multibranch pipeline and generated the pipeline syntax which fetches A,B & C from git and builds them. Looks some thing like this

    node {
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'A']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../A.git']]])
    dir('A') {
        bat 'gradle build -i --info --stacktrace --debug'
    }
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'B']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../B.git']]])
    dir('B') {
        bat 'gradle build -i --info --stacktrace --debug'
    }
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'C']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'id', url: 'http://.../C.git']]])
    dir('C') {
        bat 'gradle build -i --info --stacktrace --debug'
    }

    }

added the above script in Jenkinsfile which I placed in A repo.

Now I have created a Multibranch pipeline Fetch_all and in branch sources -> Single repository & branch -> Repository URL I have added http://.../A.git (which has Jenkinsfile). Upto here everything is working fine I am able to fetch the sources and build them.

I have created new job of Freestyle where in Source Code Management -> Git -> Repository URL will be http://.../D.git. I am trying to copy the wars generated in the Fetch_all pipeline but in Build -> Copy artifacts from another project the Project Name is not accepting the Multibranch pipeline. It is throwing error like

ERROR: Unable to find project for artifact copy: 
This may be due to incorrect project name or permission settings; see help for project name in job configuration.

Any help is appreciated.

like image 785
Navakanth Avatar asked Apr 03 '17 13:04

Navakanth


2 Answers

Finally got it, when I gave pipeline_name/branchname i.e., Fetch_all/%00 it worked fine.

like image 192
Navakanth Avatar answered Oct 06 '22 16:10

Navakanth


It took some time to find out the correct syntax. The documentations of the Coyartifact Plugin is a little bit confusing, as it mentions the encoding of special characters. Actually spaces don't have to be encoded, but slashes have to.

The Jenkinsfile which copies artifacts is located at 'Other-folder/Multi branch Pipeline Test/', put in this content to copy the artifact of the last successfull build of the 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy' project

copyArtifacts(
    projectName: 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy',// the name of project, as you find it from the root of jenkins
    selector: lastSuccessful(),             // selector to select the build to copy from. If not specified, latest stable build is used.
    filter: 'projects/Output/myzip.zip',    // ant-expression to filter artifacts to copy, Attention! Filter is case sensitive
    target: 'sources/deploy/',              // target directory to copy to, intermediate folders will be created
    flatten: true,                          // ignore directory structures of artifacts, Artifact will be placed at 'sources/deploy/myzip.zip'. Is the option false, you find it at 'projects/Outpu/myzip.py'
    optional: false,                        // do not fail the step even if no appropriate build is found.
    fingerprintArtifacts: true,             // fingerprint artifacts to track builds using those artifacts
)

And don't forget to allow artifact copy in the project you want to take the artifact from. Add this to the Jenkinsfile of 'Folder/Multi branch Pipeline/feature%2Fallow-artifact-copy'. Use absolute paths, to avoid issues if you move some projects around.

options {
     disableConcurrentBuilds()
     timeout(time: 30, unit: 'MINUTES')
     copyArtifactPermission('/Other-folder/Multi branch Pipeline Test/*, /second Folder/*') // allow all the projects or branches of 'Other-folder/Multi branch Pipeline Test' and 'second Folder' to copy artifacts of this job
} // end of options
like image 27
brainelectronics Avatar answered Oct 06 '22 16:10

brainelectronics