Skip to content

Commit 4338b22

Browse files
authored
Merge pull request #95 from replicatedhq/github-actions
GitHub actions
2 parents ca9106e + 1757af5 commit 4338b22

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

.github/workflows/preflight.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: "Run preflights on a cluster"
2+
on: [push]
3+
4+
jobs:
5+
compile-preflight:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/setup-go@v1
9+
with:
10+
go-version: '1.12.14'
11+
- name: setup env
12+
run: |
13+
echo "::set-env name=GOPATH::$(go env GOPATH)"
14+
echo "::add-path::$(go env GOPATH)/bin"
15+
shell: bash
16+
- uses: actions/checkout@master
17+
- run: make preflight
18+
- uses: actions/upload-artifact@v1
19+
with:
20+
name: preflight
21+
path: bin/preflight
22+
23+
run-preflights:
24+
runs-on: ubuntu-latest
25+
needs: compile-preflight
26+
steps:
27+
- name: Download preflight binary
28+
uses: actions/download-artifact@v1
29+
with:
30+
name: preflight
31+
path: bin/
32+
- uses: engineerd/[email protected]
33+
- run: chmod +x bin/preflight
34+
- run: bin/preflight --interactive=false --format=json https://preflight.replicated.com
35+

cmd/preflight/cli/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func runPreflights(v *viper.Viper, arg string) error {
126126
return showInteractiveResults(preflight.Name, analyzeResults)
127127
}
128128

129-
return showStdoutResults(preflight.Name, analyzeResults)
129+
return showStdoutResults(v.GetString("format"), preflight.Name, analyzeResults)
130130
}
131131

132132
func runCollectors(v *viper.Viper, preflight troubleshootv1beta1.Preflight) (map[string][]byte, error) {

cmd/preflight/cli/stdout_results.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package cli
22

33
import (
4+
"encoding/json"
45
"fmt"
56

7+
"github.com/pkg/errors"
68
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
79
)
810

9-
func showStdoutResults(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
10-
fmt.Printf("\n=== TEST %s\n", preflightName)
11-
for _, analyzeResult := range analyzeResults {
12-
fmt.Printf("=== RUN: %s\n", analyzeResult.Title)
11+
func showStdoutResults(format string, preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
12+
if format == "human" {
13+
return showStdoutResultsHuman(preflightName, analyzeResults)
14+
} else if format == "json" {
15+
return showStdoutResultsJSON(preflightName, analyzeResults)
1316
}
17+
18+
return errors.Errorf("unknown output format: %q", format)
19+
}
20+
21+
func showStdoutResultsHuman(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
1422
var failed bool
1523
for _, analyzeResult := range analyzeResults {
1624
testResultfailed := outputResult(analyzeResult)
@@ -28,6 +36,50 @@ func showStdoutResults(preflightName string, analyzeResults []*analyzerunner.Ana
2836
return nil
2937
}
3038

39+
func showStdoutResultsJSON(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
40+
type ResultOutput struct {
41+
Title string `json:"title"`
42+
Message string `json:"message"`
43+
URI string `json:"uri,omitempty"`
44+
}
45+
type Output struct {
46+
Pass []ResultOutput `json:"pass,omitempty"`
47+
Warn []ResultOutput `json:"warn,omitempty"`
48+
Fail []ResultOutput `json:"fail,omitempty"`
49+
}
50+
51+
output := Output{
52+
Pass: []ResultOutput{},
53+
Warn: []ResultOutput{},
54+
Fail: []ResultOutput{},
55+
}
56+
57+
for _, analyzeResult := range analyzeResults {
58+
resultOutput := ResultOutput{
59+
Title: analyzeResult.Title,
60+
Message: analyzeResult.Message,
61+
URI: analyzeResult.URI,
62+
}
63+
64+
if analyzeResult.IsPass {
65+
output.Pass = append(output.Pass, resultOutput)
66+
} else if analyzeResult.IsWarn {
67+
output.Warn = append(output.Warn, resultOutput)
68+
} else if analyzeResult.IsFail {
69+
output.Fail = append(output.Fail, resultOutput)
70+
}
71+
}
72+
73+
b, err := json.MarshalIndent(output, "", " ")
74+
if err != nil {
75+
return errors.Wrap(err, "failed to marshal results")
76+
}
77+
78+
fmt.Printf("%s\n", b)
79+
80+
return nil
81+
}
82+
3183
func outputResult(analyzeResult *analyzerunner.AnalyzeResult) bool {
3284
if analyzeResult.IsPass {
3385
fmt.Printf(" --- PASS %s\n", analyzeResult.Title)

0 commit comments

Comments
 (0)