1

I would like to keep a workspace in Jenkins until after the pull request is merged. Meaning, I don't want to clear the workspace after every build, but I also don't want to clear the workspace only when the branch gets deleted.

How can I use the github webhooks to trigger a workspace cleanup whenever a pull request gets resolved as merged?

Ideally, the solution would involve using my JenkinsFile/Pipelines.

edit: Background.

We have long-lived branches and short-lived branches. For brevity, I'll call the long-lived branches "team branches". Our workflow is to take short-lived branches, such as feature branches and make a PR request to pull them into the team branches.

On Jenkins, we then build the feature branch and the PR request and the team branch. 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. So I want to be able to preserve the workspace until all updates are done and the PR request actually gets merged, only then do I want to delete the workspace for the feature branch and the PR request and the team branch.

3 Answers 3

4

I am assuming you are using Jenkinsfile. You can do for example:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
             when { branch 'master' } /* when in master branch */
             steps {
                echo 'Deploying....'
                deleteDir() /* clean up our workspace */
            }
        }
    }
}
0

Gitlab seems to have a solution although the github webhook on MR should trigger an event in Jenkins. It does on travis. Configuring the Jenkins job seems to be well documented in the gitlab

2
  • Hi, I'm a bit confused by your answer. Does the gitlab solution work on github?
    – avi
    Feb 28, 2018 at 7:49
  • Hi, It is a plug in for Jenkins which anyway is only going to respond to your GitHub commit or MR, right? Feb 28, 2018 at 9:52
0

There are ways to detect using Groovy if a build is say part of a multibranch/non master branch and then you can say on successful build output perform a workspace delete.

How about adding more info around what you want to preserve?

1
  • More background info added.
    – avi
    Feb 28, 2018 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.