Is lastCompleted() in copyArtifact reliable?

Hello I am beginner in Groovy/Jenkins/JobDSL.
My question is about applying copyArtifact to the results of just finished downstream jobs.
Question is whether lastCompleted() is reliable or not in order to guarantee that I got artifacts
for this specific build. There can be many builds of job A running in parallel and is important that
we get artifacts from the right one.

Unfortunately the legacy code I have to work with does not call JobSDL primitives directly.
Instead there is a local groovy-class API that makes things harder to explain.

There is job A that runs these two Groovy “statements”

buildStep.downstreamParameterized(‘shaGetterJob’)
buildStep.copyArtifacts(‘shaGetterJob’, ‘’, ‘’, false, {lastCompleted()})

The shaGetterJob contains some steps and
publisher.archiveArtifacts(“VisionCore/detectedGitSHAs.csv”, true)
at the end.

    public BuildStep downstreamParameterized(projects, Closure parametersClosure = null, Boolean blocking = true,
                                             Boolean failfast = true, Closure parameterFactoriesClosure = null) {
        job.steps {
            downstreamParameterized {
                trigger(projects) {
                    if (blocking) {
                        block {
                            if (failfast) {
                                buildStepFailure 'FAILURE'
                            }
                            else {
                                buildStepFailure 'never'
                            }
                            failure 'FAILURE'
                            unstable 'UNSTABLE'
                        }
                    }
                    parameters(parametersClosure)
                    parameterFactories(parameterFactoriesClosure)
                }
            }
        }
        return this
    }
	
	
	public BuildStep copyArtifacts(String jobName,
                                   String includeGlob,
                                   String targetPath = '',
                                   Boolean flattenFiles = true,
                                   Closure copyArtifactsClosure = {latestSuccessful()},
                                   String excludeGlob = '')
								   
leads to

        params = [
            targetPath: '',
            flattenFiles: true,
            copyArtifactsClosure: {latestSuccessful()},
            conditionalCommand: null,
            actionIfFailed: 'DontRun',
            parameterFilters: '',
            optional: false,
            excludeGlob: ''] << params
    
		                job.steps {
                    copyArtifacts(jobName) {
                        includePatterns includeGlob
                        targetDirectory params.targetPath
                        flatten params.flattenFiles
                        buildSelector params.copyArtifactsClosure
                        parameterFilters params.parameterFilters
                        optional params.optional
                    }
                }						   
	

    public Publisher archiveArtifacts(patternString, allowEmptyArtifact=false,
        patternExclude="", ifSuccessful=false) {
        job.publishers {
            archiveArtifacts {
                pattern patternString
                allowEmpty allowEmptyArtifact
                exclude(patternExclude)
                onlyIfSuccessful(ifSuccessful)
            }
        }
        return this
    }

I thought jobdsl was about setting up jobs not running them, so I would assume that function only gets called when your jobdsl is run, not when the job that jobdsl creates runs

Yes, the code is for setting up the jobs.

But when my Job A runs there are two things happens in a sequence:

  • invocation(build) of a downstream job, which can take some seconds or some hours;
    it might end with failure;
    this build gets some consequitve number, say #77

  • getting artifact (lastCompleted()) from this individual build of that job.

My concern is whether when “getting artifact” something guarantees that I got
artifact of this specific build ?

It is safe when there is only single user running single A.
But what happens if many users run many A in parallel ?
Will lastCompleted() work correctly ?
Or there is some more reliable method I am missing ?

image
This demonstrates that using the lastCompleted() is a wrong approach. I start one build of job (#6), but receive artifact from another build of that job (#7). Artifact contents shows that it came from #7.