forked from HO-CTO/sre-monitoring-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (140 loc) · 5.63 KB
/
docker-build-branch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: 'Docker Build Branch'
on:
pull_request:
types: [ labeled, opened, reopened, synchronize ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
REPO_NAME: ${{ github.event.repository.name }}
PROMTOOL_IMAGE_NAME: sre-promtool
PROMTOOL_VERSION: 2.38.0
USER: ho-cto
jobs:
code-quality:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Hadolint Dockerfile
uses: hadolint/[email protected]
with:
dockerfile: ./monitoring-as-code/Dockerfile
- name: Jsonnet format check
run: sh ${{ github.workspace }}/monitoring-as-code/fmt.sh && git diff --exit-code --color
- name: Jsonnet lint check
run: sh ${{ github.workspace }}/monitoring-as-code/lint.sh
container-image-build:
needs: code-quality
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Set up Docker Buildx
uses: docker/[email protected]
- name: Calculate metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ github.event.pull_request.head.sha }}
- name: Build container
uses: docker/[email protected]
with:
context: ./monitoring-as-code
file: ./monitoring-as-code/Dockerfile
push: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
PACKAGE_TOKEN=${{secrets.GITHUB_TOKEN}}
MAC_VERSION='#${{ github.event.number }}'
outputs: type=docker,dest=/tmp/${{ env.REPO_NAME }}.tar
- name: Upload artifact
uses: actions/[email protected]
with:
name: ${{ env.REPO_NAME }}
path: /tmp/${{ env.REPO_NAME }}.tar
code-test:
needs: container-image-build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Download artifact
uses: actions/[email protected]
with:
name: ${{ env.REPO_NAME }}
path: /tmp
- name: Load Docker image
run: |
docker load --input /tmp/${{ env.REPO_NAME }}.tar
echo "IMAGE_TAG=$(docker image ls --format '{{.Repository}}:{{.Tag}}' | grep sre-monitoring-as-code)" >> $GITHUB_ENV
- name: Run monitoring mixin
run: docker run -v ${{ github.workspace }}/monitoring-as-code:${{ github.workspace }}/data -t ${{ env.IMAGE_TAG }} -m monitoring -rd -i ${{ github.workspace }}/data/mixin-defs -o ${{ github.workspace }}/data/output
- name: Run testing mixin
run: docker run -v ${{ github.workspace }}/monitoring-as-code:${{ github.workspace }}/data -t ${{ env.IMAGE_TAG }} -m testing -rd -i ${{ github.workspace }}/data/mixin-defs -o ${{ github.workspace }}/data/output
- name: Run promtool checks
run: for FILE in ${{ github.workspace }}/monitoring-as-code/output/prometheus-rules/*-rules.yaml; do docker run -v ${{ github.workspace }}/monitoring-as-code:${{ github.workspace }}/data ${{env.REGISTRY}}/${{env.USER}}/${{env.PROMTOOL_IMAGE_NAME}}:${{env.PROMTOOL_VERSION}} check rules ${{ github.workspace }}/data/output/prometheus-rules/$(basename $FILE); if [ $? -ne 0 ]; then echo "Validation of rules files failed for $(basename $FILE) - exiting"; exit 1; fi done
container-image-test:
needs: container-image-build
permissions:
security-events: write
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Download artifact
uses: actions/[email protected]
with:
name: ${{ env.REPO_NAME }}
path: /tmp
- name: Load Docker image
run: |
docker load --input /tmp/${{ env.REPO_NAME }}.tar
echo "IMAGE_TAG=$(docker image ls --format '{{.Repository}}:{{.Tag}}' | grep sre-monitoring-as-code)" >> $GITHUB_ENV
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.10.0
- name: Setup Snyk
run: |
npm install snyk -g
snyk auth ${{secrets.SNYK_TOKEN}}
- name: Snyk Container Scan
id: snyk-dockerfile
run: |
snyk container test --file=./monitoring-as-code/Dockerfile --sarif-file-output=snyk.sarif --app-vulns --severity-threshold=medium ${{ env.IMAGE_TAG }}
continue-on-error: true
- name: Upload result to GitHub Code Scanning
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: snyk.sarif
- name: Check on failures
if: steps.snyk-dockerfile.outcome != 'success'
run: exit 1
container-image-push:
needs: [code-test, container-image-test]
permissions:
packages: write
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Download artifact
uses: actions/[email protected]
with:
name: ${{ env.REPO_NAME }}
path: /tmp
- name: Load Docker image
run: |
docker load --input /tmp/${{ env.REPO_NAME }}.tar
echo "IMAGE_TAG=$(docker image ls --format '{{.Repository}}:{{.Tag}}' | grep sre-monitoring-as-code)" >> $GITHUB_ENV
- name: Login to GitHub Container Registry
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push image to GitHub Container Registry
run: docker push ${{ env.IMAGE_TAG }}