# Part 2: Creating Your First Jenkins Pipeline

### Jenkins CI/CD Series

| Part | Article |
| --- | --- |
| 1 | [Install Jenkins with Docker](https://ask-abhi.com/part-1-install-jenkins-with-docker) |
| 2 | [Creating Your First Jenkins Pipeline](https://ask-abhi.com/part-2-creating-your-first-jenkins-pipeline) |
| 3 | [Jenkins SSH Remote Execution](https://ask-abhi.com/part-3-jenkins-ssh-remote-execution) |
| 4 | [Automating MySQL Backup to AWS S3](https://ask-abhi.com/part-4-automating-mysql-backup-to-aws-s3) |
| 5 | [Making Jenkins Automation Scalable](https://ask-abhi.com/part-5-making-jenkins-automation-scalable) |
| 6 | [Running Ansible from Jenkins](https://ask-abhi.com/part-6-running-ansible-from-jenkins) |
| 7 | [Jenkins Pipeline from GitHub](https://ask-abhi.com/part-7-control-jenkins-pipeline-through-github) |
| 8 | [Multibranch Pipelines](https://ask-abhi.com/part-8-multibranch-pipelines) |
| 9 | [Parameterized Pipelines](https://ask-abhi.com/part-9-parameterized-pipelines) |
| 10 | [Environment Variables in Pipelines](https://ask-abhi.com/part-10-environment-variables-in-pipelines) |

* * *

### Goal

The goal of this project is to create and run the **first Jenkins Pipeline** that automates the process of building and testing an application.

In this exercise, we will configure Jenkins to pull application code from a Git repository, execute build commands using Maven, and run automated tests. This introduces the fundamental structure of a **Jenkins Declarative Pipeline**, including stages such as workspace cleanup, source code checkout, build execution, and testing.

By the end of this project, you will understand how Jenkins pipelines automate repetitive development tasks and form the foundation of modern CI/CD workflows.

* * *

### Purpose

The purpose of this exercise is to demonstrate how Jenkins pipelines automate the software development lifecycle by defining a structured workflow for building and testing applications.

In traditional development environments, developers manually run build and test commands. Jenkins pipelines eliminate these manual steps by automatically executing them whenever a job is triggered.

Through this project, you will learn how to:

*   Define a **Declarative Jenkins Pipeline**
    
*   Organize automation tasks using **pipeline stages**
    
*   Integrate Jenkins with **Git repositories**
    
*   Execute **build and test commands** automatically
    
*   Understand how CI pipelines validate code changes
    

This hands-on exercise introduces the **core concept of Continuous Integration**, where code is automatically built and verified before moving further in the deployment pipeline.

* * *

### Prerequisites

Before starting this project, ensure the following requirements are completed:

*   Ready to use `Host` and the `directory` structure to run `Dockerfiles` and `docker-compose.yml` (Refer to [Part 1](https://ask-abhi.com/part-1-install-jenkins-with-docker))
    
*   **Demo-app repo:** Fork the [application](https://github.com/abhinandan-chougule/demo-app-java-maven) repository used for the pipeline demonstration:
    

* * *

### Step-by-step Implementation

`/home/jenkins/jenkins_home/docker-compose.yml`

docker-compose.yml

```yaml
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports:
      - "8080:8080"
    volumes:
      - $PWD/jenkins_home:/var/jenkins_home
    networks:
      - net
networks:
  net:
```

> Note- No need to use customized Dockerfile since this will download a jenkins image from Docker Hub which has enough packages required for this project

```plaintext
docker compose up -d
```

*   **Install Maven**
    

```shell
# Login
docker exec -it -u root jenkins bash

# Install maven
apt-get install maven
```

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/95550608-4790-4a70-9b26-f63cefffb086.png align="center")

> Note- Since I have Maven already installed, it shows it exists

*   **Create Jenkins Job I names it** `maven-pipeline`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/02a3dea0-7c5e-4b75-aa74-df5c39cde701.png align="center")

*   **Create** `Jenkinsfile` **seperatly and paste it into Pipleine script area**
    
*   **Demo application** [**repo**](https://github.com/abhinandan-chougule/demo-app-java-maven) **can be** `forked`
    

```dockerfile
pipeline {
    agent any

    stages {

        stage('Clean Workspace') {
            steps {
                deleteDir()
            }
        }

        stage('Checkout Code') {
            steps {
                git url: 'https://github.com/abhinandan-chougule/demo-app-java-maven.git', branch: 'main'
            }
        }

        stage('Build with Maven') {
            steps {
                sh 'mvn clean install'
            }
        }

        stage('Run Tests') {
            steps {
                sh 'mvn test'
            }
        }
    }
}
```

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/02d63777-7639-47a0-9255-12bd64ade2ae.png align="center")

*   **Click on** `Build Now`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/1e0d97b5-8244-423d-9c6f-285bfd6da001.png align="center")

*   **Console output Logs**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/e703944f-8b61-4ceb-aa39-58277f062bea.png align="center")

### Done!!!

* * *

### Project 4: Jenkinsfile controlled through version control GitHub

*   **Goal**:  
    Create Pipeline Job  
    Provide where to refer the Jenkinsfile  
    Run the Build
    

> Note- We will make use of Jenkins is from above project

*   **Fork** [**repo**](https://github.com/abhinandan-chougule/demo-app-java-maven.git)
    
*   **Jenkinsfile placed at below locaion in repo and will be used**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/3b626a02-4ebd-4066-b5d2-a23966698191.png align="center")

*   **Create new Job** `maven-github-pipeline`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/1d4981fa-062c-4281-9def-2ea1c3f5b6fe.png align="center")

*   **Select options as below**
    

**Definition**: `Pipeline script from SCM`  
**SCM**: `Git`  
**Repository URL :** `Forked repo HTTP URL`  
**Branch Specifier (blank for 'any')**: `*/main`  
**Script** **Path**: `jenkins/Jenkinsfile`

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/10b6f381-0920-45b2-aaca-d7be1b29edb3.png align="center")

**Build now**

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/73863c38-5ca1-4cca-a4ab-f8a6402d0575.png align="center")

*   **Go through Console output logs**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/652c7c4b-bfb9-49dc-94cf-537bb9304cb7.png align="center")

*   **Build now one more time and go to Jobs Console output and compare**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/3604f007-0b12-41cd-9ece-d64a2643a7fb.png align="center")

<mark class="bg-yellow-200 dark:bg-yellow-500/30">This is not cloning the repo again, instead it's checking if there are any latest commits and changes</mark>

Done!!!

* * *

Project: 5 Multibranch Pipeline

*   **Go to the repo and add another branch.**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/21f54923-0ffc-401c-b876-5a35808c7308.png align="center")

*   **Scan the Multibranch pipeline now, and it will populate the branches**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/b102edd3-ead9-48eb-bb81-d8da61f27b9c.png align="center")

*   **Let's make some changes to the repo and commit to the new branch."**`devbranch`**"**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/cc4013fb-fa85-4f3a-af25-b6fde1c7277e.png align="center")

*   **I am deleting the last stage**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/9463fa15-2b8c-4908-82bd-5cbc311b8bba.png align="center")

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/13d36bd9-7447-4f96-bcb4-3812e59acc91.png align="center")

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/a30bf874-f3e9-4a2c-94ca-16a1be1c6e56.png align="center")

