Building docker images with declarative pipeline

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Building docker images with declarative pipeline

Thomas Fjellstrom
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

Andrew Bayer
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.

On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <[hidden email]> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPbPdOZq0aixi79eZ-jqad7QpgmvRYVtHiH554iY1t7xa1wnSw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

Thomas Fjellstrom
That would explain the complete lack of examples. I'm kindof surprised there wasnt some kind of mapping from plugin dsl to the new declarative syntax, to at least allow some use of the existing apis. The script blocks are good enough for now however.

Thanks :)

On Tuesday, February 14, 2017 at 2:35:19 PM UTC-7, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.

On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="aI3RfL_iAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">tfjel...@...> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="aI3RfL_iAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6410aaa9-3702-4b3c-b2da-a2665326ee0b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

Max Schindler
In reply to this post by Thomas Fjellstrom
I would also be very interested in this. @Thomas: How are you proceeding with your topic? Maybe we can share our experience and help each other on this topic?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/42af4c85-57c6-4b6d-9e67-98a1e8d97307%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

drew.leske
In reply to this post by Andrew Bayer
Hello Andrew and Thomas,

I ran in this exact problem and used a script{} block, but I am wondering if there are updates to this issue.  All of the documentation/examples I have seen use scripted syntax, but sometimes the documentation is behind the code. :)

Cheers,
Drew.

On Tuesday, February 14, 2017 at 1:35:19 PM UTC-8, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.

On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="aI3RfL_iAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">tfjel...@...> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="aI3RfL_iAAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/669bfe32-1f0a-4d10-b057-f0888ac2ef37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

Alex B
Hi Drew,

Could you post where you found the example using script{} block? I'm looking into building Docker images in ephemeral jenkins agents but having difficulty finding proper documentation for the syntax.

-Alex

On Thursday, June 1, 2017 at 1:23:11 AM UTC-5, [hidden email] wrote:
Hello Andrew and Thomas,

I ran in this exact problem and used a script{} block, but I am wondering if there are updates to this issue.  All of the documentation/examples I have seen use scripted syntax, but sometimes the documentation is behind the code. :)

Cheers,
Drew.

On Tuesday, February 14, 2017 at 1:35:19 PM UTC-8, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.

On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <[hidden email]> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/94792b60-199f-41af-92d6-d7330d6a892f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

Ari Pringle
Hi Alex,

I had some trouble figuring out how to do this as well. The docs here show what is possible for docker.image using scripted pipeline (and thus inside of a script{} block): https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#docker-workflow

I ended up with something like this:

script {
    def image = docker.build("image-name:test", '-f Dockerfile.dev .')
    // can then call methods like image.push() here, or run a command with image.inside():
    image.inside('--net=host -v /mount:/mount:ro') {
       sh 'some-command.sh'
    }
}

- Ari

On Monday, September 11, 2017 at 3:57:23 PM UTC-6, Alex B wrote:
Hi Drew,

Could you post where you found the example using script{} block? I'm looking into building Docker images in ephemeral jenkins agents but having difficulty finding proper documentation for the syntax.

-Alex

On Thursday, June 1, 2017 at 1:23:11 AM UTC-5, [hidden email] wrote:
Hello Andrew and Thomas,

I ran in this exact problem and used a script{} block, but I am wondering if there are updates to this issue.  All of the documentation/examples I have seen use scripted syntax, but sometimes the documentation is behind the code. :)

Cheers,
Drew.

On Tuesday, February 14, 2017 at 1:35:19 PM UTC-8, Andrew Bayer wrote:
Right now, we don't have a great answer for this other than to use Scripted Pipeline docker.build(...) within a script { } block - we don't allow "foo.bar()" or "foo = bar()" syntax in steps blocks in Declarative unless they're within a script block. Anything within a script block doesn't go through validation, so you can use the full Scripted Pipeline syntax there. That said, we are thinking about how to make this clearer in the future so that you can build/deploy images without having to use script {}.

A.

On Tue, Feb 14, 2017 at 2:32 PM, Thomas Fjellstrom <[hidden email]> wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/fff6ea30-6b51-4f46-8516-8da8026c4241%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/575efaf0-b077-4cf9-b6c5-62e3126970f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Building docker images with declarative pipeline

Joshua Noble
In reply to this post by Thomas Fjellstrom
With declarative pipelines this is pretty straight forward, just build the Docker image like you would on your local machine using `sh` commands.

For example, in our cluster all build agents are actually the same, but most builds run inside of a container on one of these nodes, which you've noted (correctly) that you don't want to do, since you want to build an image. We've simply given the "docker" label to our build agents, and our declarative Jenkinsfile's use that label under the agent option.

For example, you could use the following Jenkinsfile to build your-app. Note that I'm using a GIT_SHA variable here which actually doesn't resolve, but you could easily write a pipeline shared library to provide this info. In addition, I only like to publish master branch builds on some repos in order to save space in our registry. You can even tell Jenkins to use specific credentials when pushing.

pipeline {
  agent { label 'docker' }
  options {
    ansiColor colorMapName: 'XTerm'
  }
  stages {
    stage('Build') {
      steps {
        sh "docker build -t registry.yourcompany.com/company/your-app:${GIT_SHA} ."
      }
    }
    stage('Publish') {
      when {
        branch 'master'
      }
      steps {
        withDockerRegistry([credentialsId: 'registry-creds', url: 'https://registry.yourcompany.com']) {
          sh "docker push registry.yourcompany.com/company/your-app:${GIT_SHA}"
        }
      }
    }
  }
}




On Tuesday, February 14, 2017 at 2:32:56 PM UTC-5, Thomas Fjellstrom wrote:
Hi,

I'm brand new to jenkins, and have been reading a lot about the pipeline plugin. I think I have a basic grasp of it, but I haven't been able to figure out the proper syntax to build docker images in a pipeline stage. I don't want to run the steps IN a docker container, but rather build containers (multiple containers) in a pipeline Jenkinsfile. I also have some other requirements, like accessing credentials setup in the jenkins interface, and a private docker registry.

Any help would be appreciated.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/7202bd44-f3b3-4ad6-9b9b-ec9cf3425c49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.