Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins CopyArtifact step - Unable to find project for artifact copy

Based on this post trying to test the pipe line code in my enviroment. But its giving below error message. how to fix his pipeline code?

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

How can I use the Jenkins Copy Artifacts Plugin from within the pipelines (jenkinsfile)?

pipeline {
    agent any
    stages {
        stage ('push artifact') {
            steps {
                sh '[ -d archive ] || mkdir archive'
                sh 'echo test > archive/test.txt'
                sh 'rm -f test.zip'
                zip zipFile: 'test.zip', archive: false, dir: 'archive'
                archiveArtifacts artifacts: 'test.zip', fingerprint: true
            }
        }

        stage('pull artifact') {
            steps {
                sh 'pwd'
                sh 'ls -l'
                sh 'env'
                step([  $class: 'CopyArtifact',
                        filter: 'test.zip',
                        projectName: '${JOB_NAME}',
                        fingerprintArtifacts: true,
                        selector: [$class: 'SpecificBuildSelector', buildNumber: '${BUILD_NUMBER}']
                ])
                unzip zipFile: 'test.zip', dir: './archive_new'
                sh 'cat archive_new/test.txt'
            }
        }
    }
}
like image 489
sfgroups Avatar asked Feb 16 '18 20:02

sfgroups


People also ask

Under which section is the copy artifacts option present in Jenkins?

Next you need to install the Copy Artifact plugin in the Manage Plugins section of Jenkins. Go to "[PROJECT-NAME]-Output" > configure and add a new build step. Because you have installed the Copy Artifact plugin you should see an option called 'copy artifacts from another project' in the drop down menu.

How do I add an artifact to Jenkins?

How to Create an Artifact. In Jenkins, an artifact is created in either Freestyle projects or Pipeline projects. In Freestyle, add the “Archive the artifacts” post-build step. In Pipeline, use the archiveArtifacts step.


2 Answers

If you enable authorization(like rbac), you must grant permission 'Copy Artifact' to the project. In project configuration, General -> Permission to Copy Artifact, check the box and set the projects that can copy the artifact

like image 54
zero Avatar answered Sep 20 '22 15:09

zero


Rather than using projectName: '${JOB_NAME}', what worked for me is using projectName: env.JOB_NAME. I.e. your complete copy-artifacts step would look like this:

step([  $class: 'CopyArtifact',
        filter: 'test.zip',
        projectName: env.JOB_NAME,
        fingerprintArtifacts: true,
        selector: [$class: 'SpecificBuildSelector', buildNumber: env.BUILD_NUMBER]
])

Or using the more modern syntax:

copyArtifacts(
    filter: 'test.zip',
    projectName: env.JOB_NAME,
    fingerprintArtifacts: true,
    selector: specific(env.BUILD_NUMBER)
)
like image 44
Pit Avatar answered Sep 20 '22 15:09

Pit