GitHub Actions Real-World Use Cases and Examples

GitHub Actions Real-World Use Cases and Examples

GitHub Actions is one of the most flexible CI/CD tools available today. Beyond basic builds and tests, you can use it for deployments, automating documentation, managing monorepos, building Docker images, and much more.

This article is part of our complete Git and GitHub Actions workflow guide.

In this guide, we’ll walk through real-world GitHub Actions use cases with practical YAML examples that you can adapt for your own projects.


🔹 1. Continuous Integration (CI) for Pull Requests

Automatically run tests when someone opens or updates a pull request.

name: CI Tests
on:
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - run: npm install
      - run: npm test

✅ Ensures no code gets merged without passing tests.


🔹 2. Automatic Deployment to Staging

Deploy to a staging environment whenever code is merged into develop.

name: Deploy to Staging
on:
  push:
    branches: [develop]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm install && npm run build
      - name: Deploy
        run: |
          ssh user@staging "cd /app && git pull && pm2 restart app"
        env:
          SSH_KEY: ${{ secrets.STAGING_SSH_KEY }}


🔹 3. Docker Image Build and Push

Build and push Docker images to a container registry (Docker Hub or GitHub Container Registry).

name: Docker Build
on:
  push:
    branches: [main]

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_PASS }}
      - name: Build & Push Image
        run: |
          docker build -t myapp:latest .
          docker tag myapp:latest myuser/myapp:latest
          docker push myuser/myapp:latest


🔹 4. Scheduled Jobs (Cron)

Run workflows on a schedule (e.g., nightly builds, backups, or security scans).

name: Nightly Build
on:
  schedule:
    - cron: "0 2 * * *" # Runs daily at 2 AM UTC

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm ci && npm run build


🔹 5. Monorepo Path Filtering

Run workflows only when certain directories change.

on:
  push:
    paths:
      - "frontend/**"
      - "backend/**"

📌 Useful for large monorepos where you don’t want to trigger every workflow on every commit.


🔹 6. Notifications (Slack)

Send deployment results to a Slack channel.

- name: Notify Slack
  uses: slackapi/slack-github-action@v1.24.0
  with:
    payload: '{"text":"✅ Production deployment succeeded!"}'
  env:
    SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}


🔹 7. Documentation Deployment

Auto-deploy documentation (e.g., MkDocs, Docusaurus) to GitHub Pages.

name: Deploy Docs
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: 3.x
      - run: pip install mkdocs-material
      - run: mkdocs gh-deploy --force


✅ Key Takeaways

  • GitHub Actions supports a wide range of real-world automation.
  • Common use cases include:
    • CI testing for PRs
    • Deployments to staging/production
    • Docker image builds
    • Scheduled jobs
    • Monorepo workflows with path filtering
    • Notifications to Slack/Teams
    • Automated docs deployment

By combining these patterns, you can build robust CI/CD pipelines for any project.

Frequently Asked Questions

What can GitHub Actions be used for besides CI/CD?

Scheduled jobs, issue and pull request automation, releasing packages, building and pushing Docker images, deploying docs, running security scans, and syncing data between systems.

How do I run a GitHub Actions workflow on a schedule?

Add a schedule trigger with a cron expression in the on block. Scheduled runs can be delayed under load and only run on the default branch.

Can one GitHub Actions workflow deploy to multiple environments?

Yes. Use separate jobs targeting GitHub environments, or a matrix, with needs to order them and environment protection rules to gate production.

How do I limit a workflow to changes in one folder?

Use paths or paths-ignore filters on the push and pull_request triggers so the workflow only runs when files in that path change.

You Might Also Like