0

I trying to use Copy Artifacts plugin of Jenkins, want to get build number that archive copied from and use it for later pipeline script.

parameters {
    buildSelector(
        name: 'LAST_ARCHIVED_BUILD',
        defaultSelector: lastCompleted(),
        description: 'Build for copy artifact from')

...

copyArtifacts(
    projectName: "${JOB_NAME}",
    selector: ${LAST_ARCHIVED_BUILD},
    filter: "report_log_${LAST_ARCHIVED_BUILD}_dump.json"
)

...

sh( """
    jq --sort-keys . report_log_${LAST_ARCHIVED_BUILD}_dump.json > last.json
    jq --sort-keys . report_log_${BUILD_NUMBER}_dump.json > today.json
    diff last.json today.json > diff_result.txt
    """
)
...

But it seems this LAST_ARCHIVED_BUILD have a string like :

LAST_ARCHIVED_BUILD=<LastCompletedBuildSelector plugin="copyartifact@1.47"/> (for lastCompleted())
or
LAST_ARCHIVED_BUILD=<SpecificBuildSelector plugin="copyartifact@1.47">  <buildNumber>526</buildNumber></SpecificBuildSelector> (for specific build)

Is there any way to get a build number from this BuildSelector or Copy Artifact plugin?

Rivian
  • 45
  • 1
  • 6

1 Answers1

0

Answer myself :

It seems that currently there's no way to get build number of artifacts copied directly from plugin in Jenkins pipeline. There's COPYARTIFACT_BUILD_NUMBER_SUFFIX variable for this purpose but it cannot used in pipeline scripts.

https://issues.jenkins.io/browse/JENKINS-34620?src=confmacro

So I used a workaround in this post's answer : create a text file contains current build number, archive it in artifact and retrieve it. Jenkins Copy Artifact parse copied build id

copyArtifacts(
    projectName: "${JOB_NAME}",
    selector: buildParameter('LAST_ARCHIVED_BUILD'),
    filter: "report_log*.json, artifact_build_number.txt",
)

...

sh( """
    jq --sort-keys . report_log_`cat artifact_build_number.txt`_dump.json > last.json
    jq --sort-keys . report_log_${BUILD_NUMBER}_dump.json > today.json
    diff last.json today.json > diff_result.txt
    """
)
sh(
    "echo ${BUILD_NUMBER} > artifact_build_number.txt "
)
archiveArtifacts(
    artifacts: "report_log_${BUILD_NUMBER}_dump.json, artifact_build_number.txt, diff_result.txt"
)

This works for my purpose.

Rivian
  • 45
  • 1
  • 6