Answers: <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>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-28T07:49:34.823Z
Answers: Hi, I'm a bit confused by your answer. Does the gitlab solution work on github?
Answers: avi
Answers: CC BY-SA 3.0
Answers: 2018-02-28T09:52:57.110Z
Answers: Hi, It is a plug in for Jenkins which anyway is only going to respond to your GitHub commit or MR, right?
Answers: user2347763
Answers: CC BY-SA 3.0
Answers: 2018-02-27T09:03:02.507Z
Answers: 2018-02-27T09:03:02.507Z
Answers: user2347763
Answers: <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>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-28T07:49:03.293Z
Answers: More background info added.
Answers: avi
Answers: CC BY-SA 3.0
Answers: 2018-02-27T12:46:51.127Z
Answers: 2018-02-27T12:46:51.127Z
Answers: Viresh Doshi
Answers: <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>
  
Answers: CC BY-SA 3.0
Answers: 2018-04-09T16:40:20.997Z
Answers: 2018-04-09T16:40:20.997Z
Answers: Azizur Rahman
Body: <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>
  
Tags: <jenkins><jenkins-pipeline><github>
Title: How can I clean/delete a Jenkins workspace after merge?
UserDisplayName: avi
ThreadText: # 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](https://jenkins.io/doc/book/pipeline/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 travis. 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.  
  
  ---
  
  