|
| 1 | +# AWS CodeBuild Buildspec Validator |
| 2 | + |
| 3 | +A Python script to validate AWS CodeBuild `buildspec.yml` files for syntax errors, structural issues, and best practices. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **YAML Syntax Validation**: Ensures buildspec files are valid YAML |
| 8 | +- **Structure Validation**: Checks for required fields (`version`, `phases`) |
| 9 | +- **Type Checking**: Validates that commands are strings, not accidentally parsed as objects |
| 10 | +- **Best Practices**: Warns about unknown phases or deprecated features |
| 11 | +- **Multi-file Support**: Can validate multiple buildspec files at once using glob patterns |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +The validator requires Python 3.6+ and PyYAML: |
| 16 | + |
| 17 | +```bash |
| 18 | +pip install pyyaml |
| 19 | +``` |
| 20 | + |
| 21 | +For development environments with externally-managed Python (like macOS with Homebrew), create a virtual environment: |
| 22 | + |
| 23 | +```bash |
| 24 | +python3 -m venv .venv |
| 25 | +source .venv/bin/activate |
| 26 | +pip install pyyaml |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Validate a single file |
| 32 | + |
| 33 | +```bash |
| 34 | +python3 scripts/validate_buildspec.py patterns/pattern-2/buildspec.yml |
| 35 | +``` |
| 36 | + |
| 37 | +### Validate multiple files with glob patterns |
| 38 | + |
| 39 | +```bash |
| 40 | +python3 scripts/validate_buildspec.py patterns/*/buildspec.yml |
| 41 | +``` |
| 42 | + |
| 43 | +### Using the Makefile target |
| 44 | + |
| 45 | +```bash |
| 46 | +make validate-buildspec |
| 47 | +``` |
| 48 | + |
| 49 | +This is also included in the `make lint` command. |
| 50 | + |
| 51 | +## Output |
| 52 | + |
| 53 | +The validator provides clear output with: |
| 54 | +- ✅ Success indicators |
| 55 | +- ❌ Error messages with specific line numbers |
| 56 | +- ⚠️ Warnings for non-critical issues |
| 57 | +- 📊 Summary of phases and command counts |
| 58 | + |
| 59 | +### Example Output |
| 60 | + |
| 61 | +``` |
| 62 | +Validating: patterns/pattern-2/buildspec.yml |
| 63 | +====================================================================== |
| 64 | +✅ Valid buildspec file |
| 65 | +
|
| 66 | +Summary: |
| 67 | + Version: 0.2 |
| 68 | + Phases: pre_build, build, post_build |
| 69 | + - pre_build: 7 commands |
| 70 | + - build: 39 commands |
| 71 | + - post_build: 8 commands |
| 72 | +``` |
| 73 | + |
| 74 | +### Example Error Output |
| 75 | + |
| 76 | +``` |
| 77 | +Validating: patterns/pattern-2/buildspec.yml |
| 78 | +====================================================================== |
| 79 | +
|
| 80 | +❌ ERRORS (1): |
| 81 | + - Phase 'post_build', command #5 must be a string, got dict |
| 82 | +
|
| 83 | +❌ Invalid buildspec file |
| 84 | +``` |
| 85 | + |
| 86 | +## Common Issues Detected |
| 87 | + |
| 88 | +### 1. Colons in Command Strings |
| 89 | + |
| 90 | +**Problem**: YAML interprets colons as key-value separators, even in quoted strings in some cases. |
| 91 | + |
| 92 | +```yaml |
| 93 | +# ❌ BAD - May be parsed as a dictionary |
| 94 | +- echo "Note: This is a message" |
| 95 | + |
| 96 | +# ✅ GOOD - Use single quotes around the entire command |
| 97 | +- 'echo "Note: This is a message"' |
| 98 | +``` |
| 99 | +
|
| 100 | +### 2. Missing Required Fields |
| 101 | +
|
| 102 | +The validator checks for: |
| 103 | +- `version` field (must be 0.1 or 0.2) |
| 104 | +- `phases` section (must have at least one phase) |
| 105 | + |
| 106 | +### 3. Invalid Command Types |
| 107 | + |
| 108 | +All commands must be strings: |
| 109 | + |
| 110 | +```yaml |
| 111 | +# ❌ BAD - Command is a dictionary |
| 112 | +phases: |
| 113 | + build: |
| 114 | + commands: |
| 115 | + - echo: "This is wrong" |
| 116 | +
|
| 117 | +# ✅ GOOD - Command is a string |
| 118 | +phases: |
| 119 | + build: |
| 120 | + commands: |
| 121 | + - echo "This is correct" |
| 122 | +``` |
| 123 | + |
| 124 | +## Exit Codes |
| 125 | + |
| 126 | +- `0`: All buildspec files are valid |
| 127 | +- `1`: One or more buildspec files have errors |
| 128 | + |
| 129 | +This makes it suitable for use in CI/CD pipelines: |
| 130 | + |
| 131 | +```yaml |
| 132 | +- name: Validate Buildspec |
| 133 | + run: python3 scripts/validate_buildspec.py patterns/*/buildspec.yml |
| 134 | +``` |
| 135 | + |
| 136 | +## Limitations |
| 137 | + |
| 138 | +This validator checks for: |
| 139 | +- YAML syntax errors |
| 140 | +- Required fields and structure |
| 141 | +- Data type correctness |
| 142 | +- Common mistakes |
| 143 | + |
| 144 | +It does **not** validate: |
| 145 | +- AWS-specific runtime environments |
| 146 | +- Environment variable references |
| 147 | +- S3 artifact paths |
| 148 | +- IAM permissions |
| 149 | + |
| 150 | +For complete validation, test your buildspec in an actual CodeBuild environment. |
| 151 | + |
| 152 | +## Integration with CI/CD |
| 153 | + |
| 154 | +### GitHub Actions |
| 155 | + |
| 156 | +Already integrated in `.github/workflows/developer-tests.yml` via the `make lint` command. |
| 157 | + |
| 158 | +### Local Pre-commit Hook |
| 159 | + |
| 160 | +Add to `.git/hooks/pre-commit`: |
| 161 | + |
| 162 | +```bash |
| 163 | +#!/bin/bash |
| 164 | +python3 scripts/validate_buildspec.py patterns/*/buildspec.yml || exit 1 |
| 165 | +``` |
| 166 | + |
| 167 | +## Troubleshooting |
| 168 | + |
| 169 | +### "ModuleNotFoundError: No module named 'yaml'" |
| 170 | + |
| 171 | +Install PyYAML: |
| 172 | +```bash |
| 173 | +pip install pyyaml |
| 174 | +``` |
| 175 | + |
| 176 | +### "externally-managed-environment" |
| 177 | + |
| 178 | +On macOS with Homebrew Python, use a virtual environment: |
| 179 | +```bash |
| 180 | +python3 -m venv .venv |
| 181 | +source .venv/bin/activate |
| 182 | +pip install pyyaml |
| 183 | +``` |
| 184 | + |
| 185 | +## Contributing |
| 186 | + |
| 187 | +When adding new buildspec files to the repository, ensure they pass validation: |
| 188 | + |
| 189 | +```bash |
| 190 | +make validate-buildspec |
| 191 | +``` |
| 192 | + |
| 193 | +This is automatically checked in CI/CD pipelines. |
0 commit comments