Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-56766

Allow preserved stashes to be used in new run of the job


    • Similar Issues:


      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.


      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' }
              echo "BUILDING"





          fnasser Fernando Nasser created issue -


            • Assignee:
              fnasser Fernando Nasser
            • Votes:
              1 Vote for this issue
              2 Start watching this issue


              • Created: