Explain the basic structure of a GitHub Actions workflow file.

Beginner

Answer

A GitHub Actions workflow is defined in a YAML file within the .github/workflows/ directory. The basic structure includes:

name: CI Pipeline
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
      - name: Install dependencies
        run: npm install
      - name: Run tests
        run: npm test

Key components:

  • name: Optional workflow name
  • on: Events that trigger the workflow
  • jobs: Collection of steps that execute on the same runner
  • runs-on: Specifies the runner environment
  • steps: Individual tasks within a job
  • uses: References pre-built actions
  • run: Executes shell commands