似乎在管道插件中使用的语法与Blueocean不兼容。似乎语法不同,因为blueocean使用声明性管道。

示例

http://fishi.devtail.io/weblog/2016/11/20/ docker-build-pipeline-as-code-jenkins /

  stage ('Docker Build') {
    // prepare docker build context
    sh "cp target/project.war ./tmp-docker-build-context"

    // Build and push image with Jenkins' docker-plugin
    withDockerServer([uri: "tcp://<my-docker-socket>"]) {
      withDockerRegistry([credentialsId: 'docker-registry-credentials', url: "https://<my-docker-registry>/"]) {
        // we give the image the same version as the .war package
        def image = docker.build("<myDockerRegistry>/<myDockerProjectRepo>:${branchVersion}", "--build-arg PACKAGE_VERSION=${branchVersion} ./tmp-docker-build-context")
        image.push()
      }   
    }
}


结果:

WorkflowScript: 5: Unknown stage section "sh". Starting with version 0.5, steps in a stage must be in a steps block. @ line 5, column 1.
   stage ('Docker Build') {
   ^


尝试解决问题

添加步骤块后,管道再次失败:

WorkflowScript: 13: Method calls on objects not allowed outside "script" blocks. @ line 13, column 13.
               docker.withRegistry


评论

也许显示这两个案例的例子?

@DanCornilescu更新了

#1 楼

您可以尝试在声明式管道中使用脚本语法。对于某些步骤,尚无声明性语法。我在尝试使用docker全局变量作为步骤时遇到了相同的问题。

stage ('Docker Build') {
  steps {
    // prepare docker build context
    sh "cp target/project.war ./tmp-docker-build-context"

    // Build and push image with Jenkins' docker-plugin
    script {
      withDockerServer([uri: "tcp://<my-docker-socket>"]) {
        withDockerRegistry([credentialsId: 'docker-registry-credentials', url: "https://<my-docker-registry>/"]) {
            // we give the image the same version as the .war package
            def image = docker.build("<myDockerRegistry>/<myDockerProjectRepo>:${branchVersion}", "--build-arg PACKAGE_VERSION=${branchVersion} ./tmp-docker-build-context")
            image.push()
        }
      }
    }
  }
}


#2 楼

只需避免使用docker DSL;它与声明性不兼容。还要避免script块。只需

 withDockerServer([uri: "tcp://<my-docker-socket>"]) {
  withDockerRegistry([credentialsId: 'docker-registry-credentials', url: "https://<my-docker-registry>/"]) {
    sh '''
      docker build -t whatever .
      docker push whatever
      # or better, put all this stuff into a versioned Bash/Python/etc. script
    '''
  }
}
 


#3 楼

目前似乎有一个未解决的问题。另一个用户也遇到了相同的问题,并添加了以下注释:


为什么节点上下文和阶段上下文之间有区别