What does a ws() block do in Jenkins?

谁说我不能喝 提交于 2019-11-29 13:36:10

问题


I'm trying to convert few Jenkinsfiles from Scripted Pipeline to Declarative Pipeline. I have a block like this in a Jenkinsfile:

ws("/path/to/dir") {
    // do stuff
}

I wonder what does it do exactly and what's the proper way to convert it to the Declarative Pipeline syntax.


回答1:


ws allocates a new workspace. you would use this to ensure that nothing else interferes with the location on disk where you are running the enclosed steps.

  • this is not as heavy-handed as the node step, since node will also ensure that it gets run with a separate executor.
  • this provides more isolation than the dir step, since dir will not ensure an isolated location on the filesystem the way ws will.

you can use it in a declarative pipeline in the same way as scripted:

pipeline {
  agent { label 'docker' }
  stages {
    stage('hot_stage') {
      steps {
        sh 'pwd'
        ws('/tmp/hey') {
          sh 'pwd'
        }
      }
    }
  }
}

produces output:

+ pwd
/opt/jenkins/workspace/tool_jenkins2-test_master-R4LIKJR63O6POQ3PHZRAKWWWGZZEQIVXVDTM2ZWZEBAWE3XKO6CQ
[Pipeline] ws
Running in /tmp/hey
[Pipeline] {
[Pipeline] sh
[hey] Running shell script
+ pwd
/tmp/hey

references:

  • https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-ws-code-allocate-workspace
  • https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#allocating-workspaces


来源:https://stackoverflow.com/questions/43577139/what-does-a-ws-block-do-in-jenkins

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