Day 27: Jenkins Declarative Pipeline with Docker

Day 27: Jenkins Declarative Pipeline with Docker

This is a blog related to how to write Jenkins Pipeline as code in Pipeline Script.

Docker Build and Docker Run

docker build: You can use sh 'docker build . -t <tag>' in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.)

docker run: You can use sh 'docker run -d <image>' in your pipeline stage block to build the container.

How will the stages look;

stages {
        stage('Build') {
            steps {
                sh 'docker build -t trainwithshubham/django-app:latest'
            }
        }
    }

Task-01

Create a docker-integrated Jenkins declarative pipeline.

Creation of Declarative Pipeline

Screenshot from 2023-04-12 00-22-33

You can give a Description to this of what you have created

Screenshot from 2023-04-11 01-46-30

Use the above-given syntax using sh inside the stage block.

Screenshot from 2023-04-11 01-46-50

Console Output

Screenshot from 2023-04-12 00-14-31

Snapshot of Pipeline Success

Screenshot from 2023-04-11 01-46-17


Task-02

Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

Note: If someone is getting an error like this you can use following steps,

workflowscript: 11: invalid agent type "docker" specified. must be one of [any, label, none] @ line 11, column 17. docker{
error!!!

Dashboard >> Manage Jenkins >> Available Plugins >> Download Docker Pipeline and Docker Plugins.

Description of Declarative Pipeline using Docker

Screenshot from 2023-04-12 00-15-28

Pipeline Script

Screenshot from 2023-04-12 00-15-42

Screenshot from 2023-04-12 00-15-49

Console Output

Screenshot from 2023-04-12 00-16-37

Snapshot of Pipeline Success

Screenshot from 2023-04-12 00-17-06


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!