Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build docker images using a Declarative Jenkinsfile

Tags:

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....

like image 543
MarkH Avatar asked Apr 26 '18 12:04

MarkH


People also ask

How to build and deploy a docker image through Jenkins pipeline?

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

How do I build an image from a dockerfile?

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.

What is a dockerfile and how do I write it?

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.

How do I build an image from a Jenkins job?

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.


Video Answer


1 Answers

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()                 }             }         }     } } 
like image 125
v.karbovnichy Avatar answered Oct 23 '22 17:10

v.karbovnichy