GitHub Actions for Beginners: Automate Your Workflow in 30 Minutes

GitHub Actions is the built-in automation platform for GitHub repositories. It lets you run code automatically in response to events — pushing code, opening a pull request, scheduling a cron job — without setting up separate CI/CD infrastructure. Here is enough to get started.

What Is a Workflow?

A workflow is a YAML file in your repository’s .github/workflows/ directory. It defines triggers (when to run), jobs (groups of steps), and steps (individual commands). GitHub provides free Linux, Windows, and macOS virtual machines to run them on. The free tier gives 2,000 minutes per month for private repos; public repos run completely free.

Your First Workflow

Create .github/workflows/test.yml. A minimal Python testing workflow looks like:

name: Run Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - run: pip install -r requirements.txt
      - run: pytest

Every push and PR now automatically runs your tests and reports pass/fail on the GitHub interface.

Useful Triggers

Beyond push and pull_request, you can trigger workflows on: schedule (cron syntax), workflow_dispatch (manual button in the GitHub UI), release creation, issue comments, and many more. Scheduled workflows are useful for nightly builds, data fetching jobs, or automated reports.

Secrets and Environment Variables

Store API keys and passwords in GitHub repository Settings → Secrets and variables → Actions. Access them in workflows as ${{ secrets.MY_SECRET }}. Never hardcode credentials in workflow files — they end up in your git history.

Finding Existing Actions

The GitHub Marketplace has thousands of pre-built actions. Common ones: actions/cache (speed up builds by caching dependencies), docker/build-push-action (build and push Docker images), peaceiris/actions-gh-pages (deploy static sites to GitHub Pages).

上一篇 Cursor AI编程工具深度体验:能做什么、不能做什么
下一篇 GitHub Actions入门:30分钟自动化你的工作流