Exercise-6
6. Continuous Integration with Jenkins: Setting Up a CI Pipeline, Integrating Jenkins with Maven/Gradle, Running Automated Builds and Tests.
Prerequisites
Before proceeding, ensure you have the following installed:
Java Development Kit (JDK) 8 or higher
Jenkins (Installed and configured on a local or cloud environment)
Maven or Gradle
Git (Installed and configured with a repository)
Step 1: Install Required Jenkins Plugins
To integrate Jenkins with Maven/Gradle and automate builds, install the following plugins:
Navigate to Jenkins Dashboard > Manage Jenkins > Manage Plugins.
Under the Available Plugins tab, search for and install:
Pipeline Maven Plugin Database (ID:
pipeline-maven
)Gradle Plugin (ID:
gradle
)Git Plugin (ID:
git
)Restart Jenkins to apply the changes
Step 2: Configure Maven/Gradle in Jenkins
For Maven:
Go to Manage Jenkins > Global Tool Configuration.
Scroll to the Maven section and click Add Maven.
Provide a Name (e.g., "Maven 3.8") and set the Maven Home directory path.
Click Save.
For Gradle:
Navigate to Manage Jenkins > Global Tool Configuration.
Scroll to the Gradle section and click Add Gradle.
Provide a Name (e.g., "Gradle 7.3") and set the Gradle Home path.
Click Save.
Step 3: Create a Jenkins Job
From the Jenkins dashboard, click New Item.
Enter a name for the project (e.g., "My-CI-Pipeline").
Select Freestyle Project or Pipeline (depending on your requirement).
Click OK to create the job.
Step 4: Configure Source Code Management (SCM) with Git
To pull the source code from a Git repository:
Example Project:
We will use a sample Maven Java project hosted on GitHub:
Repository:
https://github.com/spring-projects/spring-petclinic.git
In the job configuration page, navigate to Source Code Management.
Select Git.
Enter the repository URL:
https://github.com/spring-projects/spring-petclinic.git
Under Branch Specifier, enter
*/main
(or*/master
if applicable).If the repository is private, click Add Credentials and enter your GitHub username and access token.
Click Save.
Step 5: Configure Build Steps
For Maven Projects:
Scroll to the Build section and click Add Build Step > Invoke Top-Level Maven Targets.
In the Goals field, enter:
clean install
Click Save.
For Gradle Projects:
Click Add Build Step > Invoke Gradle Script.
Select the Gradle Version configured earlier.
In the Tasks field, enter:
clean build
Click Save.
Step 6: Configure Post-Build Actions
Scroll to Post-Build Actions.
Click Add Post-Build Action > Publish JUnit Test Result Report.
In the Test Report XMLs field, enter:
target/surefire-reports/*.xml (for Maven) build/test-results/test/*.xml (for Gradle)
Click Save.
Step 7: Trigger Automatic Builds
To trigger builds automatically upon code changes:
In the Build Triggers section, check Poll SCM.
Enter a schedule (e.g.,
H/5 * * * *
to check for changes every 5 minutes).Click Save.
Step 8: Run and Verify the Build
Navigate to the Jenkins dashboard and select the newly created job.
Click Build Now.
Monitor the build status in the Build History section.
Click on the latest build and navigate to Console Output to review logs.
Verify that the build completes successfully and test results are generated.
Comments
Post a Comment