Add Continuous Integration(CI) for my Java project
Tags: opensource, java, python
Published: 2023-11-16 16:07:09

In order to ensure new changes won't break the test, I added Continuous Integration(CI) for my project [ConvertTxtToHtml](https://github.com/WangGithub0/ConvertTxtToHtml).

**1. Add CI**
Because my project is on GitHub, I choose to use [GitHub Actions](https://github.com/features/actions). I did it according to the [GitHub document](https://docs.github.com/en/actions/learn-github-actions/using-starter-workflows). I created a gradle.yml for my java project, and set steps for running the JUnit test.

```
name: Java CI with Gradle
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
permissions:
  contents: read
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 20
      uses: actions/setup-java@v3
      with:
        java-version: '20'
        distribution: 'temurin'
    - name: Run JUnit tests
      run: |
        export CLASSPATH=$CLASSPATH:$GITHUB_WORKSPACE/junit4.10/junit-4.10.jar
        cd $GITHUB_WORKSPACE/src
        java org.junit.runner.JUnitCore junit.ConvertTxtMdToHtmlTest
```

**2. Create a PR to test it**
Then I wrote another unit test for my project and created a [PR](https://github.com/WangGithub0/ConvertTxtToHtml/pull/15), then in the action, I could find my CI and built successfully!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/doyhj6etz0j7h8faavwz.png)

**3. Add test for other python project**
 In order to lean more about test and CI, I tried to add test for other project. [til-page-builder](https://github.com/Amnish04/til-page-builder) is a command-line tool for authoring "Today I Learned" posts in Markdown, which can be converted to HTML for publishing on the web. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcxmwet43qbswpftd24e.png)

It is written in Python, the Author write detailed [README](https://github.com/Amnish04/til-page-builder/blob/master/README.md) and [CONTRIBUTING](https://github.com/Amnish04/til-page-builder/blob/master/CONTRIBUTING.md), I can install, styling, linting, run test, get coverage successfully according to his document. Every step is clear and clean! It's really a good work.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t35h11rw9gv9iz3l09iq.png)


This entire Python project structure is well-organized with well-defined functionality for each section. And the code is broken down into modules, each responsible for specific functionality, enhancing code maintainability and reusability.
I created a unit test for the [src/utils/commandline.py](https://github.com/Amnish04/til-page-builder/issues/27), I improved the coverage from [70%->84%](https://github.com/Amnish04/til-page-builder/pull/28)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9szau70mry5s6t6s38hj.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjjbrftelepkpivzk360.png)


It was a valuable learning experience, especially in the world of continuous integration (CI) and testing. Through personal participation and practical application, I gained a deep understanding of the importance of CI pipelines and the important role of testing in software development.