如何锁定声明式Jenkins管道的多个阶段? [英] How to lock multiple stages of declarative Jenkins pipeline?

查看:133
本文介绍了如何锁定声明式Jenkins管道的多个阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在声明性Jenkins管道内的锁内运行多个阶段:

I want to run multiple stages inside a lock within a declarative Jenkins pipeline:

pipeline {
    agent any
    stages {
        lock(resource: 'myResource') {
            stage('Stage 1') {
                steps {
                  echo "my first step"
                }
            }

            stage('Stage 2') {
                steps {
                  echo "my second step"
                }
            }

        }
    }
}

我收到以下错误:

Started by user anonymous
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 10: Expected a stage @ line 10, column 9.
           lock(resource: 'myResource') {
           ^

WorkflowScript: 10: Stage does not have a name @ line 10, column 9.
           lock(resource: 'myResource') {
           ^

WorkflowScript: 10: Nothing to execute within stage "null" @ line 10, column 9.
           lock(resource: 'myResource') {
           ^

3 errors

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:116)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:430)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:393)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:257)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:405)
Finished: FAILURE

这是什么问题? 文档明确指出:

What's the problem here? The documentation explicitly states:

lock也可以用于将多个阶段包装为单个阶段 并发单位

lock can be also used to wrap multiple stages into a single concurrency unit

推荐答案

问题是,尽管声明性管道为

The problem is that, despite the fact that declarative pipelines were technically available in beta in September, 2016, the blog post you reference (from October) is documenting scripted pipelines, not declarative (it doesn't say as much, so I feel your pain). Lockable resources hasn't been baked in as a declarative pipeline step in a way that would enable the feature you're looking for yet.

您可以这样做:

pipeline {
  agent { label 'docker' }
  stages {
    stage('one') {
      steps {
        lock('something') {
          echo 'stage one'
        }
      }
    }
  }
}

但是你不能做

pipeline {
  agent { label 'docker' }
  stages {
    lock('something') {
      stage('one') {
        steps {
          echo 'stage one'
        }
      }
      stage('two') {
        steps {
          echo 'stage two'
        }
      }
    }
  }
}

而且你做不到:

pipeline {
  agent { label 'docker' }
  stages {
    stage('one') {
      lock('something') {
        steps {
          echo 'stage one'
        }
      }
    }
  }
}

您可以在此用例中使用脚本化管道.

You could use a scripted pipeline for this use case.

这篇关于如何锁定声明式Jenkins管道的多个阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