junebob
junebob

Reputation: 467

Preserve Jenkins workspace

I have Jenkins set up to run concurrent builds, so I end up with workspace, workspace@2, workspace@3, etc. If Jenkins thinks the build is finished, a new build will overwrite the workspace. Is there a way of occasionally preventing that? E.g. Don't overwrite workspace@3 until I say. We have various scenarios where this would be very useful.

Upvotes: 8

Views: 30472

Answers (4)

You can define the agent in the hole pipeline, so the jenkins will use only the "workspace" dir.

For example (this is a NOT WORKING example):

pipeline {
    agent any 
    environment {
        // Environment variables
    }    
    stages {
        stage('Clear dir') {
            steps {
                deleteDir()
            }
        }
        stage('Make checkout') {
            agent any    // **THIS IS WRONG!!!!** 
            steps {
                echo 'Making checkout'
            }
        }
    } 
}

In the previous example, the "agent any" inside the stage will allow jenkins to create a "workspace@2" folder.

To prevent this, leave the agent only in the pipeline. Correct example:

pipeline {
    agent any   // This is right! leave only this mention of agent 
    environment {
        // Environment variables
    }    
    stages {
        stage('Clear dir') {
            steps {
                deleteDir()
            }
        }
        stage('Make checkout') {
            steps {
                echo 'Making checkout'
            }
        }
    } 
}

Upvotes: 4

Antti
Antti

Reputation: 1118

This answer comes a bit late and probably not totally correct answer to your question, but this is the first ticket which I found when searching solution for my problem.

I had problem with having two folders JobName_XXXX and JobName_XXXX@2. At first my build worked well and I added parallel build for another Jenkins node. After I removed the parallel build, I encountered problem with build. It did not anymore work. I used blueocean's pipeline editor to create Jenkinsfile. By removing the parallel build I set "agent: any" to each step. It caused jenkins to reset scm inside the step. So I could not use previous steps' build results and build crashed. Solution was to remove "agent: any" from steps/stages. It is ok to have agent: any; directly inside the pipeline. With pipeline editor you cant remove those "agent: any" things -- at least not now? So edit Jenkinsfile manually.

Upvotes: 1

tarantoga
tarantoga

Reputation: 1106

You could simply archive the complete workspace at the end of a build. It would then get deleted when the job is deleted.

To do this:

  • Add post-build action -> "Archive the artifacts"
  • Enter ** as "Files to archive"

If you want to make that configurable per run, you could create a build parameter:

  • (enable "This is a parametrized build" if not yet enabled)
  • add parameter of type "Choice Parameter" and name it ARCHIVE
  • with choices being <blank line> and ** (literally, put a blank first line, then second line is exactly **. No quotes)
  • use ${ARCHIVE} as "Files to archive" in "Advanced" setting of "Archive the Artifacts" action
  • enable checkbox "Do not fail build if archiving returns nothing"

Upvotes: 10

Ivan
Ivan

Reputation: 2340

Jenkins stores current workspace as an environment ${WORKSPACE} variable. You can rename it at any moment of the job as long as you also set the renamed absolute directory path to ${WORKSPACE} variable within the build. When to do that is your choice now.

Another option is to schedule a downstream job and pass the ${WORKSPACE} to that job as parameter so that you can rename it.

Upvotes: 2

Related Questions