Skip to content

Commit

Permalink
Update cli path
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolajgs committed Jul 16, 2024
1 parent 39833c9 commit 5187660
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,29 +16,31 @@ import (
)

func main() {
cli := gocli.NewCLI("github-actions-runners-exporter", "GitHub Actions' runners exporter for Prometheus", "Mikolaj Gasior <mg@forthcoming.systems>")
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",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const VERSION = "0.1.0"
const VERSION = "0.1.2"

0 comments on commit 5187660

Please sign in to comment.