Introduction to GitHub Actions

Automation is a key part of modern software development, and GitHub Actions makes it easy to integrate Continuous Integration (CI) and Continuous Deployment (CD) directly into your GitHub repositories. Whether you want to automatically run tests, build applications, or deploy updates, GitHub Actions provides a flexible way to define workflows triggered by events in your repository.

If you're new to GitHub Actions, this guide will walk you through:

  • What GitHub Actions is and why it's useful
  • How GitHub Actions workflows work
  • Setting up your first GitHub Actions workflow
  • Best practices for using GitHub Actions effectively

By the end of this guide, you'll have a working GitHub Actions workflow and a solid understanding of how to use it to automate tasks in your projects.

What is GitHub Actions?

GitHub Actions is a CI/CD and automation platform that allows you to define workflows that run automatically in response to events in your repository. These workflows can be used to:

âś… Run tests automatically when code is pushed or a pull request is opened.
🚀 Deploy applications to cloud services or servers.
📦 Build and publish packages to registries.
🔄 Automate repetitive tasks like code formatting, issue triaging, and notifications.

GitHub Actions is built into GitHub, so there's no need to set up external CI/CD services. You can define workflows inside your repository using simple YAML configuration files, making automation easy to manage alongside your code.

How GitHub Actions Works

Key Concepts in GitHub Actions

Before setting up a workflow, it's important to understand a few core concepts:

  • Workflows - Automated processes defined in a repository (e.g., run tests, deploy code).
  • Jobs - Individual tasks within a workflow (e.g., "Run unit tests" and "Deploy to production").
  • Steps - Commands executed inside a job (e.g., installing dependencies, running tests).
  • Actions - Pre-built reusable units that perform specific tasks (e.g., checkout code, setup environments).

How Workflows Are Triggered

GitHub Actions workflows can be triggered by different events, such as:

  • Push events - Runs when code is pushed to the repository.
  • Pull requests - Runs when a PR is opened or updated.
  • Scheduled jobs - Runs at set intervals (e.g., nightly builds).
  • Manual triggers - Runs when manually triggered via GitHub UI or API.

Setting Up Your First GitHub Actions Workflow

Step 1: Creating a Workflow File

Workflows in GitHub Actions are defined using YAML files stored inside the .github/workflows/ directory. To create your first workflow:

  1. In your GitHub repository, go to the Code tab.
  2. Click Add file → Create new file.
  3. Name the file .github/workflows/ci.yml.

Step 2: Writing Your First Workflow

Copy and paste the following basic GitHub Actions workflow into your file:

name: First GitHub Actions Workflow
 
on: push # Runs the workflow every time code is pushed to the repository
 
jobs:
    example-job:
        runs-on: ubuntu-latest # The OS environment for the job
        steps:
            - name: Checkout repository
              uses: actions/checkout@v3 # Pulls your repository code into the runner
 
            - name: Print a message
              run: echo "Hello, GitHub Actions!" # Runs a shell command

Step 3: Committing and Running the Workflow

  1. Click Commit new file to save the workflow.
  2. Go to the Actions tab in your repository.
  3. You should see your workflow running automatically after the push.

If everything is set up correctly, the job should complete successfully, and you should see the message "Hello, GitHub Actions!" in the logs. 🎉

Customizing Workflows

Using Environment Variables

You can define environment variables inside your workflow to make scripts more flexible:

env:
    GREETING: "Hello"
    NAME: "GitHub Actions"
 
jobs:
    example-job:
        runs-on: ubuntu-latest
        steps:
            - name: Print a message with variables
              run: echo "$GREETING, $NAME!"

Running Jobs on Different Platforms

By default, GitHub Actions runs on Ubuntu, but you can specify other environments:

jobs:
    test-on-windows:
        runs-on: windows-latest
        steps:
            - name: Run a command
              run: echo "Running on Windows"
 
    test-on-macos:
        runs-on: macos-latest
        steps:
            - name: Run a command
              run: echo "Running on macOS"

Adding Multiple Jobs and Dependencies

Workflows can contain multiple jobs, and you can define dependencies between them:

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout code
              uses: actions/checkout@v3
            - name: Run build script
              run: ./build.sh
 
    test:
        needs: build # This job will run only after the build job completes
        runs-on: ubuntu-latest
        steps:
            - name: Run tests
              run: ./test.sh

Best Practices for GitHub Actions

âś… Keep Workflows Efficient

  • Run only necessary jobs to avoid wasted compute time.
  • Use caching to speed up workflows where possible.

đź”’ Secure Your Secrets

  • Use GitHub Secrets instead of storing sensitive data in the YAML file.
  • Example of accessing a secret:
env:
    API_KEY: ${{ secrets.API_KEY }}

🔄 Avoid Unnecessary Workflow Runs

  • Use conditional execution (if:) to prevent running jobs when they aren't needed.
  • Example: Run a job only on the main branch:
jobs:
    deploy:
        runs-on: ubuntu-latest
        if: github.ref == 'refs/heads/main'
        steps:
            - name: Deploy application
              run: ./deploy.sh

We use cookies to enhance your experience. You can manage your preferences below.