Getting Started With GitHub Actions

GitHub hosts more than 200 million repositories, and for the 11 years since its inception, developers have only been able to build CI/CD pipelines using third-party tools, such as Travis CI and CircleCI. This external process changed in November of 2019, when GitHub announced the launch of GitHub Actions. GitHub Actions is a CI/CD tool built right into every GitHub repository, using text-based configuration files located directly within the repository.

The concept of root in GitHub routines is a file workflow, or a series of automated actions. In practice, the workflow is similar to a pipeline and allows developers to configure a series of stages that can be executed each time a particular event is triggered. Each repository can contain any number of workflows, and each workflow consists of the following components:

component Describe
Profession

A set of steps to be performed on the same runner

By default, if a workflow has more than one job, the jobs will be executed in parallel, but jobs can be configured to run in a chain by declaring that one job is dependent on another. If the work B Depends on the job aProfession B It will be performed only if the work a Completed successfully.

a step

A task consisting of one or more commands or shell processes

All steps of the function are performed on the same runner, thus, the data can be shared with each other.

a job

A preset set of actions that can be performed in a step

There are several actions already available through the GitHub community that perform common tasks, such as checking code or loading artifacts.

Event

Motivation leads to the implementation of the workflow

One of the most common events is a user depositing a token to the repository.

runner

A server that performs tasks on a particular operating system (OS) or platform

Contestants can be hosted by GitHub or on independent servers.


The relationship between these components is shown below:

In practice, workflows are more general than a CD pipeline, but they are closely related:

  • Workflows = pipelines
  • Functions = stages
  • Steps = the series of actions that make up a stage

Workflow example

To demonstrate the workflow, we can create a small project with several tests. In this example, we will use the extension dzone-github-actions-refcard-example The project. This project is running a Representational State Transfer (REST) ​​API application that responds with “Hello, world!” a message from /hello endpoint and has one test to validate the endpoint response text. To clone the repository, execute the following commands:

git clone git@github.com:albanoj2/dzone-github-actions-refcard-example.git
git checkout code

Once the project is ready, we can build a new workflow by creating a file .yml file in .github/workflows/ Our GitHub repository directory – for example, .github/workflows/example.yml. We can then configure our workflow to check our repository and run our tests with mvn test The command to add the following to a file example.yml a file:

name: dzone-github-actions-example

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: mvn package -DskipTests
      - name: Upload Artifacts
        uses: actions/upload-artifact@v2
        with:
          name: jar-file
          path: target/github-actions-example-1.0.0.jar

  unit-test:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: mvn test

This composition is divided into three main parts:

  1. name – Optional name for the workflow.
  2. on – the trigger executing the workflow – in this case, when the commit is pushed to the repository, it is indicated by push. full file format on The field is documented in the workflow structure of the GitHub Actions page.
  3. jobs The functions that make up the workflow.

The job field contains two jobs: build And unit-test.

build is the function used to build our project. Note that the name build It has no special significance and any name can be used. the runs-on The field indicates the operating system and environment in which the task will be performed, such as the latest version of Ubuntu (denoted by ubuntu-latest). the stepsThe field indicates the steps of the job. In this case, there are three steps:

1. Warehouse Check Check out the code in the repository using the Checkout procedure. Since we don’t know the state of the runner that will execute each function, we first have to check our repository before we can access our code. We can specify which actions are available to run with uses field. See the workflow context page for more information.

2. Warehouse construction – we execute a shell command – in this case, mvn package -DskipTests – using run field. This command packages our app into a Jar file without running tests (which will be executed in a later task). If needed, we can also run multiple shell commands using the pipe character. For example, we can echo Running a build Then execute the build as follows:

- run: | 
    echo "Running a build"
    mvn package -DskipTests

3. Download artifacts – As we will see later, we will need to access the JAR file (our executable) created in this function. To store it at a later time, we load the JAR element with a file upload-artifact Action, set the name of the loaded widget (so we can refer to it later) using name field and determine the path of the tool using path field. See the artifact download documentation for more information.

unit-test is the function used to perform our unit tests. This job is similar to ours build But instead of running mvn package -DskipTests command, we run mvn test ordering. In addition, we also add needs whose value is simply the name of the function we have unit-test Depends on. In our case, we specify that we have unit-test work depends on build using the function needs: build. With the formation of this relationship, we have unit-test The task will only be performed once build The task has been completed successfully. See the workflow context page for more information.

when we do this example.yml file, GitHub recognizes that the workflow is configured and executes our workflow. If we click procedures Tab in our GitHub repository we can see all workflows matching our commits:

If we click on the workflow path – here, Added Actions – We can see the status of our pipeline for this along with the status and duration information:

Finally, if we click on build Function, we can see the log output that corresponds to the implementation build job in our area Added Actions committed:

Although the architecture of the GitHub Actions workflow is simple, it provides the mechanisms needed to build complex pipelines and perform almost any actions we need to meet our business goals.

.

Leave a Comment