πŸ’‘ Ask Tutor

Introduction to GitHub Actions

Imagine if your code could automatically test itself, deploy to production, or generate reports β€” without you doing anything manually. That’s what GitHub Actions enables: automation, right inside your repository.

GitHub Actions is a powerful CI/CD (Continuous Integration and Continuous Deployment) feature built into GitHub that allows you to automate workflows based on triggers like commits, pull requests, or even time schedules.

What Can You Do With GitHub Actions?

  • βœ… Run tests automatically when code is pushed
  • βœ… Build and deploy projects to servers or cloud platforms
  • βœ… Format or lint code every time it’s committed
  • βœ… Send notifications or create backup files on schedule
  • βœ… Automatically label issues or assign reviewers

How GitHub Actions Work

GitHub Actions uses a special folder in your repo:

Bash
.github/workflows/

Inside this folder, you place YAML files (e.g., ci.yml) that define your automation rules.

Sample Workflow: Run Tests on Push

YAML
# File: .github/workflows/test.yml
name: Run Tests

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'
      - run: npm install
      - run: npm test

Explanation:

  • on: [push] β†’ Runs this workflow every time code is pushed
  • jobs: β†’ A job named test runs on the latest Ubuntu server
  • steps: β†’ It checks out code, sets up Node.js, installs dependencies, and runs tests

Other Common Triggers

TriggerDescription
pushOn every push to a branch
pull_requestWhen a PR is opened/updated
scheduleRun periodically (like cron)
workflow_dispatchManual trigger from the GitHub UI

GitHub Actions comes with thousands of reusable actions from the community:

ActionPurpose
actions/checkoutClones your repo
actions/setup-pythonSets up Python
actions/setup-nodeSets up Node.js
peaceiris/actions-gh-pagesDeploys site to GitHub Pages
docker/build-push-actionBuilds and pushes Docker images

You can explore more at: https://github.com/marketplace/actions

Secrets and Security

You can store sensitive data like API keys in GitHub:

  • Go to your repo β†’ Settings β†’ Secrets and variables
  • Add a new secret, e.g., API_KEY
  • Use it in your workflow:
YAML
- run: echo ${{ secrets.API_KEY }}

Summary: GitHub Actions Essentials

ConceptMeaning
WorkflowA YAML file with automation steps
JobA group of steps run on the same machine
StepAn individual command or action
TriggerEvent that starts the workflow

GitHub Actions is free for public repositories and comes with generous limits for private ones. Once you get the hang of it, you can automate everything β€” from testing to deployment.

πŸ“˜ Next Topic

Real-World GitHub Project Workflow β†’