21

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?

Denis Howe
  • 2,092
  • 1
  • 23
  • 25
sfgroups
  • 18,151
  • 28
  • 132
  • 204

2 Answers2

37

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

zero
  • 371
  • 2
  • 3
8

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)
)
Pit
  • 3,606
  • 1
  • 25
  • 34