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:

  1. Navigate to Jenkins Dashboard > Manage Jenkins > Manage Plugins.

  2. Under the Available Plugins tab, search for and install:

    • Pipeline Maven Plugin Database (ID: pipeline-maven)

    • Gradle Plugin (ID: gradle)

    • Git Plugin (ID: git)

  3. Restart Jenkins to apply the changes

Note: if the Plugins are not shown in Available Plugins and shown in installed plugin tab, then it indicates that plugins are already installed.

Step 2: Configure Maven/Gradle in Jenkins

For Maven:

  1. Go to Manage Jenkins > Global Tool Configuration.

  2. Scroll to the Maven section and click Add Maven.

  3. Provide a Name (e.g., "Maven 3.8") and set the Maven Home directory path.

  4. Click Save.

For Gradle:

  1. Navigate to Manage Jenkins > Global Tool Configuration.

  2. Scroll to the Gradle section and click Add Gradle.

  3. Provide a Name (e.g., "Gradle 7.3") and set the Gradle Home path.

  4. Click Save.

Step 3: Create a Jenkins Job

  1. From the Jenkins dashboard, click New Item.

  2. Enter a name for the project (e.g., "My-CI-Pipeline").

  3. Select Freestyle Project or Pipeline (depending on your requirement).

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

  1. In the job configuration page, navigate to Source Code Management.

  2. Select Git.

  3. Enter the repository URL:

    https://github.com/spring-projects/spring-petclinic.git
  4. Under Branch Specifier, enter */main (or */master if applicable).

  5. If the repository is private, click Add Credentials and enter your GitHub username and access token.

  6. Click Save.

Step 5: Configure Build Steps

For Maven Projects:

  1. Scroll to the Build section and click Add Build Step > Invoke Top-Level Maven Targets.

  2. In the Goals field, enter:

    clean install
  3. Click Save.

For Gradle Projects:

  1. Click Add Build Step > Invoke Gradle Script.

  2. Select the Gradle Version configured earlier.

  3. In the Tasks field, enter:

    clean build
  4. Click Save.

Step 6: Configure Post-Build Actions

  1. Scroll to Post-Build Actions.

  2. Click Add Post-Build Action > Publish JUnit Test Result Report.

  3. In the Test Report XMLs field, enter:

    target/surefire-reports/*.xml (for Maven)
    build/test-results/test/*.xml (for Gradle)
  4. Click Save.

Step 7: Trigger Automatic Builds

To trigger builds automatically upon code changes:

  1. In the Build Triggers section, check Poll SCM.

  2. Enter a schedule (e.g., H/5 * * * * to check for changes every 5 minutes).

  3. Click Save.

Step 8: Run and Verify the Build

  1. Navigate to the Jenkins dashboard and select the newly created job.

  2. Click Build Now.

  3. Monitor the build status in the Build History section.

  4. Click on the latest build and navigate to Console Output to review logs.

  5. Verify that the build completes successfully and test results are generated.



Comments