jenkinsci / jenkinsci/workflow-basic-steps-plugin

[JENKINS-56766] Allow preserved stashes to be used in new run of the job

Aperta
#486 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
component:workflow-basic-steps-plugin imported-jira-issue priority:minor resolution:unresolved
Lingua principale
Java
Stelle
73
Fork
129
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Ref.: https://jenkins.io/doc/book/pipeline/running-pipelines/#preserving-stashes-for-use-with-restarted-stages

Currently we can request that a stash be kept after a job finishes, even for more than one job (why? it is not clear from the text above how this would be useful. Multiple restarts perhaps?). But it is only available if we restart a stage of a job (so I guess it must have failed). At least is what the above text seems to imply and some empirical data confirms.

As we are storing the stash tar balls anyway, why not make them available for the next job that starts regularly (as opposed to with a restart)? That way we could store some data to be tested on the next run and make a decision based on it.

Possible use cases would be to store something like the last commitId tested so you don't run the tests again for the same code and any other things about the last run that would be useful for the next run.

E.g.:

pipeline {

agent {
node {
label 'centos'
}
}
options {
preserveStashes(buildCount: 5)
}
environment {
CHANGED = 'False'
}
stages {
stage('Check for changes'){
agent {
node {
label 'rhel'
}
}
steps {
script {
def cmd = "git ls-remote https://xxx.yyyy.zzz.com/mycode.git master | awk '{print \$1}' | tee last-image-build.txt.new"
echo "cmd: ${cmd}"
def newcommitid = sh (returnStdout: true, script: "${cmd}")
echo "newcommitid: ${newcommitid}"
try {
unstash 'last-image-build'
} catch (Exception e) {
echo "last-image-build stash not found, assume changed"
CHANGED = 'True'
}
if (fileExists('last-image-build.txt')) {
def oldcommitid = readFile 'last-image-build.txt'
echo "oldcommitid: ${oldcommitid}"
if (oldcommitid != newcommitid) {
CHANGED = 'True'
echo "CHANGED new value: ${CHANGED}"
}
}
sh 'mv -f last-image-build.txt.new last-image-build.txt'
stash name: 'last-image-build', includes: 'last-image-build.txt', allowEmpty:true
echo "CHANGED: ${CHANGED}"
}
}
}
stage('Build only if changed'){
when {
expression { environment name: 'CHANGED', value: 'True' }
}
steps{
echo "BUILDING"
}
}
}
}

 

 

---
Originally reported by fnasser, imported from: Allow preserved stashes to be used in new run of the job


  • status: Open
  • priority: Minor
  • component(s): workflow-basic-steps-plugin
  • resolution: Unresolved
  • votes: 1
  • watchers: 3
  • imported: 20251215-220547

Raw content of original issue

Ref.: https://jenkins.io/doc/book/pipeline/running-pipelines/#preserving-stashes-for-use-with-restarted-stages

Currently we can request that a stash be kept after a job finishes, even for more than one job (why? it is not clear from the text above how this would be useful. Multiple restarts perhaps?). But it is only available if we restart a stage of a job (so I guess it must have failed). At least is what the above text seems to imply and some empirical data confirms.

As we are storing the stash tar balls anyway, why not make them available for the next job that starts regularly (as opposed to with a restart)? That way we could store some data to be tested on the next run and make a decision based on it.

Possible use cases would be to store something like the last commitId tested so you don't run the tests again for the same code and any other things about the last run that would be useful for the next run.

E.g.:



pipeline {

agent {
node {
label 'centos'
}
}
options {
preserveStashes(buildCount: 5)
}
environment {
CHANGED = 'False'
}
stages {
stage('Check for changes'){
agent {
node {
label 'rhel'
}
}
steps {
script {
def cmd = "git ls-remote https://xxx.yyyy.zzz.com/mycode.git master | awk '{print \$1}' | tee last-image-build.txt.new"
echo "cmd: ${cmd}"
def newcommitid = sh (returnStdout: true, script: "${cmd}")
echo "newcommitid: ${newcommitid}"
try {
unstash 'last-image-build'
} catch (Exception e) {
echo "last-image-build stash not found, assume changed"
CHANGED = 'True'
}
if (fileExists('last-image-build.txt')) {
def oldcommitid = readFile 'last-image-build.txt'
echo "oldcommitid: ${oldcommitid}"
if (oldcommitid != newcommitid) {
CHANGED = 'True'
echo "CHANGED new value: ${CHANGED}"
}
}
sh 'mv -f last-image-build.txt.new last-image-build.txt'
stash name: 'last-image-build', includes: 'last-image-build.txt', allowEmpty:true
echo "CHANGED: ${CHANGED}"
}
}
}
stage('Build only if changed'){
when {
expression { environment name: 'CHANGED', value: 'True' }
}
steps{
echo "BUILDING"
}
}
}
}


 

 

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.