What Is Github Action – How to Set Set it Up

In this tutorial, you will understand Github action and how to set it up.

We would create a simple Spring Boot application locally, build it and push it to Github. Then we would create a Github action that triggers the build anytime a push is made to the repository.

Next, will show you how to use Super Linter to validate your code using Github actions.

Lets follow the steps

  1. Create a SpringBoot Application and Push to Github
  2. Setup Java CI With Maven Workflow
  3. Github Action with Super Linter Workflow
  4. Multiple Operating Systems

 

1. Create a Spring Application and Push to Github

Step 1 – Create a new Spring Boot application.

Step 2 – In the terminal, run the command

mvn clean install

And make sure the application builds successfully.

Step 3 – Create a new Github repository and push the code to the repository.

 

2. Setup Java CI With Maven Workflow

What we would like to do is to setup GitHub actions. An action is a collection of Workflow where a workflow is a definition of the steps to perform when a given event occurs. A workflow is defined in a YAML file. But you don’t need to write this from the scratch. There’s already tons of pre-written workflows you can choose from and with very little adjustment, you can get it to give the desired result.

Follow the steps below:

Step 1 – Open your code repository in Github and click on the Actions tab as shown below

Github Actions tab
Github Actions tab

 

Step 2 – In the list of Worflows, find “Java With Maven” as shown below ( you can also search using the search field). This workflow contains the template for building Java projects with Maven.

Java With Maven Workflow

 

Step 3 – Click on Configure. This would display the workflow configuration file in yml. We could actually edit this file later. But for now it’s fine. Notice the structure of the file. The event that triggers the workflow is specified in the on: section. (I explain the rest in the video). In this case, the workflow would trigger on:

  • push
  • pull request

Step 4 – Click on the Start Commit button to commit this file. Note that a workflow file is actually placed inside your repository in a special directory – .github/worflows/<file.yml>. In our case, it is maven.yml.

Step 5 – Now go back to your IDE. First do a pull to update your local repository. Then make some changes and push back to Github. You will notice that the workflow triggers and the build is run.

 

3. Github Action Using Super Linter Workflow

Super Linter is a tool that you can use to check your code to make sure it is syntactically correct. Note that you can add multiple workflows. Now we would add the Super Linter workflow as a second workflow. Follow the steps

Step 1 – Go to Action and click on New Workflow.

Step 2 – Search for Super Linter.

Step 3– Follow the same procedure to add the Super Linter workflow. Notice that once you commit, it triggers the existing workflow as well.

Step 4 – Test out this workflow by pushing code with bug and see that it fails with a red mark.

 

4. Github Actions in Parallel on different OS

If you notice, you see that the basic maven.yml file has as workflow that runs the build on ubuntu-latest. You can also adjust this to allow the build to run on multiple operating sytems in parallel.

To do that, adjust the runs on section of the workflow file to include three operating systems like so;

    runs-on: ${{matrix.os}}
    strategy: 
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]

 

Once you do this, you can go ahead to modify your code in the IDE and push, you will notice that the three runs in parallel.

 

Video tutorial on Working With Github Actions

kindsonthegenius

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published. Required fields are marked *