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:
- Install Git (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- Install Java Development Kit (JDK) (https://www.oracle.com/java/technologies/javase-jdk15-downloads.html)
- Install Jenkins (https://www.jenkins.io/download/)
- JUnit: This will be included in your Java project dependencies.
Step 1: Setting Up Version Control (Git)
- Create a Git Repository:
git init myproject cd myproject
- Create Sample Files:
touch README.md
- Commit Changes:
git add . git commit -m "Initial commit"
- 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
- Install Required Jenkins Plugins:
- Login to Jenkins, go to "Manage Jenkins" -> "Manage Plugins."
- Install plugins for Git integration, JUnit, and any other necessary plugins. - Create a New Jenkins Job:
- Click on "New Item."
- Enter a name for your project, choose "Freestyle project," and click "OK." - Configure Source Code Management:
- Under the "Source Code Management" section, select Git.
- Enter your repository URL and credentials if required. - Configure Build Triggers:
- Choose "Poll SCM" and set up a schedule (e.g., `*/5 * * * *` for polling every 5 minutes). - Configure Build Steps:
- Under the "Build" section, add build steps (e.g., `mvn clean install` for Maven projects). - 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
- 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); } }
- Run Tests Locally:
- Run your tests locally using your IDE or build tool to ensure they pass. - Push Changes:
- Commit your test files and push changes to the remote repository.
Step 4: Continuous Integration in Action
- Trigger the Jenkins Job:
- After pushing changes to your Git repository, Jenkins will automatically trigger the job based on the polling schedule you set. - 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. - 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.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.