[OSD600] CI/CD for Java Maven using Github Actions
GitHub Action Workflows
I set up GitHub Action for my Static Site Generator (SSG). Actions themselves are individual jobs that can be combined together in a workflow to create a complex pipeline that handles our needs in the development life cycle. These workflow files can then be configured in YAML and triggered to run on specific events such as each time someone pushes code to the main branch.
My yaml
file means that it is triggered every time a push or a pull request is made to the main
branch. I then have the jobs keyword which state the name of all the different jobs I would like to run. In this case I start off by stating build_and_test. The first thing I then declare is in what OS environment this job should run: ubuntu-latest.
Under steps a sequence of steps is declared.
actions/checkout@v2
: I give my job access to the code in my repositoryactions/setup-java@v2
: set up a Java environment that the job can run in. I also use the with keyword to tell setup-java to use a specific version of the JDK, which in this case is JDK-11.mvn package -DskipTest
: install all dependencies and skiptestsmvn com.coveo:fmt-maven-plugin-format:fmt
: Run Formatter ( I need to configure the plugin inpom.xml
to run thismvn test
Run all the tests
name: Maven CI/CD
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Install dependencies
run: mvn package -DskipTests
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify
- name: Run Formatter
run: mvn com.coveo:fmt-maven-plugin:format
- name: Test
run: mvn test
Add Test to partner Repo
I pick Minsu Kim‘s repo to write some more unit tests on his/ her project. Though the project is written in C#, I still want to give it a try because I’m curious if C#’s testing methods are similar to java.
I created 04 unit tests to test an interface and one class
- shouldReturnTableOfContentString()
- shouldReturnOptions()
- shouldReturnAVersion()
- shouldThrowExceptionWhenTitleIsEmpty()
After testing successfully in my local machine, I made a pull request
My 04 tests pass his/her CI/CD and then got merged into her/his project.
Happy Coding!