Hello, Readers so from the topic itself you might have guessed about today’s Blog Content. Today we will be learning how we can build a CI Pipeline for a Maven application using Github Actions, in that pipeline we will define the various steps like building, Testing, and Packaging the application.
- You should have a Java application pushed onto a Github repository.
- Being Familiar with YAML
To set up and workflow using Github Action, You should make sure, You have created a .github folder and inside that folder create another folder with the name workflows. Now if you are using Github Actions for the First time then let me tell you something about Github Actions. It provides various startup Workflow for building and testing your application. You simply have to click on the action button.
But here I will be configuring the workflow manually by creating a .github/workflows at the root level. After that, we need to give a name to our Ci and then Specify the branch where we want to Run it. and also if you want to run the action or both push and pull events then you can define both or even one.
name: Maven CI on: push: branches: [ main ] pull_request: branches: [ main ]
If you want, You can add more than one Branch also Next we have to Configure jobs and define where we want to run them, Here you can choose Linux, windows, or Mac-os. Make sure the command to build the project works on your machine locally.
jobs: build: runs-on: ubuntu-latest
It is important to take care of the Indentation while writing this file. Now in the above example, I used a Single OS, Here the runners used are GitHub-hosted runners.
JVM version and architecture
The starter workflow sets up
PATH to contain OpenJDK 8 for the x64 platform. If you want to use a different version of Java or target a different architecture (
x86), you can use the
setup-java action to choose a different Java runtime environment.
steps: - uses: actions/checkout@v2 - name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'temurin' cache: maven
Build and Test the Code
Now to Build and test the code, You can use the local command as well also When you use the starter workflow, it will run the
package target by default. In the default Maven configuration, this command will download dependencies, build classes, run tests, and package classes into their distributable format, for example, a JAR file.
I have used the same commands to build and test the code that I use locally, First write the name and define the step you will perform and after that, Under the run field write the command.
- name: Build with Maven run: mvn compile - name: Test with Maven run: mvn test
This was all about building and testing applications using GitHub Actions. Now you must have noticed
This is Caching dependencies When using GitHub-hosted runners, you can cache your dependencies to speed up your workflow runs. After a successful run, your local Maven repository will be stored on GitHub Actions infrastructure So that In future runs, the cache will be restored so that it won’t downloaded again. You can cache dependencies by simply using the
setup-java action or can use
cache action for custom and more advanced configuration.
Packaging and Uploading artifact
You must be aware of the package command which helps us to generate a jar file, we will be using the same with our GitHub Actions. Maven will usually create output files like JARs, EARs, or WARs in the
target directory. So we will use this command to Package
- name: Package with maven run: mvn package
Now we will make a directory with the artifact name and we will copy this jar file
- run: mkdir artifact && cp target/*.jar artifact
Then we can upload the contents of that directory using the
- uses: actions/upload-artifact@v2 with: name: Package path: artifact
As you can see after the Build is finished you can check the summary of the successful build and you will see the artifact is present there and you can even download that from there by clicking on the name.
Now let me summarise all the code in one place, You can simply copy this code and make changes as per your requirement.
name: Maven CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'temurin' cache: maven - name: Build with Maven run: mvn compile - name: Test with Maven run: mvn test - name: Package with maven run: mvn package - run: mkdir artifact && cp target/*.jar artifact - uses: actions/upload-artifact@v2 with: name: Package path: artifact
This was all about building, Testing, and Packaging then Uploading the building artifact using Github Actions. If you liked this blog, Please like, comment, and share this.
If you want to learn more about it you can visit the official Documentation.