Skip to content

Commit

Permalink
[ci] cache tools
Browse files Browse the repository at this point in the history
  • Loading branch information
veggiemonk committed Oct 7, 2024
1 parent 27d6eb1 commit c420105
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
test:
permissions:
contents: read # for actions/checkout to fetch code
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
# pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
runs-on: ubuntu-latest
steps:
- name: Harden Runner
Expand All @@ -36,4 +36,4 @@ jobs:
cache-dependency-path: ./go.mod

- name: run CI
run: go run cmd/tools/ci/*.go -pr -nodiff
run: go run cmd/tools/ci/ -pr -nodiff
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
test:
permissions:
contents: read # for actions/checkout to fetch code
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
# pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
runs-on: ubuntu-latest
steps:
- name: Harden Runner
Expand Down
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ linters:
enable:
- unused
- errcheck
- exportloopref
- gocritic
- gofumpt
- goimports
Expand Down
57 changes: 46 additions & 11 deletions cmd/tools/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,37 @@ const (
osvScannerRepo = "github.com/google/osv-scanner/cmd/osv-scanner"
osvScannerVersion = "@v1.8.2"
osvScanner = osvScannerRepo + osvScannerVersion
osvScannerBin = "osv-scanner"

// goVuln to find vulnerabilities
vulnRepo = "golang.org/x/vuln/cmd/govulncheck"
vulnVersion = "@latest"
goVuln = vulnRepo + vulnVersion
goVulnBin = "govulncheck"

// goCILint is for linting code
goCILintRepo = "github.com/golangci/golangci-lint/cmd/golangci-lint"
goCILintVersion = "@v1.60.3"
goCILint = goCILintRepo + goCILintVersion
goCILintBin = "golangci-lint"

// goFumpt is mvdan.cc/gofumpt to format code
goFumptRepo = "mvdan.cc/gofumpt"
goFumptVersion = "@v0.6.0"
goFumptVersion = "@v0.7.0"
goFumpt = goFumptRepo + goFumptVersion
goFumptBin = "gofumpt"

dirK8s = "./docs/kubernetes"
dirTerra = "./docs/terraform"
)

var binz = map[string]string{
osvScanner: osvScannerBin,
goVuln: goVulnBin,
goCILint: goCILintBin,
goFumpt: goFumptBin,
}

type Result struct {
Tasks []Task
}
Expand Down Expand Up @@ -84,7 +95,7 @@ func (t Task) String() string {
var errUnrecoverable = errors.New("unrecoverable")

func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: false,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
Expand Down Expand Up @@ -211,19 +222,20 @@ func Main(logger *slog.Logger) error {
run(V, ".", "go", genargs...),
run(V, ".", "go", "test", vargs, "./..."),
run(V, ".", "go", "mod", "tidy"),
run(V, dirK8s, "go", genargs...),
run(V, dirK8s, "go", "mod", "tidy"),
run(V, dirTerra, "go", genargs...),
run(V, dirTerra, "go", "mod", "tidy"),
run(V, ".", "go", "run", goFumpt, "-w", "-extra", "."),
run(V, ".", "go", "run", goCILint, vargs, "run", "./..."),
// FIXME: causing too many issues right now
// run(V, dirK8s, "go", genargs...),
// run(V, dirK8s, "go", "mod", "tidy"),
// run(V, dirTerra, "go", genargs...),
// run(V, dirTerra, "go", "mod", "tidy"),
installRun(V, goFumpt, "-w", "-extra", "."),
installRun(V, goCILint, vargs, "run", "./..."),
))
}

if scan {
p.Steps = append(p.Steps, p.Series(
run(V, ".", "go", "run", goVuln, "./..."),
run(V, ".", "go", "run", osvScanner, "."),
installRun(V, goVuln, "./..."),
installRun(V, osvScanner, "."),
))
}

Expand Down Expand Up @@ -305,11 +317,34 @@ func (g *CLI) String() string {
return fmt.Sprintf("CLI{Dir: %s, Command: '%s'}", g.Dir, g.Bin+" "+strings.Join(g.Args, " "))
}

func installRun(verbose bool, bin string, args ...string) wf.Step[Result] {
cli, ok := binz[bin]
if !ok {
// not a tool we use
return run(verbose, ".", "go", append([]string{"run", bin}, args...)...)
}
if _, err := exec.LookPath(cli); err != nil {
// tool not found => installing
fmt.Println("install tool", bin)
instErr := exec.Command("go", "install", bin).Run()
if instErr != nil {
return run(verbose, ".", "go", append([]string{"run", bin}, args...)...)
}
}
if _, err := exec.LookPath(cli); err != nil {
// tool not in the path
fmt.Println("tool not in the path", bin)
return run(verbose, ".", "go", append([]string{"run", bin}, args...)...)
}
fmt.Println("running local tool", cli)
return run(verbose, ".", cli, args...)
}

func (g *CLI) Run(ctx context.Context, r *Result) (*Result, error) {
if g.Bin == "" {
return r, fmt.Errorf("%T: binary not set: %w", g, errUnrecoverable)
}
cmd := exec.Command(g.Bin, g.Args...)
cmd := exec.CommandContext(ctx, g.Bin, g.Args...)
var buf strings.Builder
if g.ShowOutput {
cmd.Stdout = io.MultiWriter(&buf, os.Stdout)
Expand Down

0 comments on commit c420105

Please sign in to comment.