Preserve Jenkins workspace

泄露秘密 提交于 2020-06-12 04:40:09

问题


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.


回答1:


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"



回答2:


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'
            }
        }
    } 
}



回答3:


Jenkins stores current workspace as an environment ${WORKSPACE} variable. You can rename it at any moment of the job as long as you also set the renamed absolute directory path to ${WORKSPACE} variable within the build. When to do that is your choice now.

Another option is to schedule a downstream job and pass the ${WORKSPACE} to that job as parameter so that you can rename it.




回答4:


This answer comes a bit late and probably not totally correct answer to your question, but this is the first ticket which I found when searching solution for my problem.

I had problem with having two folders JobName_XXXX and JobName_XXXX@2. At first my build worked well and I added parallel build for another Jenkins node. After I removed the parallel build, I encountered problem with build. It did not anymore work. I used blueocean's pipeline editor to create Jenkinsfile. By removing the parallel build I set "agent: any" to each step. It caused jenkins to reset scm inside the step. So I could not use previous steps' build results and build crashed. Solution was to remove "agent: any" from steps/stages. It is ok to have agent: any; directly inside the pipeline. With pipeline editor you cant remove those "agent: any" things -- at least not now? So edit Jenkinsfile manually.



来源:https://stackoverflow.com/questions/33282783/preserve-jenkins-workspace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!