Published on

Getting Started with GitHub Actions

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

GitHub Actions is a powerful tool that allows developers to automate workflows directly in their GitHub repositories. Whether you want to run tests, build your project, or deploy applications, GitHub Actions provides a flexible and customizable CI/CD (Continuous Integration/Continuous Deployment) solution.

This guide covers the basics of GitHub Actions, including creating workflows, understanding key concepts, and a practical example.


Key Concepts

Before diving into creating workflows, it's essential to understand a few core concepts:

1. Workflows

A workflow is an automated process defined in a YAML file within the .github/workflows directory of your repository. Each workflow can contain one or more jobs.

2. Jobs

A job is a set of steps executed in the same runner (virtual environment). Jobs run sequentially or in parallel based on the configuration.

3. Steps

Steps are individual tasks that a job performs. They can include running scripts, installing dependencies, or calling actions.

4. Actions

Actions are reusable units of code used in workflows. GitHub provides a marketplace for pre-built actions, or you can create custom actions.

5. Runners

A runner is a server that executes your workflows. GitHub provides hosted runners, but you can also use self-hosted runners.


Creating Your First GitHub Action Workflow

Step 1: Create the Workflow File

In your repository, create a new directory called .github/workflows. Inside this directory, create a YAML file for your workflow (e.g., ci.yml):

name: CI Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

Step 2: Commit and Push

Commit the YAML file to your repository and push it to GitHub. The workflow will automatically run whenever there is a push or pull request to the main branch.


Understanding the Workflow File

1. name

The name field specifies the name of the workflow (e.g., "CI Pipeline").

2. on

Defines the events that trigger the workflow. In this case, it runs on push or pull_request events targeting the main branch.

3. jobs

Specifies the tasks to perform. Here, the build job runs on an Ubuntu environment (ubuntu-latest).

4. steps

Defines the tasks within the job:

  • actions/checkout: Checks out the repository code.
  • actions/setup-node: Sets up Node.js.
  • npm install: Installs dependencies.
  • npm test: Runs tests.

Advanced Features

1. Matrix Builds

You can test your code against multiple environments (e.g., Node.js versions):

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14, 16, 18]

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

2. Reusable Workflows

You can create reusable workflows to avoid duplication across repositories:

Reusable Workflow (.github/workflows/reusable.yml):

name: Reusable Workflow

on:
  workflow_call:
    inputs:
      environment:
        required: true
        type: string

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to ${{ inputs.environment }}
        run: echo Deploying to ${{ inputs.environment }}

Calling Workflow:

jobs:
  call-reusable:
    uses: ./.github/workflows/reusable.yml
    with:
      environment: production

3. Secrets Management

Store sensitive data like API keys or credentials securely in GitHub Secrets and use them in workflows:

env:
  API_KEY: ${{ secrets.API_KEY }}

Best Practices

  1. Use Specific Versions: Always pin actions to a specific version (e.g., actions/checkout@v3) to avoid unexpected changes.
  2. Limit Workflow Scope: Use the paths filter to trigger workflows only when relevant files are modified.
  3. Test Locally: Use tools like act to run workflows locally before pushing.
  4. Monitor Workflow Runs: Regularly review and optimize workflow execution times.

Conclusion

GitHub Actions simplifies automation and CI/CD for your projects. By understanding the basics and exploring advanced features, you can create efficient workflows tailored to your needs. Whether you're building, testing, or deploying, GitHub Actions has you covered!