Git Notes
Workflows are the heart of GitHub Actions — they define what happens, when it happens, and how it happens. A workflow is an automated process described in a...
Workflows are the heart of GitHub Actions — they define what happens, when it happens, and how it happens. A workflow is an automated process described in a YAML file that lives in your repository. It specifies the triggers, the machines to run on, and the exact sequence of commands to execute. Mastering workflow configuration is essential for building efficient CI/CD pipelines.
Workflow File Structure
Every workflow lives in .github/workflows/ and follows a specific structure:
Trigger Events in Detail
Push and Pull Request Events
Scheduled Workflows
on:
schedule:
- cron: '0 6 * * MON-FRI' # Weekdays at 6 AM UTC
- cron: '0 0 1 * *' # First day of each monthCron syntax: minute hour day-of-month month day-of-week
Manual Triggers
on:
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: true
type: choice
options:
- staging
- production
dry_run:
description: 'Dry run (no actual deployment)'
type: boolean
default: falseAccess inputs in steps: ${{ github.event.inputs.environment }}
Repository Events
Job Configuration
Basic Job Structure
jobs:
build:
name: Build Application
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run buildJob Dependencies
Matrix Strategy
Run the same job with different configurations:
Steps Deep Dive
Running Commands
steps:
# Single command
- run: echo "Hello"
# Multi-line script
- run: |
echo "Line 1"
echo "Line 2"
npm test
# Different shell
- run: Get-Process
shell: pwshUsing Actions
steps:
# GitHub official action
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for versioning
# Community action
- uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
# Action from a specific commit
- uses: owner/repo@abc123Conditional Steps
steps:
- name: Deploy to production
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: deploy-prod.sh
- name: Notify on failure
if: failure()
run: send-alert.sh
- name: Always cleanup
if: always()
run: cleanup.shOutputs and Communication Between Jobs
jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get-version.outputs.version }}
steps:
- id: get-version
run: echo "version=$(cat VERSION)" >> $GITHUB_OUTPUT
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Deploying version ${{ needs.build.outputs.version }}"Reusable Workflows
Create reusable workflows that other workflows can call:
# .github/workflows/reusable-deploy.yml
name: Reusable Deploy
on:
workflow_call:
inputs:
environment:
required: true
type: string
secrets:
deploy_key:
required: true
jobs:
deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- run: echo "Deploying to ${{ inputs.environment }}"Call it from another workflow:
jobs:
deploy-staging:
uses: ./.github/workflows/reusable-deploy.yml
with:
environment: staging
secrets:
deploy_key: ${{ secrets.STAGING_KEY }}Concurrency Control
Prevent multiple instances of the same workflow from running simultaneously:
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true # Cancel previous runs for same branchKey Takeaways
Workflows are YAML-defined automation pipelines triggered by repository events. Master the trigger system to control when workflows run, use matrix strategies for comprehensive testing, leverage job dependencies for ordered execution, and employ reusable workflows to eliminate duplication. Well-structured workflows are the foundation of reliable CI/CD — they encode your team's quality standards and deployment procedures into executable, version-controlled automation.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for GitHub Actions Workflows.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, actions, workflows, github actions workflows
Related Git & GitHub Topics