From 5187660c66d3c86504893725330a3cfe3f198b68 Mon Sep 17 00:00:00 2001 From: Mikolaj Gasior Date: Tue, 16 Jul 2024 21:13:55 +0200 Subject: [PATCH] Update cli path --- .github/workflows/test-build.yml | 34 ++++++++++++++++++++++++++++++++ Dockerfile | 2 +- go.mod | 2 +- go.sum | 4 ++-- main.go | 20 +++++++++++-------- version.go | 2 +- 6 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/test-build.yml diff --git a/.github/workflows/test-build.yml b/.github/workflows/test-build.yml new file mode 100644 index 0000000..26e5e25 --- /dev/null +++ b/.github/workflows/test-build.yml @@ -0,0 +1,34 @@ +name: Test build + +on: + pull_request: + branches: + - main + +jobs: + main: + name: Build and run + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Call action get-ref-properties + id: get-ref-properties + uses: Cardinal-Cryptography/github-actions/get-ref-properties@v4 + + - name: Build docker image + run: | + docker build --tag garu-${{ steps.get-ref-properties.outputs.sha }} -f Dockerfile . + + - name: Run docker containers + run: | + docker run --rm \ + --name test-garu-${{ steps.get-ref-properties.outputs.sha }} \ + garu-${{ steps.get-ref-properties.outputs.sha }} + + - name: Remove docker container + if: always() + run: | + docker rm -f test-garu-${{ steps.get-ref-properties.outputs.sha }} || true + diff --git a/Dockerfile b/Dockerfile index e4c8c46..222f3df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM golang:alpine AS builder -LABEL maintainer="Mikolaj Gasior" +LABEL maintainer="infra-team@cardinals" RUN apk add --update git bash openssh make gcc musl-dev diff --git a/go.mod b/go.mod index 336f692..44859de 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Cardinal-Cryptography/github-actions-runners-exporter go 1.20 require ( - github.com/MikolajGasior/go-mod-cli v1.0.0 + github.com/mikolajgs/broccli v2.0.0+incompatible github.com/prometheus/client_golang v1.16.0 ) diff --git a/go.sum b/go.sum index f53c2fb..ff7a681 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/MikolajGasior/go-mod-cli v1.0.0 h1:YNP1TnQgKGFw9KF1Zgf+2PLPr445kzjxHPl1rZgIRak= -github.com/MikolajGasior/go-mod-cli v1.0.0/go.mod h1:M4KyBYxD8cWYLuB62FTs/Ng4pkyqcuOjnQlAYWidyic= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -14,6 +12,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/mikolajgs/broccli v2.0.0+incompatible h1:zpLdjMIA9QrUtcXQqjFi3WCY3P8n+/45VHRAc2bfZ4c= +github.com/mikolajgs/broccli v2.0.0+incompatible/go.mod h1:IOqvFr2wSjX/7otXFff5+jOdyWVXi5OsKEJ5viXk/vg= github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= diff --git a/main.go b/main.go index b8258bc..28f6e47 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "encoding/json" "fmt" - gocli "github.com/MikolajGasior/go-mod-cli" + "github.com/mikolajgs/broccli" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -16,29 +16,31 @@ import ( ) func main() { - cli := gocli.NewCLI("github-actions-runners-exporter", "GitHub Actions' runners exporter for Prometheus", "Mikolaj Gasior ") + cli := broccli.NewCLI("github-actions-runners-exporter", "GitHub Actions' runners exporter for Prometheus", "infra-team@cardinals") cmdRun := cli.AddCmd("run", "Runs the daemon, requires GITHUB_TOKEN environment variable", runHandler) - cmdRun.AddFlag("organization", "o", "", "GitHub Organization owner of the runners", gocli.TypeString|gocli.Required, nil) - cmdRun.AddFlag("sleep", "s", "", "Seconds between each request to GitHub API", gocli.TypeInt|gocli.Required, nil) - cmdRun.AddFlag("port", "p", "", "Port to expose /metrics endpoint on", gocli.TypeInt|gocli.Required, nil) + cmdRun.AddFlag("organization", "o", "", "GitHub Organization owner of the runners", broccli.TypeString, broccli.IsRequired) + cmdRun.AddFlag("sleep", "s", "", "Seconds between each request to GitHub API", broccli.TypeInt, broccli.IsRequired) + cmdRun.AddFlag("port", "p", "", "Port to expose /metrics endpoint on", broccli.TypeInt, broccli.IsRequired) _ = cli.AddCmd("version", "Prints version", versionHandler) if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version") { os.Args = []string{"App", "version"} } - os.Exit(cli.Run(os.Stdout, os.Stderr)) + os.Exit(cli.Run()) } -func versionHandler(c *gocli.CLI) int { +func versionHandler(c *broccli.CLI) int { fmt.Fprintf(os.Stdout, VERSION+"\n") return 0 } -func runHandler(cli *gocli.CLI) int { +func runHandler(cli *broccli.CLI) int { if os.Getenv("GITHUB_TOKEN") == "" { fmt.Fprint(os.Stderr, "!!! GITHUB_TOKEN environment variable is missing\n") return 1 } + // TODO: This one doesn't seem to be too useful as GitHub does not clean up runners + // from the list straight away. It's done after 14 days or so. totalCount := promauto.NewGauge(prometheus.GaugeOpts{ Namespace: "github_actions", Subsystem: "runners", @@ -54,6 +56,8 @@ func runHandler(cli *gocli.CLI) int { }) var response Response + // Periodally call GitHub API for number of runners, parse the response into Response struct + // and set prometheus' Gauge value go func() { for { // We are never going to have more than 100 runners so getting page 1 only diff --git a/version.go b/version.go index a44ae0e..d6ae413 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package main -const VERSION = "0.1.0" +const VERSION = "0.1.2"