Skip to content

Commit 0be7d92

Browse files
committed
make buildspec validation part of the lint
1 parent 07d2158 commit 0be7d92

File tree

4 files changed

+461
-2
lines changed

4 files changed

+461
-2
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ package-lock.json
3131

3232
# Type checking
3333
pyrightconfig.temp.json
34-
.pyright/
34+
.pyright/
35+
36+
# Python virtual environments
37+
.venv/
38+
.venv-*/
39+
venv/

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test:
1515
cd idp_cli && python -m pytest -v
1616

1717
# Run both linting and formatting in one command
18-
lint: ruff-lint format check-arn-partitions ui-lint
18+
lint: ruff-lint format check-arn-partitions validate-buildspec ui-lint
1919

2020
# Run linting checks and fix issues automatically
2121
ruff-lint:
@@ -53,6 +53,13 @@ lint-cicd:
5353

5454
@echo -e "$(GREEN)All code quality checks passed!$(NC)"
5555

56+
# Validate AWS CodeBuild buildspec files
57+
validate-buildspec:
58+
@echo "Validating buildspec files..."
59+
@python3 scripts/validate_buildspec.py patterns/*/buildspec.yml || \
60+
(echo -e "$(RED)ERROR: Buildspec validation failed!$(NC)" && exit 1)
61+
@echo -e "$(GREEN)✅ All buildspec files are valid!$(NC)"
62+
5663
# Check CloudFormation templates for hardcoded AWS partition ARNs and service principals
5764
check-arn-partitions:
5865
@echo "Checking CloudFormation templates for hardcoded ARN partitions and service principals..."
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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

Comments
 (0)