Skip to content

Instantly share code, notes, and snippets.

@reitzig
Created April 11, 2023 10:10
Show Gist options
  • Save reitzig/abaad3760c8ac2c6bc7f61898da84a7b to your computer and use it in GitHub Desktop.
Save reitzig/abaad3760c8ac2c6bc7f61898da84a7b to your computer and use it in GitHub Desktop.
Jenkins: stash-like helper functions that use artifacts
void stashAsArtifact(steps, String name, String fileGlob) {
steps.sh([label: "Bundle stash ${name}",
script: "#!/usr/bin/env bash\nshopt -s globstar\ntar --hard-dereference -czf '${name}.tar.gz' ${fileGlob}"])
steps.archiveArtifacts([artifacts: "${name}.tar.gz",
fingerprint: true,
onlyIfSuccessful: true])
}
void unstashFromArtifact(steps, String name) {
steps.unarchive(mapping: ["${name}.tar.gz": "${name}.tar.gz"])
steps.sh([label: "Unbundle stash ${name}",
script: "tar -xzf '${name}.tar.gz' --unlink-first --recursive-unlink && rm '${name}.tar.gz'"])
}
// "Stash" in one stage ...
script {
stashAsArtifact(this, 'node-dependencies', '**/node_modules/')
}
// ... and "unstash" in another
script {
unstashFromArtifact(this, 'node-dependencies')
}
@reitzig
Copy link
Author

reitzig commented Apr 11, 2023

The official docs say:

Note that the stash and unstash steps are designed for use with small files. [...] There's not a hard stash size limit, but between 5-100 MB you should probably consider alternatives.

In my experiments, this didn't perform better. Probably because I don't avoid the main reason stated:

This is because stashed files are archived in a compressed TAR, and with large files this demands considerable on-master resources, particularly CPU time.

So it's definitely a better idea to go another route:

For large data transfers, use the External Workspace Manager plugin, or use an external repository manager such as Nexus or Artifactory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment