Overview
GitHub Actions provides built-in CI/CD capabilities for automating your testing workflow. This guide walks you through setting up automatic test execution on every push and pull request.
Prerequisites
- GitHub repository
- Existing test suite (npm test, pytest, etc.)
- Basic familiarity with YAML
Step 1: Create Workflow Directory
In your repository root, create the workflows directory:
|
|
This is where GitHub Actions looks for workflow definitions.
Step 2: Create Workflow File
Create .github/workflows/test.yml:
|
|
Step 3: Customize for Your Tech Stack
For Python Projects
|
|
For Go Projects
|
|
Step 4: Add Status Badge to README
Add this markdown to your README.md:
|
|
Replace USERNAME and REPO with your values.
Step 5: Commit and Push
|
|
Step 6: Verify Execution
- Navigate to your repository on GitHub
- Click the Actions tab
- You should see your workflow running
- Click on the workflow run to see details
Advanced Configuration
Run on Schedule
Add to the on: section:
|
|
Cache Dependencies
Already included in the example above with:
|
|
This speeds up workflow runs significantly.
Parallel Jobs
Test multiple configurations simultaneously using matrix strategy (shown in example).
Troubleshooting
Tests pass locally but fail in Actions
Common causes:
- Environment variables not set
- Different Node.js/Python versions
- OS-specific dependencies
Solution: Add debugging steps:
|
|
Workflow doesn’t trigger
Check:
- Workflow file is in
.github/workflows/ - YAML syntax is valid (use a validator)
- Branch names match your
on:configuration
Best Practices
- Use
npm ciinstead ofnpm installfor faster, more reliable installs - Cache dependencies to reduce build time
- Set up branch protection requiring tests to pass before merge
- Upload artifacts for test reports and coverage
- Use secrets for API keys and credentials
Next Steps
- Add code coverage reporting (Codecov, Coveralls)
- Set up deployment workflows
- Add linting and formatting checks
- Implement matrix testing across OS platforms