Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 957
junebob Avatar asked Oct 22 '15 13:10

junebob


People also ask

Does Jenkins delete workspace after build?

To clean up the workspace before build: Under Build Environment, check the box that says Delete workspace before build starts. To clean up the workspace after the build: Under the heading Post-build Actions select Delete workspace when build is done from the Add Post-build Actions drop down menu.

Why Jenkins workspace is empty?

Possible reasons are: The project was renamed recently and no build was done under the new name. The agent this project has run on for the last time was removed. The workspace directory (null) is removed outside Jenkins.

Does Jenkins clean workspace before build?

There is a way to clean up a workspace in Jenkins. You need to install the Workspace Cleanup Plugin. This plugin can clean up the workspace before build or after a build. Under Build Environment, check the box that says Delete workspace before build starts.

How to automatically clean up Jenkins workspace after builds complete?

Automatically clean up Jenkins Workspace after Builds Complete 1 Step 1: Install Workspace Cleanup Plugin#N#In the most recent Jenkins versions “ Workspace Cleanup Plugin ” comes... 2 Step 2: Making use of the Plugin#N#Once the plugin is installed successfully from the previous step, we are now ready to... 3 Step 3: Sample Jenkinsfile More ...

How to prevent a Jenkins build from overwritten by a new build?

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. You could simply archive the complete workspace at the end of a build.

How do I name a workspace in Jenkins?

Workspace name: Jenkins slave nodes must each use a unique Perforce workspace. The format string configures the workspace name by substituting the specified variables. At least one variable must be used, but it is recommended that, as a minimum, the following variables are used:

Why does it take so long to build Jenkins updates?

Normally this works fine, but sometimes when multiple teams are merging to qa each team and feature branch needs to update (pull) and then get built on Jenkins again. Currently, we are always cleaning the workspace which makes the update builds take longer than they need to.


2 Answers

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"
like image 199
tarantoga Avatar answered Nov 07 '22 06:11

tarantoga


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'
            }
        }
    } 
}
like image 39
Rômulo Z. C. Cunha Avatar answered Nov 07 '22 05:11

Rômulo Z. C. Cunha