GitHub Actions 101

GitHub Actions 101

CI/CD pipelines are typically complex and involve many tools that range from application testing to integration testing to container platforms to application platforms, among others. GitHub Actions, which were introduced in 2018, simplify the process especially with Node and Docker integrations. In this article, we’ll delve into its features and get our hands on a demo to illustrate its capabilities.


Summary :

  1. What is Github Actions ?
  2. Use cases for Github Actions
  3. The Github Actions workflow YAML file 
  4. Tutorial

1- What is GitHub Actions ?

GitHub Actions is a continuous integration and continuous delivery platform used to automate developer workflows. It allows us to build, test and deploy pipelines. These pipelines are triggered by events that we can specify in our configuration file.

These triggers are specified events that can range from creating a pull request to building a new brand in a repository.

2 – Use cases for Github Actions

GitHub actions can automate any webhook on GitHub, which makes it a powerful platform. It can also handle complex workflows and simple operations like sending a message on Slack to a specific member when a Pull Request is ready for review. 

In the following section, we will present different scenarios in which it would be more suitable to choose to work with GitHub actions

  • Build, test, and deploy within the GitHub flow
  • Automate repetitive tasks
  • Manage users easily at scale
  • Easily add preferred tools and services to your project
  • Quickly review & test code on GitHub
  • Keep track of your projects

3 – The workflow YAML file 

GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored in a separate YAML file in your code repository, in a directory called . github/workflows. 

a workflow yaml file specifies events, jobs, runners, steps and actions 

Events: Events are defined triggers that initiate a workflow. They can be configured to look for one or more triggers and qualified as needed by a developer. They can also be configured to run on specific coding branches in a given repository on GitHub.

Jobs: Jobs are a set of steps that run on the same runner. Each one runs in its own VM and in parallel with other jobs, unless otherwise specified.

Steps: Steps are individual tasks that execute the commands of a job. This can be an action or a shell command. All steps in a job run on the same runner.

Actions: An action is a command that runs on a runner-and. the central element of GitHub Actions, named after it.

Runners: A runner is a GitHub Actions server. It listens for available jobs, runs each one in parallel, and returns progress, logs, and results. Each runner can be hosted by GitHub or self-hosted on a localized server. GitHub-hosted runners are based on Ubuntu Linux, Windows, and macOS.

An example workflow on GitHub Actions. 
An example workflow on GitHub Actions. 

An example workflow on GitHub Actions. 

Note: GitHub Actions also supports matrix builds, which allow builds to be tested simultaneously on multiple operating systems and runtime versions. 

4 – Tutorial 

For the demonstration, we will use a java application to build its artifact with Gradle using Github actions. Once done, we will push the artifact to a docker hub repo. 

Note: For building a java application artifact, we can use both Maven and Gradle. Here we have chosen to use gradle.

Prerequisites

  • Docker Hub account 
  • Github account
  1. Start by forking the Github application repo at the main branch
https://github.com/pi-square-io/java-github-actions/tree/main
  1. Define the Github Actions workflow 

To define the workflow configuration, we can choose to use a template from the github action templates or create our own template from scratch in the .github\workflows path. In this case, we choose the first option.

By choosing Actions in the menu bar, Github will display a set of pipelines, we will search for Java with Gradle pipeline

Get started with GitHub Actions

As for the main core of the pipeline, it consists of creating an artifact for our java application and then we publish it in a docker hub. For this purpose we have defined the following YAML file.

  • The events of our pipeline are push and pull requests defined on the master branch
  • The runner is ubuntu
  • The steps consist of: 
    • Setting up Java Development Kit 
    • Granting execute permissions for Grawdlew 
    • Building and publishing the created docker image, for this purpose we stored the docker hub credentials in Github Secrets.

After commiting the changes of the pipeline we can take an overview of the executed steps

work flow executed steps on Github

And finally, the docker image is created and published in our docker hub repository.

Conclusion

GitHub Actions is a convenient way to implement the CI/CD pipeline for projects, as they are already registered on GitHub for version control. Having both features on the same platform simplifies the process. Actions is also easy to configure and supports a wide variety of technology stacks.

Vous avez un projet Cloud ?