GitHub Actions Explained

ยท

3 min read

GitHub Actions Explained

I have always wondered how GitHub Actions worked and how they were important. It wasn't until I started working with Azure ML Studio and Azure DevOps that they made sense. In the article, we will be looking at what GitHub Actions are, how you can use them and more importantly how you can create your actions to automate some parts of your workflow as a developer or engineer.

What are GitHub Actions? ๐Ÿง

To put it simply, GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. This means that you can use them to test your code as you are building it, build your applications or systems using actions and deploy your code using actions that are very helpful when dealing with DevOps or MLOps but also go way beyond them.

GitHub Actions help you automate your workflow and make your work that much easier because then you don't have to do everything yourself.

Parts of a GitHub Action

How to create your own GitHub Actions

The basic syntax for creating a GitHub Action:

name: CI Workflow
on:
  push:
    branches:
      - main
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2
      - name: Run Tests
        run: npm test

In this example, the workflow is triggered on push events to the main branch. It also runs a job called test on the latest version of the Ubuntu operating system. The steps in the job first check out the repository and then run tests using the npm test command.

You can create actions for various tasks such as when a pull request (PR) is created to check for code quality to ensure that it meets the set standards before being merged to the main branch. This workflow is created using the pull_request event trigger. The syntax for creating a workflow that is activated when a PR is created is as follows:

name: PR Created Workflow
on:
  pull_request:
    types:
      - opened

jobs:
  build_and_test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'

      - name: Install Dependencies
        run: npm install

      - name: Run Tests
        run: npm test

In this example, we have a workflow named "PR Created Workflow." The on section specifies the event that triggers the workflow, which is the pull_request event with the type opened. This means the workflow will run whenever a new pull request is created.

The jobs section defines the jobs that the workflow should execute. In this case, there is one job named build_and_test, which will run on the latest version of the Ubuntu operating system (ubuntu-latest).

The steps within the job define the tasks to be performed. The example steps include checking out the repository, setting up Node.js with version 14, installing project dependencies with npm install, and finally, running tests with npm test.

With this workflow, every time a new pull request is created in the repository, the specified steps will be executed automatically. This helps run automated tests, checks, and other tasks to ensure the quality of code changes before they are merged into the main codebase.

th.webp

You can connect with me on:

GitHub LinkedIn Twitter

ย