Skip to content

GitHub Actions

Automate workflows with GitHub Actions using YAML, runners, jobs, and the marketplace.

What you'll learn

  • What GitHub Actions is and how workflows work
  • Writing workflow YAML files
  • Jobs, steps, runners, and actions
  • Using secrets and environment variables

Concept

What Is GitHub Actions?

GitHub Actions is GitHub's built-in CI/CD platform. You define workflows in YAML files inside the .github/workflows/ directory. A workflow is triggered by events like pushes, pull requests, or schedules. Each workflow contains one or more jobs, and each job contains steps that run on a runner (a virtual machine). Actions can use pre-built actions from the GitHub Marketplace or run custom shell commands.

Workflow Structure

A workflow file starts with on: to define triggers. jobs: defines what to run. Each job specifies a runs-on: environment (like ubuntu-latest) and a list of steps:. Steps can use existing actions with uses: or run commands with run:. The actions/checkout@v4 action is almost always the first step — it clones your repository so subsequent steps can access the code.

Runners and Environments

Runners execute your workflow. GitHub provides hosted runners with Ubuntu, Windows, and macOS. You can also use self-hosted runners on your own infrastructure for security or specialized hardware. Each job runs on a fresh runner — there is no state between jobs unless you use caching or artifacts. Environments let you define protection rules and secrets for different deployment targets like staging and production.

Secrets and Variables

Store sensitive values like API keys and tokens as encrypted secrets in your repository or environment settings. Reference them in workflows with the secrets context. They are masked in logs and never exposed. Non-sensitive configuration can be stored as variables. Never hardcode secrets in workflow files — always use the secrets store.

Reusable Workflows

GitHub Actions supports composite actions and reusable workflows to avoid repetition. Define a reusable workflow in one file and call it from others with uses:. This is useful for standardizing build or deploy steps across multiple repositories. Matrix strategies run the same job across multiple configurations — different OS versions, Node.js versions, or browser targets. Concurrency controls cancel in-progress runs when a new push arrives, saving resources and avoiding stale deployments.

Example

              name: Build and Deploy
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm ci
      - run: npm test
      - run: npm run build
      - name: Deploy
        run: |
          docker build -t myapp:latest .
          docker push registry.example.com/myapp:latest
        env:
          DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
            

This workflow checks out code, sets up Node.js, installs, tests, builds, and deploys a Docker image. The Docker token is injected from encrypted secrets.

Try it

Common mistakes

The mistake

Printing secrets in workflow logs

The fix

GitHub automatically masks secrets in logs, but avoid echoing them or using them in ways that might expose them. Never use secrets in build names or matrix values where they could be visible.

The mistake

Not pinning action versions

The fix

Use full version tags like actions/checkout@v4 or commit SHAs. Avoid @main or @master references — they can change unexpectedly and introduce supply chain risks.

Practice GitHub Actions

1 exercise

Practice GitHub Actions

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors