GitHub Actions Introduction
GitHub Actions automates workflows directly in your repository. Build, test, and deploy code; respond to events; automate repetitive tasks—all in GitHub.
What are GitHub Actions?
Actions are automated workflows triggered by events:
Event (push, PR, schedule, etc.)
↓
Workflow runs
↓
Jobs execute
↓
Steps run commands
Key Concepts
Workflows
A configurable automated process:
- Defined in
.github/workflows/*.yml - Triggered by events
- Contains one or more jobs
Events
What triggers a workflow:
push: Code pushedpull_request: PR opened/updatedschedule: Cron scheduleworkflow_dispatch: Manual triggerissues: Issue created/updated- Many more...
Jobs
A set of steps that run on the same runner:
- Jobs run in parallel by default
- Can set dependencies between jobs
Steps
Individual tasks in a job:
- Run commands
- Use actions (reusable units)
Actions
Reusable units of code:
- Official:
actions/checkout,actions/setup-node - Community: Thousands available
- Custom: Create your own
Your First Workflow
Create .github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build
run: npm run build
Workflow Syntax
Triggers (on)
# Push to specific branches
on:
push:
branches: [main, develop]
# Pull requests
on:
pull_request:
branches: [main]
# Scheduled (cron)
on:
schedule:
- cron: '0 0 * * *' # Daily at midnight
# Manual trigger
on:
workflow_dispatch:
# Multiple events
on:
push:
branches: [main]
pull_request:
schedule:
- cron: '0 0 * * 0' # Weekly
Jobs
jobs:
# Job ID (any name)
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building..."
test:
runs-on: ubuntu-latest
needs: build # Wait for build to complete
steps:
- run: echo "Testing..."
deploy:
runs-on: ubuntu-latest
needs: [build, test] # Wait for both
steps:
- run: echo "Deploying..."
Steps
steps:
# Run shell command
- run: npm install
# Use an action
- uses: actions/checkout@v4
# Named step with action and inputs
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
# Multi-line command
- name: Run script
run: |
echo "Line 1"
echo "Line 2"
npm test
Common Actions
actions/checkout
Clone your repository:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history (for tags, etc.)
actions/setup-node
Set up Node.js:
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # Cache dependencies
actions/cache
Cache dependencies:
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
actions/upload-artifact
Save files between jobs:
- uses: actions/upload-artifact@v4
with:
name: build
path: dist/
Environment Variables
env:
# Workflow level
NODE_ENV: production
jobs:
build:
env:
# Job level
CI: true
steps:
- name: Build
env:
# Step level
API_URL: ${{ secrets.API_URL }}
run: npm run build
Secrets
Store sensitive data:
- Go to Settings → Secrets and variables → Actions
- Add secret
- Use in workflow:
steps:
- name: Deploy
env:
API_KEY: ${{ secrets.API_KEY }}
run: ./deploy.sh
Matrix Builds
Test across multiple versions:
jobs:
test:
strategy:
matrix:
node: [18, 20, 22]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm test
This creates 6 jobs (3 Node versions × 2 OS).
Conditional Execution
steps:
# Only on push (not PR)
- run: ./deploy.sh
if: github.event_name == 'push'
# Only on main branch
- run: ./release.sh
if: github.ref == 'refs/heads/main'
# Only if previous step succeeded
- run: ./notify-success.sh
if: success()
# Only if previous step failed
- run: ./notify-failure.sh
if: failure()
# Always run
- run: ./cleanup.sh
if: always()
Common Workflow Examples
CI Pipeline
name: CI
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm test
build:
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm run build
Deploy on Release
name: Deploy
on:
release:
types: [published]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build
- run: ./deploy.sh
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
Viewing Workflow Runs
- Go to "Actions" tab
- Select workflow
- Click run to see details
- View logs for each step
Summary
- Actions automate workflows in your repository
- Workflows are defined in YAML files
- Triggered by events (push, PR, schedule, etc.)
- Jobs contain steps that run commands or actions
- Use matrix builds for cross-platform testing
- Store sensitive data in secrets
- View runs in the Actions tab
In the next lesson, we'll learn about GitHub Releases.
التالي · الدرس 36 من 40
Creating Releases
قراءة 4 دقيقة
أنهيت 35 من 40 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

