Skip to content

How to create a first CI project in GitHub Actions workflows?

Hi. Today I’m going to show you how you can launch your first CI project in GitHub Actions workflows.

This article is part of a series showing how to get started with popular CI/CD tools. As part of this post, I will show you how to prepare the process of building and testing a simple Maven-based project.

What are GitHub Actions workflows?

GitHub Actions workflows are a service of the GitHub portal that allows you to run a process consisting of one or more tasks. These processes can be used as Continuous Integration and Continuous Delivery components for our projects.

The service runs within the GitHub portal and integrates very well with the Git repositories it stores. By default, the portal provides GitHub Actions workflows as a service which means you don’t have to deal with maintaining the system itself.

This service allows you to build projects based on many different languages, including : Python, Java, JavaScript, PHP, Ruby, C#, C++, and Go.

A unique feature of GitHub Actions workflows is that build processes consist of steps called actions. These actions can be downloaded from the official https://github.com/marketplace?type= store as well as from the many repositories available on GitHub. There are also no difficulties to creating your own action.

Preparations

Projects in GitHub Actions workflows are defined in GIT repositories hosted on the GitHub portal. Therefore, it is necessary to have an account on this service at the beginning.

After possibly creating an account in GitHub, I also suggest creating a fork (copy) of the repository available at: https://github.com/czerniga/helloworld . In this repository I have posted a simple Java project that will serve as the source code for our CI project.

Granting permission to launch actions

Before we start working on the CI/CD process, we need to grant permissions to run actions in the repository. To do this, we go to our cloned repository and click on Settings.

Settings in the GitHub repository
Settings in the GitHub repository

After that, in the menu on the left, choose Actions -> General and then Allow all actions and reusable workflows. Save your selection by clicking Save.

GitHub Actions settings
GitHub Actions settings

After saving, we should notice that a new Actions tab has appeared. When clicked, it will take us to a screen where we can create a new workflow.

Creating the first workflow
Creating the first workflow

Creating a workflow in GitHub

To create a new workflow in the GitHub repository, we can manually create a new YAML file in the .github/workflows directory, or we can choose a predefined template. In our case, we will use the Java with Maven template, which is available on the main action page. Here we click on the Configure button.

After navigating to a new page, we will see the workflow editor. In the sample template, we see one task called Build. This task is to build a Java application using the mvn -B package –file pom.xml command.

Worflow editor
Worflow editor

Our goal is to build an application and run it for a simple test. Therefore, I prepared an extended workflow, which you can see below.

name: Java CI with Maven

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/[email protected]
    - name: Set up JDK 11
      uses: actions/[email protected]
      with:
        java-version: '11'
        distribution: 'temurin'
        cache: maven
    - name: Build with Maven
      run: |
        echo "Compiling the code..."
        mvn clean package
        echo "Compile complete."
    - name: 'Upload Artifact'
      uses: actions/[email protected]
      with:
        name: helloworld
        path: target/helloworld-1.1.jar
        retention-days: 5    

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
    - uses: actions/[email protected]
    - name: Set up JDK 11
      uses: actions/[email protected]
      with:
        java-version: '11'
        distribution: 'temurin'
        cache: maven
    - name: Download a single artifact
      uses: actions/[email protected]
      with:
        name: helloworld
    - name: Test
      run: |
        echo "Running tests"
        java -cp helloworld-1.1.jar com.coveros.demo.helloworld.HelloWorld

The above workflow consists of the following elements:

  1. name: name of our workflow
  2. on: defines when the workflow should be started. In our case, the process will be started on push and pull request actions on the master branch
  3. jobs: list of tasks in the workflow
  4. build: defines us the task that builds the application. It consists of:
    1. runs-on: defines the name of the runner on which the application will be built
    2. steps: the steps of the task. I have four tasks here:
      1. actions/[email protected] – download application code
      2. Set up JDK 11: preparing the java environment
      3. Build with Maven: process of building the application
      4. Upload Artifact: saving the built application as an artifact
  5. Test: defines us a task that tests the application. It consists of:
    1. runs-on: defines the name of the runner on which the application will be built
    2. needs: build: here we have an indicated dependency between steps. In this case, the test task will be started only after the build task is completed.
    3. steps: the steps of the task. I have four tasks here:
      1. actions/[email protected] – downloading the application code
      2. Set up JDK 11: prepare the java environment
      3. Download a single artifact: download an artifact with the built application from the previous step
      4. Test: run a simple test

We paste the workflow given above into a text editor on the site and replace the original code. We save the change to the repository by clicking on Start Commit. If we want we can add a comment and description. We save the commit by clicking Commit new file.

After saving the file, we go to the Actions tab again. Here we should see on the page that our workflow has already been started. Now let’s click on the name of our first commit (Create maven.yml by default) to go to the details of our build. After a few seconds or so, we should see that our task has been built.

First build on GitHub
First build on GitHub

If we want to, we can view the full logs of the build. To do this, go to the individual tasks available in the left panel. Here we have available logs from all steps.

Summary

In this tutorial I have shown you how to quickly create your first project in GitHub Actions workflows. The process of creating the project itself should not take more than 10 minutes.

Github also provides a free version of this tool. For public projects, we can run the build process for 2,000 minutes per month. This should be enough to verify if this tool is good for us.

Published inCI/CDTutorial

Be First to Comment

Leave a Reply

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