<p>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 <a href="https://github.com/jenkinsci/gitlab-plugin#configuring-access-to-gitlab" rel="nofollow noreferrer">gitlab</a></p>

CC BY-SA 3.0
2018-02-28T07:49:34.823Z
Hi, I'm a bit confused by your answer. Does the gitlab solution work on github?
avi
CC BY-SA 3.0
2018-02-28T09:52:57.110Z
Hi, It is a plug in for Jenkins which anyway is only going to respond to your GitHub commit or MR, right?
user2347763
CC BY-SA 3.0
2018-02-27T09:03:02.507Z
2018-02-27T09:03:02.507Z
user2347763
<p>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. </p>

<p>How about adding more info around what you want to preserve?</p>

CC BY-SA 3.0
2018-02-28T07:49:03.293Z
More background info added.
avi
CC BY-SA 3.0
2018-02-27T12:46:51.127Z
2018-02-27T12:46:51.127Z
Viresh Doshi
<p>I am assuming you are using <a href="https://jenkins.io/doc/book/pipeline/jenkinsfile/" rel="nofollow noreferrer">Jenkinsfile</a>. You can do for example:</p>

<pre><code>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 */
            }
        }
    }
}
</code></pre>

CC BY-SA 3.0
2018-04-09T16:40:20.997Z
2018-04-09T16:40:20.997Z
Azizur Rahman
<p>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.</p>

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

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

<p>edit: Background.</p>

<p>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.</p>

<p>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.</p>

<jenkins><jenkins-pipeline><github>
How can I clean/delete a Jenkins workspace after merge?
avi
# How can I clean/delete a Jenkins workspace after merge?

(Asked by: username_0 on 2018-02-27)

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.

**Tags:** jenkins, jenkins-pipeline, github


---

## 3 answers

### Answer by username_3 on 2018-04-09. Score: 4 | Accepted answer

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 */
            }
        }
    }
}


---

### Answer by username_1 on 2018-02-27. Score: 0

Gitlab seems to have a solution although the github webhook on MR should trigger an event in Jenkins. It does on trusername_0s. Configuring the Jenkins job seems to be well documented in the [gitlab](https://github.com/jenkinsci/gitlab-plugin#configuring-access-to-gitlab)

> **Comments:**
> **username_0**: Hi, I'm a bit confused by your answer. Does the gitlab solution work on github?  
> **username_1**: Hi, It is a plug in for Jenkins which anyway is only going to respond to your GitHub commit or MR, right?  

---

### Answer by username_2 on 2018-02-27. Score: 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?

> **Comments:**
> **username_0**: More background info added.  

---

