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:
.github/workflows/
Inside this folder, you place YAML files (e.g., ci.yml
) that define your automation rules.
Sample Workflow: Run Tests on Push
# 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 pushedjobs:
β A job namedtest
runs on the latest Ubuntu serversteps:
β It checks out code, sets up Node.js, installs dependencies, and runs tests
Other Common Triggers
Trigger | Description |
---|---|
push | On every push to a branch |
pull_request | When a PR is opened/updated |
schedule | Run periodically (like cron) |
workflow_dispatch | Manual trigger from the GitHub UI |
Popular Actions You Can Use
GitHub Actions comes with thousands of reusable actions from the community:
Action | Purpose |
---|---|
actions/checkout | Clones your repo |
actions/setup-python | Sets up Python |
actions/setup-node | Sets up Node.js |
peaceiris/actions-gh-pages | Deploys site to GitHub Pages |
docker/build-push-action | Builds 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:
- run: echo ${{ secrets.API_KEY }}
Summary: GitHub Actions Essentials
Concept | Meaning |
---|---|
Workflow | A YAML file with automation steps |
Job | A group of steps run on the same machine |
Step | An individual command or action |
Trigger | Event 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.