Many of us use GitHub to host our projects, and you've probably already heard of GitHub Actions. It's a pretty handy tool for automating certain tasks, such as running tests, building an application, publishing a package, or deploying a website.

The good news is that you don't need to develop all this automation yourself. GitHub Actions relies on YAML files to describe the different steps to execute.

Let's look at a few basic examples.

What is GitHub Actions?

GitHub Actions is an automation system built into GitHub that allows you to trigger workflows in response to different events in a repository.

For example, you can automatically run tests when a developer pushes code, check a pull request, or deploy an application after a new release is created.

These automations are defined in YAML files. We like this data format on this site.

Each workflow file must be placed in the repository's .github/workflows/ directory. It can use either the .yml or .yaml extension. Here's an example of how things might be organized:

my-project/
├── .github/
│   └── workflows/
│       ├── tests.yml
│       └── deploy.yml
├── src/
├── tests/
└── README.md

Each file corresponds to an independent workflow. So you can create several files to separate tests, deployments, or other tasks.

Structure of a GitHub Actions YAML file

A workflow consists of several elements, the main ones being:

  • name: the name of the workflow.
  • on: the events that trigger its execution.
  • jobs: the jobs to be performed.
  • steps: the different steps of a job.

Let's start with a fairly simple example:

name: PHP Tests

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

jobs:
  tests:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install dependencies
        run: composer install

      - name: Run tests
        run: vendor/bin/phpunit

This workflow automatically runs PHP tests whenever a commit is pushed to the main branch or when a pull request targets that branch.

Let's break down the different elements to make sure we understand them.

Events with on

The on property allows you to define the events that trigger a workflow.

GitHub Actions supports several events. The most common ones are:

  • push: when a commit is pushed to the repository.
  • pull_request: when a pull request is created or modified.
  • schedule: to run a workflow at regular intervals.
  • workflow_dispatch: to manually trigger a workflow.
  • release: when a release is published.

For example, you can trigger a workflow on every push:

name: Build

on: push

Or only when a commit is pushed to a specific branch:

name: Build

on:
  push:
    branches:
      - main
      - develop

You can also combine several events:

name: CI

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

In this example, the workflow will run when a push is made to main, when a pull request is opened or updated, but also when we trigger it manually from GitHub. You can also filter events based on modified files, branches, or tags. This is pretty handy for avoiding unnecessary tasks.

Jobs: Organizing the work

The jobs property allows you to define the different jobs that make up a workflow. Each job has a unique identifier and can contain several properties, including:

  • name: the name displayed in GitHub.
  • runs-on: the system on which the job will run.
  • steps: the steps to execute.
  • needs: dependencies on other jobs.

Here's an example with two jobs:

name: CI

on: push

jobs:
  tests:
    runs-on: ubuntu-latest
    steps:
      - name: Run tests
        run: echo "Running tests..."

  build:
    runs-on: ubuntu-latest
    steps:
      - name: Build application
        run: echo "Building application..."

By default, jobs run in parallel. In our example, the tests and the application build can therefore run simultaneously to save time (but this uses more resources).

But let's imagine that we only want to build the application if the tests pass. We can use needs:

jobs:
  tests:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running tests..."

  build:
    needs: tests
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building application..."

This time, the build job will wait for the tests job to finish. If the tests fail, the build job will normally be skipped.

Runners with runs-on

Each job must run on a machine called a runner. GitHub provides hosted runners with different operating systems:

jobs:
  linux:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running on Linux"

  windows:
    runs-on: windows-latest
    steps:
      - run: echo "Running on Windows"

  macos:
    runs-on: macos-latest
    steps:
      - run: echo "Running on macOS"

You can also use your own machines with self-hosted runners.

This is useful if you want to control the execution environment or use specific hardware resources.

Steps: Executing the different steps

The steps represent the different operations performed by a job. There are mainly two ways to define them:

  • run: directly execute a command.
  • uses: use an existing action.

For example, to execute a few commands:

steps:
  - name: Display PHP version
    run: php -v

  - name: Install dependencies
    run: composer install

  - name: Run tests
    run: vendor/bin/phpunit

You can also execute several commands in a single step:

steps:
  - name: Build application
    run: |
      composer install
      npm install
      npm run build

Here, the | character allows us to represent a multi-line YAML string. This is particularly handy for scripts.

Using actions with uses

Rather than rewriting certain operations, you can use actions that have already been developed and published on GitHub. Handy.

