|
| 1 | +# GitHub Action Development - Best Practices Used |
| 2 | + |
| 3 | +This document outlines the best practices we implemented while creating the CodeAnt CI Scan GitHub Action. |
| 4 | + |
| 5 | +## 1. Action Metadata (action.yml) |
| 6 | + |
| 7 | +### ✅ Proper Action Definition |
| 8 | +- **Clear naming**: Used descriptive `name` and `description` fields |
| 9 | +- **Author identification**: Set proper `author` field |
| 10 | +- **Branding**: Added `icon` and `color` for marketplace visibility |
| 11 | + ```yaml |
| 12 | + branding: |
| 13 | + icon: "shield" |
| 14 | + color: "blue" |
| 15 | + ``` |
| 16 | +
|
| 17 | +### ✅ Input Configuration |
| 18 | +- **Clear descriptions**: Each input has a detailed description |
| 19 | +- **Required vs Optional**: Properly marked required inputs |
| 20 | +- **Default values**: Provided sensible defaults (e.g., `api_base`) |
| 21 | +- **Documentation**: Included examples in descriptions |
| 22 | + |
| 23 | +## 2. Composite Action Best Practices |
| 24 | + |
| 25 | +### ✅ Shell Specification |
| 26 | +**Critical for composite actions**: Always specify `shell` in run steps |
| 27 | +```yaml |
| 28 | +- name: Step name |
| 29 | + shell: bash |
| 30 | + run: | |
| 31 | + # commands here |
| 32 | +``` |
| 33 | + |
| 34 | +### ✅ Error Handling |
| 35 | +- **Fail fast**: Used `set -e` to exit on any error |
| 36 | +- **Explicit error messages**: Added conditional checks with helpful error messages |
| 37 | + ```bash |
| 38 | + if ! curl -fsSL ...; then |
| 39 | + echo "Error: Failed to fetch scan script" |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + ``` |
| 43 | + |
| 44 | +### ✅ Informative Logging |
| 45 | +- Added echo statements to track progress |
| 46 | +- Clear messages at each step for debugging |
| 47 | + |
| 48 | +## 3. Dependency Management |
| 49 | + |
| 50 | +### ✅ Version Pinning |
| 51 | +- Updated to `actions/checkout@v4` (latest stable version) |
| 52 | +- Avoid using `@master` or `@latest` in production |
| 53 | + |
| 54 | +### ✅ Minimal External Dependencies |
| 55 | +- Used standard tools available in GitHub runners (curl, base64, bash) |
| 56 | +- No need for additional setup steps |
| 57 | + |
| 58 | +## 4. Security Best Practices |
| 59 | + |
| 60 | +### ✅ Token Handling |
| 61 | +- Never hardcode tokens |
| 62 | +- Use inputs with clear security requirements |
| 63 | +- Document required permissions in README |
| 64 | + |
| 65 | +### ✅ Secure Script Handling |
| 66 | +- Validate downloaded scripts before execution |
| 67 | +- Check curl exit codes |
| 68 | +- Verify base64 decoding success |
| 69 | + |
| 70 | +### ✅ Fail-Safe Execution |
| 71 | +- Use `curl -fsSL` flags: |
| 72 | + - `-f`: Fail on HTTP errors |
| 73 | + - `-s`: Silent mode |
| 74 | + - `-S`: Show errors |
| 75 | + - `-L`: Follow redirects |
| 76 | + |
| 77 | +## 5. Documentation Best Practices |
| 78 | + |
| 79 | +### ✅ Comprehensive README |
| 80 | +- Clear feature list |
| 81 | +- Input/output documentation in table format |
| 82 | +- Multiple usage examples (basic and advanced) |
| 83 | +- Testing instructions before marketplace publication |
| 84 | +- Required permissions documentation |
| 85 | +- Publishing checklist |
| 86 | + |
| 87 | +### ✅ Usage Examples |
| 88 | +- Provided complete workflow examples |
| 89 | +- Showed both simple and complex use cases |
| 90 | +- Included testing from another repository instructions |
| 91 | + |
| 92 | +## 6. Code Organization |
| 93 | + |
| 94 | +### ✅ Clean Repository Structure |
| 95 | +``` |
| 96 | +codeant-ci-scan/ |
| 97 | +├── action.yml # Action definition |
| 98 | +├── README.md # User documentation |
| 99 | +├── README_LOCAL.md # Development documentation |
| 100 | +└── LICENSE # License file (recommended) |
| 101 | +``` |
| 102 | +
|
| 103 | +### ✅ No Unnecessary Files |
| 104 | +- Removed boilerplate code (main.py) |
| 105 | +- Keep only essential files |
| 106 | +
|
| 107 | +## 7. Action Steps Structure |
| 108 | +
|
| 109 | +### ✅ Logical Step Separation |
| 110 | +1. **Checkout**: Get repository code |
| 111 | +2. **Fetch**: Download scan script |
| 112 | +3. **Prepare**: Decode and make executable |
| 113 | +4. **Execute**: Run the analysis |
| 114 | +
|
| 115 | +### ✅ Clear Step Naming |
| 116 | +- Use descriptive names for each step |
| 117 | +- Avoid generic names like "Run script" |
| 118 | +
|
| 119 | +## 8. Testing Strategy |
| 120 | +
|
| 121 | +### ✅ Before Publishing |
| 122 | +1. **Local testing**: Test in another repository using branch reference |
| 123 | + ```yaml |
| 124 | + uses: username/repo@branch-name |
| 125 | + ``` |
| 126 | + |
| 127 | +2. **Commit testing**: Test specific commits |
| 128 | + ```yaml |
| 129 | + uses: username/repo@commit-sha |
| 130 | + ``` |
| 131 | +
|
| 132 | +3. **Tag testing**: Create and test release tags |
| 133 | + ```yaml |
| 134 | + uses: username/repo@v1.0.0 |
| 135 | + ``` |
| 136 | +
|
| 137 | +### ✅ Test Scenarios |
| 138 | +- Test with minimal required inputs |
| 139 | +- Test with all optional inputs |
| 140 | +- Test error conditions (invalid tokens, unreachable API) |
| 141 | +- Test on different runners (ubuntu, macos, windows if applicable) |
| 142 | +
|
| 143 | +## 9. Marketplace Preparation |
| 144 | +
|
| 145 | +### ✅ Pre-Publication Checklist |
| 146 | +- [ ] Add LICENSE file (required for marketplace) |
| 147 | +- [ ] Create semantic version release (v1.0.0) |
| 148 | +- [ ] Test thoroughly from another repository |
| 149 | +- [ ] Verify branding displays correctly |
| 150 | +- [ ] Ensure README has no placeholder text |
| 151 | +- [ ] Add repository topics/tags |
| 152 | +- [ ] Configure repository description |
| 153 | +- [ ] Add GitHub repository metadata |
| 154 | +
|
| 155 | +### ✅ Release Strategy |
| 156 | +- Use semantic versioning (MAJOR.MINOR.PATCH) |
| 157 | +- Create major version tags (v1) pointing to latest minor/patch |
| 158 | +- Document breaking changes in release notes |
| 159 | +
|
| 160 | +## 10. Common Pitfalls Avoided |
| 161 | +
|
| 162 | +### ❌ Missing Shell Specification |
| 163 | +Composite actions MUST specify shell for run steps |
| 164 | +
|
| 165 | +### ❌ No Error Handling |
| 166 | +Always handle potential failures gracefully |
| 167 | +
|
| 168 | +### ❌ Outdated Action Versions |
| 169 | +Keep dependencies up-to-date (checkout@v4, not v2) |
| 170 | +
|
| 171 | +### ❌ Poor Input Documentation |
| 172 | +Users need to know what each input does and requires |
| 173 | +
|
| 174 | +### ❌ No Branding |
| 175 | +Actions without branding are less discoverable in marketplace |
| 176 | +
|
| 177 | +### ❌ Incomplete README |
| 178 | +Missing usage examples make actions hard to adopt |
| 179 | +
|
| 180 | +### ❌ No Testing Instructions |
| 181 | +Users should know how to test before publishing |
| 182 | +
|
| 183 | +## 11. GitHub Actions Specific Features Used |
| 184 | +
|
| 185 | +### ✅ Context Variables |
| 186 | +```yaml |
| 187 | +${{ github.repository }} # Current repository |
| 188 | +${{ github.sha }} # Commit SHA |
| 189 | +${{ github.ref_name }} # Branch/tag name |
| 190 | +${{ secrets.GITHUB_TOKEN }} # Auto-generated token |
| 191 | +``` |
| 192 | + |
| 193 | +### ✅ Environment Variables |
| 194 | +```yaml |
| 195 | +env: |
| 196 | + API_BASE: ${{ inputs.api_base }} |
| 197 | +``` |
| 198 | +
|
| 199 | +### ✅ Input References |
| 200 | +```yaml |
| 201 | +${{ inputs.access_token }} |
| 202 | +${{ inputs.api_base }} |
| 203 | +``` |
| 204 | + |
| 205 | +## 12. Performance Considerations |
| 206 | + |
| 207 | +### ✅ Efficient Script Fetching |
| 208 | +- Single API call to fetch script |
| 209 | +- No unnecessary downloads or processing |
| 210 | + |
| 211 | +### ✅ Minimal Checkout |
| 212 | +- Uses default checkout settings (fast) |
| 213 | +- Only checks out when needed |
| 214 | + |
| 215 | +## 13. Maintainability |
| 216 | + |
| 217 | +### ✅ Clear Code Comments |
| 218 | +- Self-documenting step names |
| 219 | +- Informative echo messages |
| 220 | + |
| 221 | +### ✅ Version Control |
| 222 | +- Proper git usage |
| 223 | +- Clean commit history |
| 224 | +- Semantic versioning for releases |
| 225 | + |
| 226 | +### ✅ Documentation |
| 227 | +- Both user-facing (README.md) and developer-facing (README_LOCAL.md) |
| 228 | +- Inline documentation in action.yml |
| 229 | + |
| 230 | +## Summary |
| 231 | + |
| 232 | +This action follows GitHub's official best practices for: |
| 233 | +- Composite action structure |
| 234 | +- Security and token handling |
| 235 | +- Error handling and logging |
| 236 | +- Documentation and examples |
| 237 | +- Testing and marketplace publication |
| 238 | + |
| 239 | +Following these practices ensures a reliable, secure, and maintainable GitHub Action that users can trust and easily integrate into their workflows. |
0 commit comments