Home > Net >  Jenkins: unable to access the artifacts on the initial run
Jenkins: unable to access the artifacts on the initial run

Time:02-20

My setup: main node runs on Linux and an agent on Windows. I want to compile a library on an agent, archive those artifacts and copy them on the main node to create a release togather with the Linux compiled binaries.

This is my Jenkinsfile:

pipeline {
    agent none
    stages {
        stage('Build-Windows') {
            agent {
                dockerfile {
                    filename 'docker/Dockerfile-Windows'
                    label 'windows'
                }
            }
            steps {
                bat "tools/ci/build.bat"
                archiveArtifacts artifacts: 'build_32/bin/mylib.dll'
            }
        }
    }
    post {
        success {
            node('linux') {
                copyArtifacts filter: 'build_32/bin/mylib.dll', flatten: true, projectName: '${JOB_NAME}', target: 'Win32'
            }
        }
    }
}

My problem is, when I run this project for the first time, I get the following error

Unable to find project for artifact copy: mylib

But when I comment the copyArtifacts block and rerun the project, it is successful and I have artifacts vivible in the project overview. After this I can reenable the copyArtifacts and then the artifacts will be copied as expected.

How to configure the pipeline so it can access the artifacts on the initial run?

CodePudding user response:

The copyArtifacts capability is usually used to copy artifacts between different builds and not between agents on the same build. Instead, to achieve what you want you can use the stash and unstash keywords which are designed exactly for passing artifacts from different agents in the same pipeline execution:

stash: Stash some files to be used later in the build.
Saves a set of files for later use on any node/workspace in the same Pipeline run. By default, stashed files are discarded at the end of a pipeline run

unstash: Restore files previously stashed.
Restores a set of files previously stashed into the current workspace.

In your case it can look like:

pipeline {
    agent none
    stages {
        stage('Build-Windows') {
            agent {
                dockerfile {
                    filename 'docker/Dockerfile-Windows'
                    label 'windows'
                }
            }
            steps {
                bat "tools/ci/build.bat"
                // dir is used to control the path structure of the stashed artifact
                dir('build_32/bin'){
                    stash name: "build_artifact" ,includes: 'mylib.dll'
                }
            }
        }
    }
    post {
        success {
            node('linux') {
                // dir is used to control the output location of the unstash keyword
                dir('Win32'){
                   unstash "build_artifact"
            }
        }
    }
}
  • Related