I'm new to using Jenkins....
I'm trying to automate the production of an image (to be stashed in a repo) using a declarative Jenkinsfile. I find the documentation to be confusing (at best). Simply put, how can I convert the following scripted example (from the docs)
node { checkout scm def customImage = docker.build("my-image:${env.BUILD_ID}") customImage.push() }
to a declarative Jenkinsfile....
In order to build and deploy a Docker image through a Jenkins Pipeline, we need to install the Plugin Docker Pipeline plugin. The Docker Pipeline plugin provides a build () method for creating a new image, from a Dockerfile in the repository, during a Pipeline run. Once installed. Let’s get started
To build the image on your own computer, navigate to the project directory (the one with your application code and the Dockerfile), and run docker build: docker build . -t getintodevops-hellonode:1 This instructs Docker to build the Dockerfile in the current directory with the tag getintodevops-hellonode:1.
Writing a Dockerfile To be able to build a Docker image with our app, we’ll need a Dockerfile. You can think of it as a blueprint for Docker: it tells Docker what the contents and parameters of our image should be. Docker images are often based on other images.
To build it, press Build Now. After a few minutes you should see an image appear in your Docker Hub repository, and something like this on the page of your new Jenkins job: We have successfully containerised an application, and set up a Jenkins job to build and publish the image on every change to a repository.
You can use scripted pipeline blocks in a declarative pipeline as a workaround
pipeline { agent any stages { stage('Build image') { steps { echo 'Starting to build docker image' script { def customImage = docker.build("my-image:${env.BUILD_ID}") customImage.push() } } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With