Git & GitHub ยท Chapter 34 of 42

GitHub Actions Basics

GitHub Actions is a built-in automation tool that runs workflows in response to events like pushes or pull requests. It's commonly used for continuous integration (CI), running tests automatically on every change.

Workflows are defined in YAML files stored in a `.github/workflows` folder in your repository, describing what triggers the workflow and what steps to run.

Syntax
name: CI
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm test

Creating a workflow

Add a YAML file like `.github/workflows/ci.yml` describing triggers (like push or pull_request) and jobs containing steps such as checking out code and running tests.

Common use cases

GitHub Actions is used for running automated tests, linting code, building and deploying applications, and publishing packages, all automatically on every push or PR.

Example 1 (bash)
mkdir -p .github/workflows
cat > .github/workflows/ci.yml << 'YML'
name: CI
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm test
YML

Creates a basic GitHub Actions workflow that runs tests every time code is pushed.

Example 2 (bash)
git add .github/workflows/ci.yml
git commit -m "Add CI workflow"
git push
Output
[main 8b2f1aa] Add CI workflow

Committing and pushing the workflow file activates it on GitHub, running automatically on the next push.

Key points

  • GitHub Actions automates workflows like testing and deployment.
  • Workflows are defined in YAML files in .github/workflows.
  • Common triggers include push and pull_request events.
  • Actions are widely used for continuous integration (CI).
๐Ÿ’ก Note: Green checkmarks on a pull request usually mean the GitHub Actions workflow (like tests) passed successfully.

๐Ÿ“ Quick Quiz

1. What is GitHub Actions used for?

2. Where are GitHub Actions workflows defined?

3. What is a common trigger for a GitHub Actions workflow?