|
| 1 | +# Local Testing for GitHub Actions |
| 2 | + |
| 3 | +This document explains how to test your GitHub Actions workflows locally before pushing to GitHub. |
| 4 | + |
| 5 | +## 🧪 Methods to Test GitHub Actions Locally |
| 6 | + |
| 7 | +### 1. **Direct Command Testing** (Recommended) |
| 8 | + |
| 9 | +Run the exact same commands that GitHub Actions uses: |
| 10 | + |
| 11 | +```bash |
| 12 | +# Test linting (same as CI) |
| 13 | +$(go env GOPATH)/bin/golangci-lint run --timeout=5m |
| 14 | + |
| 15 | +# Test building |
| 16 | +go build -v ./... |
| 17 | + |
| 18 | +# Test with race detection and coverage |
| 19 | +go test -v -race -coverprofile=coverage.out ./... |
| 20 | + |
| 21 | +# Generate coverage report |
| 22 | +go tool cover -html=coverage.out -o coverage.html |
| 23 | + |
| 24 | +# Run security scanner (if you have it) |
| 25 | +# gosec ./... |
| 26 | +``` |
| 27 | + |
| 28 | +### 2. **Using Makefile** (Convenient) |
| 29 | + |
| 30 | +Use the local CI command: |
| 31 | + |
| 32 | +```bash |
| 33 | +# Install dependencies and run all CI checks |
| 34 | +make ci-local |
| 35 | + |
| 36 | +# Individual commands |
| 37 | +make lint # Run linting |
| 38 | +make test-race # Run tests with race detection |
| 39 | +make coverage # Generate coverage report |
| 40 | +make build # Build the project |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. **Using `act` Tool** (Full GitHub Actions Simulation) |
| 44 | + |
| 45 | +Install and use `act` to run GitHub Actions locally with Docker: |
| 46 | + |
| 47 | +```bash |
| 48 | +# Install act (requires Docker) |
| 49 | +brew install act |
| 50 | + |
| 51 | +# Run specific jobs |
| 52 | +act -j lint # Run just the lint job |
| 53 | +act -j test # Run just the test job |
| 54 | +act -j integration-test # Run integration tests |
| 55 | +act # Run all jobs |
| 56 | + |
| 57 | +# Run with specific event |
| 58 | +act push # Simulate push event |
| 59 | +act pull_request # Simulate PR event |
| 60 | +``` |
| 61 | + |
| 62 | +### 4. **VS Code Extensions** |
| 63 | + |
| 64 | +- **GitHub Actions**: Syntax highlighting and validation |
| 65 | +- **YAML**: Better YAML editing experience |
| 66 | + |
| 67 | +## 🔧 Local Setup |
| 68 | + |
| 69 | +### Install Required Tools |
| 70 | + |
| 71 | +```bash |
| 72 | +# Install golangci-lint |
| 73 | +go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest |
| 74 | + |
| 75 | +# Install goimports (for formatting) |
| 76 | +go install golang.org/x/tools/cmd/goimports@latest |
| 77 | + |
| 78 | +# Install act (optional, requires Docker) |
| 79 | +brew install act |
| 80 | +``` |
| 81 | + |
| 82 | +### Verify Installation |
| 83 | + |
| 84 | +```bash |
| 85 | +# Check golangci-lint |
| 86 | +$(go env GOPATH)/bin/golangci-lint --version |
| 87 | + |
| 88 | +# Check Go tools |
| 89 | +go version |
| 90 | +``` |
| 91 | + |
| 92 | +## 🚀 Quick Test Script |
| 93 | + |
| 94 | +Create a `test-ci.sh` script: |
| 95 | + |
| 96 | +```bash |
| 97 | +#!/bin/bash |
| 98 | +set -e |
| 99 | + |
| 100 | +echo "🔍 Running local CI tests..." |
| 101 | + |
| 102 | +echo "📦 Building..." |
| 103 | +go build -v ./... |
| 104 | + |
| 105 | +echo "🧪 Running tests..." |
| 106 | +go test -v -race ./... |
| 107 | + |
| 108 | +echo "🔍 Running linter..." |
| 109 | +$(go env GOPATH)/bin/golangci-lint run --timeout=5m |
| 110 | + |
| 111 | +echo "✅ All local CI tests passed!" |
| 112 | +``` |
| 113 | + |
| 114 | +Make it executable and run: |
| 115 | + |
| 116 | +```bash |
| 117 | +chmod +x test-ci.sh |
| 118 | +./test-ci.sh |
| 119 | +``` |
| 120 | + |
| 121 | +## 📋 Common Issues and Solutions |
| 122 | + |
| 123 | +### Linting Issues |
| 124 | + |
| 125 | +**Problem**: `golangci-lint` reports many issues |
| 126 | +**Solution**: |
| 127 | +1. Run `$(go env GOPATH)/bin/goimports -w .` to fix imports |
| 128 | +2. Check `.golangci.yml` configuration |
| 129 | +3. Fix critical issues, exclude overly strict rules |
| 130 | + |
| 131 | +**Problem**: Import formatting issues |
| 132 | +**Solution**: |
| 133 | +```bash |
| 134 | +$(go env GOPATH)/bin/goimports -w . |
| 135 | +``` |
| 136 | + |
| 137 | +### Test Issues |
| 138 | + |
| 139 | +**Problem**: Tests hang or timeout |
| 140 | +**Solution**: |
| 141 | +1. Add `t.Parallel()` to tests |
| 142 | +2. Use in-memory databases for testing |
| 143 | +3. Add proper cleanup functions |
| 144 | +4. Set reasonable timeouts |
| 145 | + |
| 146 | +### Build Issues |
| 147 | + |
| 148 | +**Problem**: Build fails locally but works in CI |
| 149 | +**Solution**: |
| 150 | +1. Check Go version compatibility |
| 151 | +2. Run `go mod tidy` |
| 152 | +3. Ensure all dependencies are available |
| 153 | + |
| 154 | +## 🎯 Best Practices |
| 155 | + |
| 156 | +1. **Test Early**: Run local tests before every commit |
| 157 | +2. **Use Makefile**: Standardize common commands |
| 158 | +3. **Parallel Tests**: Use `t.Parallel()` for faster execution |
| 159 | +4. **Clean Setup**: Use proper test setup and cleanup |
| 160 | +5. **Reasonable Timeouts**: Don't let tests hang indefinitely |
| 161 | + |
| 162 | +## 📊 Performance Tips |
| 163 | + |
| 164 | +- **Parallel Execution**: Tests run in parallel by default |
| 165 | +- **In-Memory Databases**: Use `:memory:` for SQLite tests |
| 166 | +- **Minimal Setup**: Only create what you need for tests |
| 167 | +- **Proper Cleanup**: Clean up resources to avoid conflicts |
| 168 | + |
| 169 | +## 🔗 Related Files |
| 170 | + |
| 171 | +- `.golangci.yml` - Linting configuration |
| 172 | +- `Makefile` - Build and test commands |
| 173 | +- `.github/workflows/ci.yml` - GitHub Actions workflow |
| 174 | +- `.github/workflows/release.yml` - Release workflow |
| 175 | + |
| 176 | +This approach ensures your code passes CI before you push, saving time and avoiding failed builds! |
0 commit comments