**Done!!!**

* * *

### Project 6: Parameterize to give the option in the Jenkins Job to run on a specific AZ

### Goal

Create a Jenkins pipeline named `parameter-pipeline` that accepts user inputs through parameters to control a deployment configuration. The pipeline should allow the user to specify a custom deployment name, choose the target AWS Availability Zone, and confirm the deployment before execution.

### Purpose

The purpose of this Configuration is to demonstrate how **parameterized Jenkins pipelines** work. By defining parameters, the pipeline becomes more flexible and interactive, allowing users to provide deployment-specific inputs at runtime instead of hardcoding values in the pipeline.

This exercise introduces three commonly used Jenkins parameter types:

*   **String Parameter** – to provide a custom deployment name.
    
*   **Choice Parameter** – to select the AWS Availability Zone (EU-WEST-2A, EU-WEST-2B, EU-WEST-2C).
    
*   **Boolean Parameter** – to confirm whether the deployment should proceed.
    

Using parameterized pipelines is a common practice in **DevOps CI/CD workflows**, enabling controlled deployments across different environments or regions while minimizing manual changes to pipeline code.

Here is the code that will be used

```typescript
pipeline {
    agent any

    parameters {
        string(
            name: "deploymentName",
            defaultValue: "",
            description: "Deployment Name?"
        )

        choice(
            name: "azDeploy",
            choices: ["ap-southeast-1a", "ap-southeast-1b", "ap-southeast-1c"],
            description: "What AZ?"
        )

        booleanParam(
            name: "confirmDeploy",
            defaultValue: false,
            description: "CONFIRM DEPLOYMENT?"
        )
    }

    stages {
        stage("Deploy") {
            steps {
                echo "Deployment Name: ${params.deploymentName}"
                echo "AZ Selected: ${params.azDeploy}"
                echo "Deployment Confirmation: ${params.confirmDeploy}"
            }
        }
    }
}
```

