diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..a153073 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,60 @@ +name: Docker + +on: + push: + branches: + - 'main' + tags: + - 'v*' + pull_request: + branches: + - 'main' + paths: + - 'Dockerfile' + - '.dockerignore' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v4 + with: + images: | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - name: Login to Container Registry + if: github.event_name != 'pull_request' + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..980fdd0 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,25 @@ +name: go + +on: + push: + tags: + - v* + branches: + - master + - main + pull_request: + +permissions: + contents: read + pull-requests: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: 1.21 + - name: Test + run: go test -v ./... diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index 9b3f8d5..e5755b4 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -3,7 +3,7 @@ name: goreleaser on: push: tags: - - '*' + - 'v*' permissions: contents: write diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..1287689 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,33 @@ +name: lint + +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +jobs: + + golangci-lint: + name: golangci-lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.21' + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.53 + + conventional-commits: + name: conventional-commits + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: webiny/action-conventional-commits@v1.1.0 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..074c75f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.21-alpine AS build +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download -x +COPY . ./ +RUN CGO_ENABLED=0 go build -trimpath -o /bin/ksei-exporter ./exporter +RUN CGO_ENABLED=0 go build -trimpath -o /bin/wei ./wei + +FROM alpine:edge +RUN apk add -U curl ca-certificates && update-ca-certificates +COPY --from=build /bin/ksei-exporter /bin/ksei-exporter +COPY --from=build /bin/wei /bin/wei +CMD [ "/bin/ksei-exporter" ] diff --git a/exporter/main.go b/exporter/main.go index c5ce6b9..420b173 100644 --- a/exporter/main.go +++ b/exporter/main.go @@ -16,14 +16,19 @@ import ( ) type Config struct { - Bind string `envconfig:"bind" default:"0.0.0.0:8080"` - Router struct { + BindHost string `envconfig:"bind_host" default:"0.0.0.0"` + BindPort int `envconfig:"bind_port" default:"8080"` + Router struct { URL string `envconfig:"url"` Username string `envconfig:"username"` Password string `envconfig:"password"` } `envconfig:"router"` } +func (c *Config) Bind() string { + return fmt.Sprintf("%s:%d", c.BindHost, c.BindPort) +} + func main() { _ = godotenv.Overload(".env") @@ -53,12 +58,12 @@ func main() { }))) log.Info(). - Str("bind", cfg.Bind). + Str("bind", cfg.Bind()). Str("url", cfg.Router.URL). Str("user", cfg.Router.Username). Msg("starting metrics exporter server") - if err := e.Start(cfg.Bind); err != nil { + if err := e.Start(cfg.Bind()); err != nil { fmt.Println() fmt.Println(err) os.Exit(1)