Continuous Integration (CI) is a development practice where developers integrate code into a shared repository frequently, ideally several times a day. Each integration can then be verified by an automated build and automated tests. This helps to detect errors quickly and improve the quality of the software.

In this section, we will cover how to set up a CI pipeline in Jenkins. We will go through the following steps:

  1. Understanding the CI Pipeline Workflow
  2. Creating a New Jenkins Job for CI
  3. Configuring Source Code Management (SCM)
  4. Setting Up Build Triggers
  5. Adding Build Steps
  6. Configuring Post-Build Actions
  7. Running and Monitoring the CI Pipeline

  1. Understanding the CI Pipeline Workflow

A typical CI pipeline involves the following stages:

  • Source Code Management (SCM): Fetching the latest code from the version control system (e.g., Git).
  • Build: Compiling the code and creating build artifacts.
  • Test: Running automated tests to ensure the code is working as expected.
  • Post-Build Actions: Actions that are performed after the build, such as archiving artifacts, sending notifications, or deploying to a staging environment.

  1. Creating a New Jenkins Job for CI

  1. Open Jenkins Dashboard:

    • Navigate to your Jenkins dashboard.
  2. Create a New Job:

    • Click on "New Item" on the left-hand menu.
    • Enter a name for your job (e.g., "My-CI-Pipeline").
    • Select "Freestyle project" and click "OK".

  1. Configuring Source Code Management (SCM)

  1. Select SCM:

    • In the job configuration page, scroll down to the "Source Code Management" section.
    • Select "Git" (or your preferred SCM).
  2. Enter Repository URL:

    • Enter the URL of your Git repository.
    • If your repository requires credentials, add them by clicking "Add" next to the "Credentials" field.
  3. Specify Branches to Build:

    • By default, Jenkins will build the master branch. You can specify other branches if needed.
Source Code Management
-----------------------
Repository URL: https://github.com/your-repo.git
Credentials: [Add your credentials]
Branches to build: */main

  1. Setting Up Build Triggers

  1. Enable Build Triggers:
    • Scroll down to the "Build Triggers" section.
    • Select "Poll SCM" and enter a schedule (e.g., H/5 * * * * to poll every 5 minutes).
Build Triggers
--------------
Poll SCM: H/5 * * * *

  1. Adding Build Steps

  1. Add Build Step:

    • Scroll down to the "Build" section.
    • Click "Add build step" and select "Execute shell" (or your preferred build step).
  2. Enter Build Commands:

    • Enter the commands to build your project. For example, if you are using Maven, you might enter:
#!/bin/bash
mvn clean install

  1. Configuring Post-Build Actions

  1. Add Post-Build Actions:

    • Scroll down to the "Post-build Actions" section.
    • Click "Add post-build action" and select "Archive the artifacts".
  2. Specify Artifacts to Archive:

    • Enter the path to the build artifacts (e.g., target/*.jar).
Post-build Actions
------------------
Archive the artifacts: target/*.jar

  1. Running and Monitoring the CI Pipeline

  1. Save and Build:

    • Click "Save" to save your job configuration.
    • Click "Build Now" to trigger a build manually.
  2. Monitor Build:

    • Go to the job's page and click on the build number to see the build details.
    • Check the "Console Output" to see the build logs.

Practical Exercise

Exercise: Set Up a CI Pipeline for a Simple Java Project

  1. Create a New Jenkins Job:

    • Name: Java-CI-Pipeline
    • Type: Freestyle project
  2. Configure SCM:

    • Repository URL: https://github.com/your-java-repo.git
    • Branch: main
  3. Set Up Build Triggers:

    • Poll SCM: H/5 * * * *
  4. Add Build Step:

    • Execute shell:
      #!/bin/bash
      mvn clean install
      
  5. Configure Post-Build Actions:

    • Archive artifacts: target/*.jar
  6. Run the Job:

    • Save and click "Build Now".

Solution

Follow the steps outlined above to set up the CI pipeline. Ensure that your Git repository contains a pom.xml file for Maven to build the project.

Conclusion

In this section, we have learned how to set up a CI pipeline in Jenkins. We covered the essential steps, including configuring SCM, setting up build triggers, adding build steps, and configuring post-build actions. By following these steps, you can automate the process of integrating code changes and ensure that your software is always in a buildable state. In the next section, we will explore how to set up a Continuous Delivery (CD) pipeline.

© Copyright 2024. All rights reserved