*   **Create Pipeline** `parameter-pipeline`**"**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/19ab4202-af9a-4259-bb26-87f8ef7d7c93.png align="center")

*   **Add the script to the** `pipeline script`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/d6a281e0-1ff9-4e2a-b01b-154533527444.png align="center")

*   `Build Now`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/483c7115-129f-4f90-8bd0-c7d22ad617cb.png align="center")

*   **Provide inputs and <mark class="bg-yellow-200 dark:bg-yellow-500/30">click</mark>** `build with parameters`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/da21cb7c-31a1-4380-8731-08c16a274827.png align="center")

*   **Expected** `console output`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/c4a41512-e3d4-496e-9b66-06b2485334a9.png align="center")

*   **Pipeline status**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/63257ee8-ea95-4ee2-900b-4ceecdb4c5f8.png align="center")

**Done!!!**

* * *

### Project: How to virtualize the Jenkins jobs so it would be reusable

### Goal

Create a Jenkins pipeline that demonstrates how to define and use **environment variables** within a Jenkins Declarative Pipeline. The pipeline initializes different types of variables (string, number, and boolean) and verifies that they can be accessed and printed during pipeline execution.

### Purpose

The purpose of this task is to understand how **environment variables work inside a Jenkins pipeline** and how they can be referenced within stages and steps. By defining variables such as a string, a number, and a boolean inside the `environment` block, the pipeline provides a simple demonstration of how configuration values can be stored and reused across pipeline stages.

This approach helps standardize pipeline configurations and allows DevOps engineers to manage commonly used values in a centralized location within the pipeline. Environment variables are widely used in CI/CD workflows for tasks such as configuring application settings, defining build parameters, and passing values between different stages of a pipeline.

Through this exercise, users learn how Jenkins pipelines can manage and reference variables efficiently, which is a foundational concept when building more advanced automation and deployment pipelines.

*   **Prepare the code**
    

```typescript
pipeline {
    agent any

    environment {
        def myString = "Hello World"
        def myNumber = 10
        def myBool = true
    }

    stages{
        stage("Demo") {
            steps {
                echo "myString: ${myString}"
                echo "myNumber: ${myNumber}"
                echo "myBool: ${myBool}"
            }
        }
    }
}
```

*   **Create pipeline "variable-jenkins."**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/cb521024-5c27-44b5-b243-c5c36cc4833a.png align="center")

*   **Paste the code into the** `pipeline script`
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/4659097f-a1f2-4f34-840e-d1e2db00bbb6.png align="center")

*   **Build Now**
    
*   **Now we can see the pipeline using the variables given in the environment block**
    

![](https://cdn.hashnode.com/uploads/covers/6997142f587b14d25b5231b7/2bbc1e60-fdd7-46fb-ba42-7e7596f28b6c.png align="center")

### Done!!!

* * *

### Conclusion

In this part of the Jenkins CI/CD series, we created and executed our **first Jenkins pipeline**. The pipeline automated a basic CI workflow that includes cleaning the workspace, cloning source code from Git, building the application using Maven, and running automated tests.

This project introduced the fundamental structure of Jenkins pipelines and demonstrated how build and test processes can be automated through a simple pipeline script.

Understanding how to create and execute pipelines is a critical step toward building scalable CI/CD workflows. Jenkins pipelines allow teams to standardize build processes, reduce manual intervention, and ensure consistent application validation.

In the next part of the series, we will expand Jenkins automation capabilities by enabling it to **execute commands on remote servers using SSH**, which is a common requirement in deployment and infrastructure automation workflows.

* * *

## 🔗 Continue the Series

⬅️ **Previous Article:** [Part 1 Install Jenkins with Docker](#)  
➡️ **Next Article:** [Part 3 Jenkins SSH Remote Execution](#)

* * *

⭐ If you found this article useful, follow **https://ask-abhi.com** for more DevOps tutorials.

* * *
