sfgroups
sfgroups

Reputation: 19109

Jenkins CopyArtifact step - Unable to find project for artifact copy

Based on this post, I'm trying to test this pipeline code in my environment:

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

but it gives the error message:

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 fix his pipeline code?

Upvotes: 25

Views: 27322

Answers (3)

Lenormju
Lenormju

Reputation: 4368

It could also be that the project is in a workspace, so that its full name include a potential leading slash, i.e. /my-workspace/my-project.

Upvotes: 0

Pit
Pit

Reputation: 4046

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

Upvotes: 10

zero
zero

Reputation: 441

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

Upvotes: 44

Related Questions