GitHub Actions Workflow
๐ฅ Vibe Prompt
"Create a GitHub Actions workflow: Node.js 20, triggered on push to main, npm ci, lint, test, build."
name: CI Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: npm test
- run: npm run build
Anatomy of a Workflow
| Component | Description |
|-----------|-------------|
| on | Trigger event |
| jobs | Groups of steps |
| runs-on | Runner type |
| steps | Individual actions |
| uses | Reusable action |
| run | Shell command |
Chapter Summary
- Understand core concepts and principles
- Master implementation methods and techniques
- Familiar with common issues and solutions
- Able to apply in real projects
Further Reading
- Official documentation and API references
- Open source examples on GitHub
- Technical books and online courses
- Community discussions and tech blogs
Implementation Example
Basic Example
# This section provides a complete implementation example
Steps
- Setup: Configure development environment
- Data: Prepare required data
- Implementation: Build core functionality
- Testing: Verify correctness
- Optimization: Improve performance
Common Errors
| Error Type | Cause | Solution | |------------|-------|----------| | Compilation | Syntax | Check code syntax | | Runtime | Environment | Verify dependencies installed | | Logic | Algorithm | Step-by-step debugging | | Performance | Efficiency | Use profilers |
Code Example
import sys
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
References
- Official documentation
- API reference
- Open source examples
- Community discussions
GitHub Actions Workflow Structure
A workflow is an automated process defined in YAML. Each workflow lives in .github/workflows/.
name: CI Pipeline # Workflow name (displayed in GitHub UI)
on: # Trigger events
push:
branches: [main]
pull_request:
branches: [main]
jobs: # Tasks to execute
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
Workflow Components
| Component | Description |
|-----------|-------------|
| name | Display name for the workflow |
| on | Event that triggers the workflow |
| jobs | Collection of jobs to execute |
| runs-on | Runner OS (ubuntu, windows, macos) |
| steps | Individual tasks within a job |
| uses | Reuse an action from the marketplace |
| run | Execute a shell command |
| with | Pass inputs to an action |
| env | Set environment variables |
| needs | Dependencies between jobs |
Trigger Events
| Event | When It Fires |
|-------|-------------|
| push | Code pushed to a branch |
| pull_request | PR opened, synchronized, reopened |
| schedule | Cron-based schedule |
| workflow_dispatch | Manual trigger (UI button) |
| release | Release published |
| issues | Issue opened, closed |
| deployment | Deployment created |
| registry_package | Package published |
Marketplace Actions
| Action | Purpose | Creator |
|--------|---------|---------|
| actions/checkout | Check out repository | GitHub |
| actions/setup-node | Install Node.js | GitHub |
| actions/setup-python | Install Python | GitHub |
| docker/login-action | Login to container registry | Docker |
| docker/build-push-action | Build and push Docker image | Docker |
| amondnet/vercel-action | Deploy to Vercel | Community |
| aws-actions/configure-aws-credentials | AWS access | AWS |
| azure/webapps-deploy | Deploy to Azure | Microsoft |
| google-github-actions/auth | GCP auth | Google |
| codecov/codecov-action | Upload coverage reports | Codecov |
Environment Variables
jobs:
deploy:
runs-on: ubuntu-latest
env:
NODE_ENV: production
API_URL: https://api.example.com
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build
env:
NEXT_PUBLIC_API_URL: https://api.example.com
- run: npm test
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
Secrets
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: |
curl -X POST https://api.example.com/deploy \
-H "Authorization: Bearer ${{ secrets.DEPLOY_TOKEN }}"
Job Dependencies
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run lint
test:
needs: [lint] # Runs AFTER lint succeeds
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
build:
needs: [test] # Runs AFTER test succeeds
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
Matrix Builds
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [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-version }}
- run: npm ci && npm test
Caching Dependencies
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # Automatically caches ~/.npm
- run: npm ci
- run: npm test
Summary
GitHub Actions automates CI/CD workflows directly in your repository. Define workflows in YAML, trigger on push/PR/schedule, use marketplace actions, manage secrets, and cache dependencies for speed.
Key takeaways:
- Workflows are YAML files in
.github/workflows/ - Trigger on push, PR, schedule, or manually
- Jobs run in parallel by default; use
needsfor dependencies - Use
matrixto test multiple versions/configs - Cache dependencies with
cache: npmoractions/cache - Secrets stored in GitHub, referenced as
${{ secrets.NAME }} - Marketplace: 20,000+ reusable actions
- Environment variables at workflow, job, and step levels
What's Next: Docker Push
The next chapter covers building and pushing Docker images in CI/CD pipelines.