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.
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.
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.
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.
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 ...
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.
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:
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.
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:
**
as "Files to archive"If you want to make that configurable per run, you could create a build parameter:
ARCHIVE
<blank line>
and **
(literally, put a blank first line, then second line is exactly **
. No quotes)${ARCHIVE}
as "Files to archive" in "Advanced" setting of "Archive the Artifacts" actionYou 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'
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With