HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to publish a docker image to a private docker registry using Jenkins' blueocean?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
publishimagedockerjenkinsregistryprivateusinghowblueocean

Problem

It seems that the syntax that is used in the pipeline plugin is incompatible with Blueocean. It looks like that the syntax is different as blueocean uses a declarative pipeline.

Example

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://"]) {
      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()
      }   
    }
}


results in:

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') {
   ^


Attempt to solve the issue

When a steps block was added the pipeline failed again:

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

Solution

You can try to use scripting syntax into the declarative pipeline. For some step there is no declarative syntax yet. I had the same problem trying to use the docker global variable as a step.

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()
        }
      }
    }
  }
}

Code Snippets

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()
        }
      }
    }
  }
}

Context

StackExchange DevOps Q#2143, answer score: 6

Revisions (0)

No revisions yet.