Integrating Version Control, Build, and Test Automation in CI Workflows - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Integrating Version Control, Build, and Test Automation in CI Workflows

Share This
Continuous Integration (CI) is a development practice that can significantly enhance the quality and efficiency of software projects. In this tutorial, we will guide you through integrating version control, build automation, and automated testing in CI workflows using Git as the version control system, Jenkins as the CI server, and JUnit as the testing framework for a Java project.

Prerequisites:

  1. Install Git (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) 
  2. Install Java Development Kit (JDK) (https://www.oracle.com/java/technologies/javase-jdk15-downloads.html) 
  3. Install Jenkins (https://www.jenkins.io/download/) 
  4. JUnit: This will be included in your Java project dependencies.


Step 1: Setting Up Version Control (Git)


  1. Create a Git Repository:
       
          git init myproject
          cd myproject
          
  2. Create Sample Files:
       
          touch README.md
          
  3. Commit Changes:
       
          git add .
          git commit -m "Initial commit"
          
  4. Create a Remote Repository (GitHub, GitLab, Bitbucket, etc.):
    - Create a repository on your preferred platform.
    - Link your local repository to the remote:
       
          git remote add origin <repository_url>
          git branch -M main
          git push -u origin main
          

Step 2: Configuring Jenkins for Build and Test Automation


  1. Install Required Jenkins Plugins:
    - Login to Jenkins, go to "Manage Jenkins" -> "Manage Plugins."
    - Install plugins for Git integration, JUnit, and any other necessary plugins.
  2. Create a New Jenkins Job:
    - Click on "New Item."
    - Enter a name for your project, choose "Freestyle project," and click "OK."
  3. Configure Source Code Management:
    - Under the "Source Code Management" section, select Git.
    - Enter your repository URL and credentials if required.
  4. Configure Build Triggers:
    - Choose "Poll SCM" and set up a schedule (e.g., `*/5 * * * *` for polling every 5 minutes).
  5. Configure Build Steps:
    - Under the "Build" section, add build steps (e.g., `mvn clean install` for Maven projects).
  6. Configure Post-Build Actions:
    - Add a post-build action to publish JUnit test results:
    - Select "Publish JUnit test result report."
    - Enter the path to your JUnit XML report (e.g., `target/surefire-reports/*.xml` for Maven projects).

Step 3: Writing Automated Tests


  1. Write JUnit Tests:
    - In your Java project, create test classes using JUnit.
    - For example:
         import org.junit.jupiter.api.Test;
         import static org.junit.jupiter.api.Assertions.assertEquals;
    
         public class MyTests {
             @Test
             public void testAddition() {
                 assertEquals(4, 2 + 2);
             }
         }
    
  2. Run Tests Locally:
    - Run your tests locally using your IDE or build tool to ensure they pass.
  3. Push Changes:
    - Commit your test files and push changes to the remote repository.

Step 4: Continuous Integration in Action


  1. Trigger the Jenkins Job:
    - After pushing changes to your Git repository, Jenkins will automatically trigger the job based on the polling schedule you set.
  2. View Jenkins Build Output:
    - Access your Jenkins dashboard and click on your project to view the build output.
    - Jenkins will clone the repository, build the project, run tests, and display the results.
  3. Analyze Test Results:
    - Check the test results published in Jenkins. It will show if the tests passed or failed.
You have successfully integrated version control, build automation, and automated testing into a CI workflow. Now, every time you make changes to your code and push them, Jenkins will automatically build your project and run tests, ensuring the stability and reliability of your application.


Happy Exploring!

No comments:

Post a Comment