如何锁定声明Jenkins管道的多个阶段?

时间:2017-05-21 16:06:13

标签: jenkins jenkins-pipeline

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

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

这里有什么问题? documentation明确指出:

  

lock也可用于将多个阶段包装成单个阶段   并发单元

5 个答案:

答案 0 :(得分:30)

应该注意,您可以使用锁定选项锁定管道中的所有阶段:

pipeline {
    agent any
    options {
        lock resource: 'shared_resource_lock'
    }
    stages {
        stage('will_already_be_locked') {
            steps {
                echo "I am locked before I enter the stage!"
            }
        }
        stage('will_also_be_locked') {
            steps {
                echo "I am still locked!"
            }
        }
    }
}

答案 1 :(得分:16)

此问题已解决。

您现在可以通过将多个阶段分组在父阶段中来锁定它们,例如:

stage('Parent') {
  options {
    lock('something')
  }
  stages {
    stage('one') {
      ...
    }
    stage('two') {
      ...
    }
  }
}

(不要忘记您需要可锁定资源插件)

答案 2 :(得分:15)

问题在于,尽管声明性管道是technically available in beta in September, 2016,但你引用的博客文章(从10月开始)是记录脚本化的管道,而不是声明性的(它没有多说,所以我感受到你的痛苦)。可锁定资源尚未作为声明性管道步骤进行烘焙,以便启用您正在寻找的功能。

你可以这样做:

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

您可以为此用例使用脚本管道。

答案 3 :(得分:1)

如果资源仅由此管道使用,您还可以禁用并发构建:

pipeline {
    agent any
    options {
        disableConcurrentBuilds()
    }
    stages {
        stage('will_already_be_locked') {
            steps {
                echo "I am locked before I enter the stage!"
            }
        }
        stage('will_also_be_locked') {
            steps {
                echo "I am still locked!"
            }
        }
   }
}

答案 4 :(得分:0)

我在同一构建节点上运行多个构建和测试容器。测试容器必须将节点名称锁定为db用户名进行测试。

lock(resource: "${env.NODE_NAME}" as String, variable: 'DBUSER')

选项锁定是在加载时计算的,但NODE_NAME早就未知。为了锁定多个阶段以获得视觉效果,我们可以在脚本块内创建阶段,即代码段中的“运行测试”阶段。舞台可视化效果与其他舞台图块一样好。

pipeline {
    agent any
    stages {
        stage('refresh') {
            steps {
                echo "freshing on $NODE_NAME"
                lock(resource: "${env.NODE_NAME}" as String, variable: 'DBUSER') {
                    sh '''
                        printenv | sort
                    '''
                    script {
                        stage('run test')
                        sh '''
                            printenv | sort
                        '''
                    }
                }
            }
        }
    }
}