How to use GitHub Actions

Posted by Java developer blog on May 24, 2020

Overview

In the post, we are going to review GitHub Actions.

GitHub Actions Overview

Firstly, we are going to look at GitHub Actions. It is a CI and CD engine from GitHub.

It is very easy to set up a GitHub Action pipeline if you have a project in GitHub.

Simply click on the Actions tab. Then click on the New workflow button.

You could either create a Simple workflow or choose from one of the existing workflows.

In the end, you will end up with a yml file in the .github/workflows folder.

You could create multiple workflows, customise them and reuse actions as well.

GitHub Actions Hello World

We are going to build a Spring application with Gradle build system and with React frontend.

Add a build.yml file to the .github/workflows folder with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
name: Build

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          node-version: 12.x
      - run: cd demo-frontend && npm install
      - run: cd demo-frontend && npm run build
      - run: cp -a demo-frontend/build/. demo-server/src/main/resources/static/
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Cache Gradle packages
        uses: actions/cache@v1
        with:
          path: ~/.gradle/caches
          key: $-gradle-$
          restore-keys: $-gradle
      - name: Grant execute permission for gradlew
        run: cd demo-server && chmod +x gradlew
      - name: Build with Gradle
        run: cd demo-server && ./gradlew build

name: Build - the name of the workflow

on: - you could choose what is going to trigger the workflow. In the example above it is push to the master branch.

jobs: - it composes the workflow

runs-on: ubuntu-latest - set a system to run on. You could choose even Mac OS;)

steps: - a sequence of tasks execute by a workflow.

You could use a simple bash command like

- run: cd demo-frontend && npm install

or reuse an existing action with the help of uses.

For example a command *uses: actions/checkout@v2** helps you to check out the source code.

There are lots of helpful actions in the marketplace such as cache dependencies for gradle and deploy docker image to heroku cloud.

You also could [add a badge](https://help.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#adding-a-workflow-status-badge-to-your-repository. The easiest way to do is the following: Actions/Choose workflow/Create status badge.

In the end, it’s a very easy and convenient way to build and test code on Github platform.

Conclusion

We have reviewed GitHub Actions. You could check out the source code here.