Skip to content

Commit d2dd912

Browse files
author
Joris Conijn
authored
docs: describe native codebuild example (#29)
* docs: describe native codebuild example * docs: add CDK example Describe how you could leverage this package using a native CodeBuild `buildspec.yml` file and how to use it in a CDK Pipeline. Issue: #9
1 parent 7983049 commit d2dd912

File tree

1 file changed

+114
-7
lines changed

1 file changed

+114
-7
lines changed

README.md

Lines changed: 114 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ The following syntax can be used to convert a report:
2626
report2junit <SOURCE_LOCATION>
2727
```
2828

29-
### Examples
29+
### CLI Examples
3030

31-
Convert an output report from [cloudformation-guard](https://github.com/aws-cloudformation/cloudformation-guard) using
32-
the following command(s):
31+
Convert an output report from [cloudformation-guard][cloudformation-guard] using the following command(s):
3332

3433
```bash
3534
report2junit ./sample-reports/cfn-guard.json
@@ -38,8 +37,7 @@ report2junit ./sample-reports/cfn-guard.json
3837
report2junit ./sample-reports/cfn-guard.json --destination-file ./sample-reports/cfn-guard-other-destination.xml
3938
```
4039

41-
Convert an output report from [cfn-nag](https://github.com/stelligent/cfn_nag) using
42-
the following command(s):
40+
Convert an output report from [cfn-nag][cfn-nag] using the following command(s):
4341

4442
```bash
4543
report2junit ./sample-reports/cfn-nag.json
@@ -48,8 +46,7 @@ report2junit ./sample-reports/cfn-nag.json
4846
report2junit ./sample-reports/cfn-nag.json --destination-file ./sample-reports/cfn-nag-other-destination.xml
4947
```
5048

51-
Combine both the [cloudformation-guard](https://github.com/aws-cloudformation/cloudformation-guard) and [cfn-nag](https://github.com/stelligent/cfn_nag)
52-
reports into a single output report.
49+
Combine both the [cloudformation-guard][cloudformation-guard] and [cfn-nag][cfn-nag] reports into a single output report.
5350

5451
```bash
5552
report2junit ./sample-reports/cfn-nag.json ./sample-reports/cfn-guard.json
@@ -71,3 +68,113 @@ echo $?
7168
report2junit ./sample-reports/cfn-guard.json --ignore-failures
7269
echo $?
7370
```
71+
72+
### AWS CodeBuild Examples
73+
74+
One of the reasons for writing this tool to use it in combination with AWS CodeBuild. In this section you will find a
75+
few examples in how you could use it.
76+
77+
#### Native buildspec.yml
78+
79+
After you synthesized your template, or you use a CloudFormation native template. You can scan it using [cloudformation-guard][cloudformation-guard]
80+
or [cfn-nag][cfn-nag] to scan the template. The outcome of those tools are not compatible with the reporting tools from
81+
AWS CodeBuild. So we will use [report2junit][report2junit] to convert the 2 results into a single, combined compatible
82+
report.
83+
84+
```yaml
85+
version: 0.2
86+
87+
phases:
88+
install:
89+
runtime-versions:
90+
python: 3.8
91+
commands:
92+
- pip install -Ur requirements.txt
93+
- mkdir -p reports
94+
build:
95+
commands:
96+
# Generate the template or use the already existing template.
97+
- cdk synth > template.yml
98+
# Use cfn_nag and cfn-guard to scan the generated template
99+
- cfn_nag_scan --fail-on-warnings --input-path template.yml -o json > reports/cfn-nag.json || true
100+
- cfn-guard validate --rules cfn-rules.guard --data template.yml --output-format json --show-summary none > reports/cfn-guard.json || true
101+
post_build:
102+
commands:
103+
- report2junit reports/cfn-guard.json reports/cfn-nag.json --destination-file ./reports/combined-junit-report.xml
104+
105+
artifacts:
106+
files: '**/*'
107+
108+
reports:
109+
Conpliance:
110+
base-directory: ./reports
111+
file-format: JUNITXML
112+
files:
113+
- combined-junit-report.xml
114+
```
115+
116+
#### Using CDK CodePipeline
117+
118+
When you want to use the pipelines functionality from CDK you can use the following sample to implement [report2junit][report2junit]
119+
into that pipeline.
120+
121+
```python
122+
from aws_cdk import (
123+
core as cdk,
124+
aws_codebuild as codebuild,
125+
aws_codecommit as codecommit,
126+
pipelines as pipelines,
127+
)
128+
from pipeline_stage import PipelineStage
129+
130+
default_synth_spec = {
131+
"version": "0.2",
132+
"reports": {
133+
"Conpliance": {
134+
"base-directory": "./reports",
135+
"file-format": "JUNITXML",
136+
"files": [
137+
"combined-junit-report.xml",
138+
],
139+
},
140+
},
141+
}
142+
143+
144+
class PipelineStack(cdk.Stack):
145+
146+
def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None:
147+
super().__init__(scope, id, **kwargs)
148+
149+
repository = codecommit.Repository(self, "MyRepo", repository_name="MyRepo")
150+
151+
pipelines.CodePipeline(
152+
self,
153+
"Pipeline",
154+
self_mutation=True,
155+
synth_code_build_defaults=pipelines.CodeBuildOptions(
156+
partial_build_spec=codebuild.BuildSpec.from_object(default_synth_spec),
157+
),
158+
synth=pipelines.ShellStep(
159+
"Build",
160+
input=pipelines.CodePipelineSource.code_commit(repository, "main"),
161+
install_commands=[
162+
"pip install -r requirements.txt",
163+
"npm install -g aws-cdk",
164+
"curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/aws-cloudformation/cloudformation-guard/main/install-guard.sh | sh && ",
165+
"mkdir -p /codebuild/user/bin/",
166+
"ln -s ~/.guard/bin/cfn-guard /codebuild/user/bin/cfn-guard"
167+
],
168+
commands=[
169+
"mkdir reports",
170+
"cdk synth > template.yml",
171+
"cfn-guard validate --rules cfn-rules.guard --data template.yml --output-format json --show-summary none > reports/cfn-guard.json || true",
172+
"report2junit reports/cfn-guard.json",
173+
],
174+
),
175+
)
176+
```
177+
178+
[cloudformation-guard]: https://github.com/aws-cloudformation/cloudformation-guard "AWS CloudFormation Guard"
179+
[cfn-nag]: https://github.com/stelligent/cfn_nag "Stelligen cfn_nag"
180+
[report2junit]: https://github.com/Nr18/report2junit "Report2JUnit"

0 commit comments

Comments
 (0)