GitHub Actions CI/CD
Comprehensive guide to the GitHub Actions workflows for continuous integration and deployment.
Overview
The template includes two main GitHub Actions workflows:
- CI Pipeline (
ci.yml
) - Quality gates and testing - Documentation Deployment (
deploy-docs.yml
) - VitePress to GitHub Pages
CI Pipeline (.github/workflows/ci.yml
)
Triggers
- Push to main branch
- Pull requests to main branch
Jobs
Quality Gates
yaml
- name: Run quality checks
run: npm run check
- name: Build project
run: npm run build
- name: Generate API documentation
run: npm run docs:api
- name: Build documentation
run: npm run docs:build
Multi-Version Testing
- Node.js 18 (LTS)
- Node.js 20 (Current)
Security Auditing
yaml
- name: Audit dependencies
run: npm audit --audit-level=moderate
Quality Standards
- ✅ All ESLint rules pass
- ✅ Prettier formatting enforced
- ✅ 80%+ test coverage required
- ✅ TypeScript compilation succeeds
- ✅ Documentation builds successfully
Documentation Deployment (.github/workflows/deploy-docs.yml
)
Triggers
- Push to main branch (deploys)
- Pull requests to main branch (builds only)
Deployment Process
- Generate API docs with TypeDoc
- Build VitePress documentation
- Deploy to GitHub Pages automatically
Configuration
yaml
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
Setup Instructions
1. Enable GitHub Pages
- Go to repository Settings → Pages
- Set source to GitHub Actions
- Save configuration
2. Configure Secrets (if needed)
For private repositories or additional integrations:
bash
# Example secrets (not required for basic setup)
NPM_TOKEN=your_npm_token
CODECOV_TOKEN=your_codecov_token
3. Branch Protection
Recommended branch protection rules for main
:
- ✅ Require status checks to pass
- ✅ Require branches to be up to date
- ✅ Include administrators
- ✅ Restrict pushes that create files
Workflow Customization
Adding New Quality Gates
yaml
- name: Custom quality check
run: npm run custom:check
- name: Security scan
run: npm run security:scan
Environment-Specific Builds
yaml
strategy:
matrix:
node-version: [18, 20]
environment: [development, production]
Conditional Steps
yaml
- name: Deploy to staging
if: github.ref == 'refs/heads/develop'
run: npm run deploy:staging
- name: Deploy to production
if: github.ref == 'refs/heads/main'
run: npm run deploy:production
Monitoring and Debugging
Workflow Status
- Green checkmark: All checks passed
- Red X: One or more checks failed
- Yellow circle: Checks in progress
Debugging Failed Builds
- Click on failed workflow in Actions tab
- Expand failed step to see error details
- Check logs for specific error messages
- Re-run jobs if needed
Common Issues
Node.js Version Mismatch
yaml
# Ensure consistent Node.js version
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
Cache Issues
yaml
# Clear cache if needed
- name: Clear npm cache
run: npm cache clean --force
Permission Issues
yaml
# Ensure proper permissions
permissions:
contents: read
pages: write
id-token: write
Performance Optimization
Caching Dependencies
yaml
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
Parallel Jobs
yaml
jobs:
test:
# Test job
build:
# Build job
deploy:
needs: [test, build]
# Deploy only after test and build succeed
Conditional Execution
yaml
- name: Skip on draft PRs
if: github.event.pull_request.draft == false
run: npm run expensive:check
Security Best Practices
Dependency Auditing
yaml
- name: Audit dependencies
run: npm audit --audit-level=moderate
- name: Check for vulnerabilities
run: npm run security:check
Secret Management
- ✅ Use GitHub Secrets for sensitive data
- ✅ Limit secret access to necessary workflows
- ✅ Rotate secrets regularly
- ✅ Use least-privilege principle
Code Scanning
yaml
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: typescript
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
Integration Examples
Slack Notifications
yaml
- name: Notify Slack
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
webhook_url: ${{ secrets.SLACK_WEBHOOK }}
Code Coverage
yaml
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage/lcov.info
Deployment Status
yaml
- name: Update deployment status
uses: bobheadxi/deployments@v1
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: success
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
Troubleshooting Guide
Build Failures
- Check Node.js version compatibility
- Verify dependencies are correctly installed
- Review error logs for specific issues
- Test locally with same Node.js version
Deployment Issues
- Verify GitHub Pages is enabled
- Check permissions are correctly set
- Ensure base URL matches repository name
- Review deployment logs for errors
Performance Issues
- Enable caching for dependencies
- Use matrix builds for parallel execution
- Optimize test suites for faster execution
- Consider workflow triggers to reduce unnecessary runs