For example, actions/checkout allows you to retrieve the repository contents in the runner's environment:

steps:
  - name: Checkout repository
    uses: actions/checkout@v6

There are also actions for setting up different environments, caching dependencies, or publishing artifacts.

Some actions require additional parameters. You can pass them using the with property:

steps:
  - name: Setup Node.js
    uses: actions/setup-node@v7
    with:
      node-version: 22

In this example, the action sets up Node.js version 22.

It's best to specify a version for the actions you use. For sensitive workflows, you can even reference a commit SHA directly to pin down exactly which code is executed.

Environment variables with env

You can also define environment variables at different levels of a workflow:

  • At the global level, for the entire workflow.
  • At the job level, for all its steps.
  • At the step level, for that step only.

For example:

name: Build

on: push

env:
  APP_ENV: production

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      APP_VERSION: 1.0.0

    steps:
      - name: Display configuration
        run: |
          echo "Environment: $APP_ENV"
          echo "Version: $APP_VERSION"

You can also use GitHub Actions expressions to retrieve certain information:

env:
  REPOSITORY: ${{ github.repository }}
  BRANCH: ${{ github.ref_name }}

The ${{ }} expressions allow you to access GitHub contexts, variables, secrets, and the results of other steps.

Managing secrets

Some workflows need sensitive information, such as an API key, a token, or credentials to connect to a server.

Obviously, you shouldn't put this information directly in the YAML file!

GitHub provides a secrets system that allows you to store these values in the repository settings.

You can then use them in a workflow:

steps:
  - name: Deploy application
    run: ./deploy.sh
    env:
      API_TOKEN: ${{ secrets.API_TOKEN }}

Here, the API_TOKEN variable retrieves the value of the API_TOKEN secret configured in GitHub.

This is much cleaner than leaving credentials lying around in a Git repository. However, be careful not to display these values in logs or pass them to actions you don't trust.

Complete example: Automating tests for a PHP project

Come on, let's move on to a slightly more concrete example, using a PHP project (I like PHP too, and the more people hate it, the more I like it...)! Let's imagine that we want to automate tests for a PHP project on every push to the main branch and every pull request.

Here's our .github/workflows/tests.yml file:

name: PHP Tests

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

jobs:
  tests:
    name: Run PHPUnit
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.4'
          coverage: none

      - name: Install dependencies
        run: composer install --no-interaction --prefer-dist

      - name: Run PHPUnit
        run: vendor/bin/phpunit

Let's see what happens:

  1. GitHub detects a push to main or a pull request targeting that branch.
  2. It starts an Ubuntu runner.
  3. It retrieves the repository contents using actions/checkout.
  4. It installs PHP 8.4 using the setup-php action.
  5. It installs the dependencies with Composer.
  6. It runs PHPUnit.

And that's it! We now have a system that automatically checks our code whenever it changes.

If a test fails, GitHub indicates that the workflow has failed. We can then check the logs to identify the problem.

Some common mistakes with YAML files

Even though GitHub Actions workflows are relatively easy to write, certain mistakes can quickly waste your time. Here are a few pitfalls to avoid.

Watch out for indentation

As always with YAML, indentation matters. I should write an article about that. Incorrect indentation can change the structure of the workflow or make the file invalid.

For example, this is incorrect:

jobs:
tests:
  runs-on: ubuntu-latest

You need to respect the hierarchy of the properties:

jobs:
  tests:
    runs-on: ubuntu-latest

Don’t confuse run and uses

Both properties allow you to define a step, but they don't work in the same way.

run executes a command on the runner, while uses calls an existing action.

steps:
  - name: Display message
    run: echo "Hello!"

  - name: Checkout repository
    uses: actions/checkout@v6

Watch out for strings that look like numbers

Some values should be represented as strings.

For example, for a PHP version, it's better to write:

with:
  php-version: '8.4'

This avoids any ambiguity about the value's type.

Don’t forget permissions

Workflows have access to a GITHUB_TOKEN that allows them to interact with the repository.

It's best to limit its permissions to what is strictly necessary rather than automatically granting it write access.

You can define permissions directly in the workflow:

permissions:
  contents: read

Here, the workflow only has read access to the repository contents.

Conclusion

GitHub Actions is a really handy tool for automating development tasks. And thanks to YAML, it's relatively easy to describe the events, jobs, and different steps of a workflow.

If you use GitHub for your projects, there's really no excuse not to automate a few repetitive tasks :)!

GitHub Actions: Understanding workflow files