-
Notifications
You must be signed in to change notification settings - Fork 0
205 lines (171 loc) · 5.37 KB
/
ci.yml
File metadata and controls
205 lines (171 loc) · 5.37 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
name: CI
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
env:
IMAGE_REPOSITORY: quay.io/rhacs-eng/sensor-metrics
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Verify dependencies
run: go mod verify
- name: Build
run: go build -v ./...
- name: Run go vet
run: go vet ./...
- name: Check formatting
run: |
if [ -n "$(gofmt -l .)" ]; then
echo "Code is not formatted. Run 'gofmt -w .'"
gofmt -d .
exit 1
fi
- name: Run unit tests
run: go test -v -race -coverprofile=coverage.out ./internal/...
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
files: coverage.out
fail_ci_if_error: false
e2e:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Build binary
run: go build -o bin/metrics-analyzer ./cmd/metrics-analyzer
- name: Run E2E tests
run: go test -v ./cmd/...
validate-rules:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Build
run: go build -o bin/metrics-analyzer ./cmd/metrics-analyzer
- name: Validate rules
run: ./bin/metrics-analyzer validate ./automated-rules
container-smoke:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Read version
id: version
run: echo "version=$(cat VERSION)" >> "$GITHUB_OUTPUT"
- name: Set build time
id: build_time
run: echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
- name: Build image
run: |
docker build \
--build-arg VERSION="${{ steps.version.outputs.version }}" \
--build-arg BUILD_TIME="${{ steps.build_time.outputs.build_time }}" \
-t sma:ci .
- name: Start container
run: |
docker run -d --rm --name sma-ci -p 8080:8080 sma:ci
- name: Wait for readiness
run: |
for i in {1..10}; do
if curl -fsS http://localhost:8080/health >/dev/null; then
exit 0
fi
sleep 1
done
docker logs sma-ci
exit 1
- name: Check version endpoint
run: |
for i in {1..5}; do
if curl -fsS http://localhost:8080/version -o /tmp/version.json; then
if [ -s /tmp/version.json ]; then
break
fi
fi
sleep 1
done
if [ ! -s /tmp/version.json ]; then
echo "version response was empty"
cat /tmp/version.json || true
docker logs sma-ci || true
exit 1
fi
python3 - <<'PY'
import json
with open("/tmp/version.json") as f:
data = json.load(f)
assert "version" in data
assert "lastUpdate" in data
print("version ok")
PY
- name: Analyze sample metrics
run: |
curl -fsS -o /tmp/analysis.json -X POST \
-F "file=@testdata/fixtures/sample_metrics.txt" \
http://localhost:8080/api/analyze/both
python3 - <<'PY'
import json
with open("/tmp/analysis.json") as f:
data = json.load(f)
assert "console" in data
assert "markdown" in data
print("analysis ok")
PY
- name: Show container logs on failure
if: failure()
run: docker logs sma-ci
container-image:
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
env:
QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }}
QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }}
steps:
- uses: actions/checkout@v4
- name: Read version
id: version
run: echo "version=$(cat VERSION)" >> "$GITHUB_OUTPUT"
- name: Set build time
id: build_time
run: echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Quay
id: login
if: ${{ env.QUAY_USERNAME != '' && env.QUAY_PASSWORD != '' }}
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ env.QUAY_USERNAME }}
password: ${{ env.QUAY_PASSWORD }}
- name: Build and push image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: ${{ steps.login.conclusion == 'success' }}
tags: |
${{ env.IMAGE_REPOSITORY }}:${{ steps.version.outputs.version }}
${{ env.IMAGE_REPOSITORY }}:${{ github.sha }}
${{ env.IMAGE_REPOSITORY }}:latest
build-args: |
VERSION=${{ steps.version.outputs.version }}
BUILD_TIME=${{ steps.build_time.outputs.build_time }}