Curriculum
Course: Developer Operation (DevOps)
Login
Text lesson

Introduction to GitHub Actions

Core Concepts: Workflows, Events, and Jobs

 

GitHub Actions is an event-driven automation tool. It allows you to automate tasks and define custom CI/CD pipelines directly within your GitHub repository.

  • Workflow: A workflow is an automated procedure that you add to your repository. It’s defined by a YAML file in the .github/workflows directory. A repository can have multiple workflows, and each workflow can be triggered by different events.

  • Event: An event is a specific activity in a repository that triggers a workflow run. Common events include:

    • Push: When code is pushed to a branch.

    • Pull request: When a pull request is created, updated, or closed.

    • Schedule: To run a workflow at a specific time (e.g., every day at midnight).

  • Job: A job is a set of steps in a workflow that executes on the same runner (a virtual machine). Each job runs in a clean environment and can run in parallel with other jobs by default.

  • Step: A step is a single task within a job. It can be a command to execute (e.g., npm install) or an action to run.

  • Action: An action is a reusable command or script that performs a specific task. GitHub provides many official actions, and you can also find a vast library of community-created actions in the GitHub Marketplace.

Creating a Simple GitHub Actions Workflow

Let’s create a simple workflow that automatically builds a Node.js application whenever code is pushed to the main branch.

Step 1: Create a Workflow File

First, navigate to your repository and create a new directory: .github/workflows. Inside this directory, create a new YAML file. Let’s name it nodejs.yml.

Step 2: Define the Workflow

The workflow file will look like this:

YAML
# .github/workflows/nodejs.yml

# Name of the workflow
name: Node.js CI

# Trigger the workflow on push events to the 'main' branch
on:
  push:
    branches: [ "main" ]

# Define the jobs to run
jobs:
  build:
    # Name of the job
    name: Build and Test

    # Specify the runner environment
    runs-on: ubuntu-latest

    # Define the steps for this job
    steps:
      # Step 1: Check out the code from the repository
      - uses: actions/checkout@v4

      # Step 2: Set up the Node.js environment
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18.x'

      # Step 3: Install dependencies
      - name: Install dependencies
        run: npm ci

      # Step 4: Run the tests
      - name: Run tests
        run: npm test

Explanation of the Code

  • name: Node.js CI: This gives our workflow a human-readable name that appears in the GitHub UI.

  • on: push: branches: [ "main" ]: This specifies the trigger for our workflow. It will run whenever a push event occurs on the main branch.

  • jobs: build:: This defines a single job named build.

  • runs-on: ubuntu-latest: This specifies the virtual machine environment where our job will run. In this case, we are using the latest version of Ubuntu.

  • steps:: This is a list of individual tasks to be executed sequentially in the job.

    • The actions/checkout@v4 action checks out our repository code so the job can access it.

    • The actions/setup-node@v4 action installs the specified version of Node.js on the runner.

    • The run: commands execute standard shell commands to install dependencies and run tests.

Step 3: Commit and Push

Commit this file to your repository and push it to the main branch. GitHub will automatically detect the workflow file and start a new run, which you can monitor under the “Actions” tab of your repository.

This simple workflow demonstrates how GitHub Actions can be used to automate a fundamental part of the DevOps process: Continuous Integration.–>–>