一千萬個為什麽

搜索

如何使用Jenkins的blueocean將Docker鏡像發布到私人碼頭註冊表中?



看來,管道插件中使用的語法與Blueocean不兼容。它看起來像語法不同,因為blueocean使用聲明式管道。

http://fishi.devtail .IO /博客/ 2016/11/20 /搬運工-集結管道-AS-碼詹金斯/

  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://"]) {
      withDockerRegistry([credentialsId: 'docker-registry-credentials', url: "https:///"]) {
       //we give the image the same version as the .war package
        def image = docker.build("/:${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

轉載註明原文: 如何使用Jenkins的blueocean將Docker鏡像發布到私人碼頭註冊表中?

一共有 3 個回答:

您可以嘗試在聲明式管道中使用腳本語法。對於某些步驟,還沒有聲明性語法。嘗試使用 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://"]) {
        withDockerRegistry([credentialsId: 'docker-registry-credentials', url: "https:///"]) {
           //we give the image the same version as the .war package
            def image = docker.build("/:${branchVersion}", "--build-arg PACKAGE_VERSION=${branchVersion} ./tmp-docker-build-context")
            image.push()
        }
      }
    }
  }
}

只需避免 docker DSL;它與Declarative不兼容。還要避免 script 塊。只是

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

目前似乎有公開問題。另一位用戶也遇到了同樣的問題,並添加了以下評論:

為什麽節點上下文和階段上下文存在差異