Exercise-8

8. Practical Exercise: Set Up a Jenkins CI Pipeline for a Maven Project, Use Ansible to Deploy Artifacts Generated by Jenkins.

🔧 Part 1: Install Jenkins and Maven on Ubuntu (VirtualBox)

Step 1: Update System

sudo apt update
sudo apt upgrade -y

Step 2: Install Java (required for Jenkins & Maven)

sudo apt install openjdk-17-jdk -y
java -version

Step 3: Install Maven

sudo apt install maven -y
mvn -version

Step 4: Install Jenkins

Add Jenkins Repository

wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian binary/ > /etc/apt/sources.list.d/jenkins.list'

Install Jenkins

sudo apt update
sudo apt install jenkins -y

Start and Enable Jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins

Access Jenkins

Open browser inside Ubuntu and go to:

http://localhost:8080

Unlock Jenkins

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Use this password to unlock Jenkins and install Suggested Plugins.


✨ Part 2: Create a Simple "Hello World" Java Maven Project

Step 1: Create Directory and Project

mkdir hello-maven
cd hello-maven mvn archetype:generate -DgroupId=com.example -DartifactId=hello-maven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Step 2: Navigate to the Project

cd hello-maven

Step 3: 

Edit the App.java

// File: src/main/java/com/example/App.java
package com.example; public class App { public static void main(String[] args) { System.out.println("Hello, Jenkins CI!"); } }

Edit pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>hello-maven</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <!-- JUnit 4 for testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- Compiler plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <encoding>UTF-8</encoding> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>

Edit AppTest.java

// File: src/test/java/com/example/AppTest.java
  1. package com.example; import org.junit.Test; import static org.junit.Assert.*; public class AppTest { @Test public void testApp() { assertTrue(true); } }

Step 4: Build and Test

mvn clean install

Output will be in:

target/hello-maven-1.0-SNAPSHOT.jar

🚀 Part 3: Set Up Jenkins CI Pipeline for the Maven Project

Step 1: Open Jenkins → Create New Job

  • Choose Freestyle project

  • Name: hello-maven-ci

Step 2: Configure Project

  • Source Code Management: None (since local project)

  • Build:

    • Add a build step: Invoke top-level Maven targets

    • Goals: clean install

    • POM: path-to-your-project/pom.xml (e.g., workspace/hello-maven/hello-maven/pom.xml)

Note: Keep All Jenkins Projects Inside /var/lib/jenkins/workspace/
Ex:
sudo cp -r /home/anil/Desktop/hello-maven /var/lib/jenkins/workspace/
sudo chown -R jenkins:jenkins /var/lib/jenkins/workspace/hello-maven

Step 3: Save and Build

Click Build Now – Jenkins will compile and build the JAR locally.


⚙️ Part 4: Deploy Artifact Using Ansible

Step 1: Install Ansible

sudo apt install ansible -y

Step 2: Prepare Ansible Playbook

Create a directory:

mkdir ansible-deploy
cd ansible-deploy

Create deploy.yml:

- name: Deploy Maven Artifact Locally
hosts: localhost tasks: - name: Copy JAR to /opt/app copy: src: /home/user/hello-maven/target/hello-maven-1.0-SNAPSHOT.jar dest: /opt/app/hello-maven.jar


Note: Change the src path accordings

Step 3: Run the Playbook

sudo mkdir -p /opt/app
ansible-playbook deploy.yml


Comments

  1. This is a very practical exercise. I’ve always used jenkins for builds, but combining it with Ansible for artifact deployment is something I want to try in my next project. Looking forward to seeing more guides that connect CI pipelines with configuration management tools.

    ReplyDelete
  2. This was easy to follow and very hands-on. I tried a similar setup on my VM and was surprised how smooth the artifact copy worked with Ansible. Do you also recommend extending the playbook for service restarts after deployment?

    ReplyDelete

Post a Comment