Skip to content

Commit

Permalink
Use GitHub Actions for releases and Docker image (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon authored Oct 5, 2021
1 parent 9c6e59c commit 916576b
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 84 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CI
on: push
jobs:
test:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.x
- run: make lint
- run: make test

release:
runs-on: ubuntu-latest
if: contains(github.ref, 'refs/tags/')
needs: test
outputs:
upload_url: ${{ steps.release.outputs.upload_url }}
steps:
- name: Create GitHub release
uses: actions/create-release@v1
id: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
body: TODO
draft: true

assets:
strategy:
matrix:
platform: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.platform }}
needs: release
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.x
- run: echo "APP_VERSION=$(git describe --tags --abbrev=0 | sed -e 's/^v//')" >> $GITHUB_ENV
shell: bash
- run: echo "ASSET_NAME=fastly-exporter-${{ env.APP_VERSION }}.$(go env GOOS)-$(go env GOARCH).tar.gz" >> $GITHUB_ENV
shell: bash
- run: echo "ASSET_PATH=dist/v${{ env.APP_VERSION }}/${{ env.ASSET_NAME }}" >> $GITHUB_ENV
shell: bash
- run: make dist
- uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ${{ env.ASSET_PATH }}
asset_name: ${{ env.ASSET_NAME }}
asset_content_type: application/gzip

docker:
runs-on: ubuntu-latest
needs: release
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/metadata-action@v3
id: meta
with:
images: ghcr.io/peterbourgon/fastly-exporter
- uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
30 changes: 0 additions & 30 deletions .github/workflows/test.yml

This file was deleted.

15 changes: 1 addition & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
/dist/
/fastly-exporter

# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.tar.gz
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:latest AS builder
RUN groupadd -r fastly-exporter
RUN useradd -r -g fastly-exporter fastly-exporter
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
ADD .git .git
ADD cmd cmd
ADD pkg pkg
RUN env CGO_ENABLED=0 go build \
-a \
-ldflags="-X main.programVersion=$(git describe --tags --abbrev=0 | sed -e 's/^v//')" \
-o /fastly-exporter \
./cmd/fastly-exporter

FROM scratch
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /fastly-exporter /fastly-exporter
USER fastly-exporter
EXPOSE 8080
ENTRYPOINT ["/fastly-exporter", "-listen=0.0.0.0:8080"]
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
GO ?= go
GOOS ?= $(shell ${GO} env GOOS)
GOARCH ?= $(shell ${GO} env GOARCH)
VERSION ?= $(shell git describe --tags --abbrev=0 | sed -e 's/^v//')
STATICCHECK ?= $(shell $(GO) env GOPATH)/bin/staticcheck
REVIVE ?= $(shell $(GO) env GOPATH)/bin/revive
GOFUMPT ?= $(shell $(GO) env GOPATH)/bin/gofumpt
DOCKER ?= docker
BINARY = fastly-exporter
BINPKG = ./cmd/fastly-exporter
SOURCE = $(shell find . -name *.go)
DIST_DIR = dist/v${VERSION}
DIST_BIN_FILE = ${BINARY}-${VERSION}.${GOOS}-${GOARCH}
DIST_ZIP_FILE = ${DIST_BIN_FILE}.tar.gz
DIST_BIN = ${DIST_DIR}/${DIST_BIN_FILE}
DIST_ZIP = ${DIST_DIR}/${DIST_ZIP_FILE}
DOCKER_TAG = fastly-exporter:${VERSION}
DOCKER_ZIP = ${DIST_DIR}/${BINARY}-${VERSION}.docker.tar.gz

${BINARY}: ${SOURCE} Makefile
env CGO_ENABLED=0 ${GO} build -o ${BINARY} -ldflags="-X main.programVersion=${VERSION}" ${BINPKG}

${DIST_BIN}: ${DIST_DIR} ${SOURCE} Makefile
env CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} ${GO} build -o $@ -ldflags="-X main.programVersion=${VERSION}" ${BINPKG}

${DIST_DIR}:
mkdir -p $@

${DIST_ZIP}: ${DIST_BIN}
tar -C ${DIST_DIR} -c -z -f ${DIST_ZIP} ${DIST_BIN_FILE}

${DOCKER_ZIP}: ${SOURCE} Dockerfile
${DOCKER} build --tag=${DOCKER_TAG} .
${DOCKER} save --output=$@ ${DOCKER_TAG}

${STATICCHECK}:
${GO} install honnef.co/go/tools/cmd/staticcheck@latest

${REVIVE}:
${GO} install github.com/mgechev/revive@latest

${GOFUMPT}:
${GO} install mvdan.cc/gofumpt@latest

.PHONY: lint
lint: ${STATICCHECK} ${REVIVE} ${GOFUMPT} ${SOURCE}
${GO} vet ./...
${STATICCHECK} ./...
${REVIVE} ./...
${GOFUMPT} -s -l -d -e .

.PHONY: test
test: ${SOURCE}
${GO} test -race ./...

.PHONY: dist
dist: ${DIST_ZIP}

.PHONY: docker
docker: ${DOCKER_ZIP}

.PHONY: release
release: dist docker
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ Go to the [releases page][releases].

### Docker

Avaliable as [mrnetops/fastly-exporter][container] from [Docker Hub][hub].
Available on the [packages page][pkg] as [peterbourgon/fastly-exporter][img].

[container]: https://hub.docker.com/r/mrnetops/fastly-exporter
[hub]: https://hub.docker.com
[pkg]: https://github.com/peterbourgon/fastly-exporter/packages
[img]: https://github.com/peterbourgon/fastly-exporter/pkgs/container/fastly-exporter

```sh
docker pull ghcr.io/peterbourgon/fastly-exporter:latest
```
docker pull mrnetops/fastly-exporter
```

Note that version `latest` will track RCs, alphas, etc. -- always use an
explicit version in production.

### Source

If you have a working Go installation, you can clone the repo and install the
binary from any revision, including HEAD.

```
```sh
git clone git@github.com:peterbourgon/fastly-exporter
cd fastly-exporter
go build ./cmd/fastly-exporter
Expand All @@ -49,7 +52,7 @@ for information on creating API tokens. The token can be provided via the

[token]: https://docs.fastly.com/guides/account-management-and-security/using-api-tokens#creating-api-tokens

```
```sh
fastly-exporter -token XXX
```

Expand All @@ -74,7 +77,7 @@ services among different fastly-exporter instances by using the `-service-shard`
flag. For example, to shard all services between 3 exporters, you would start
each exporter as

```
```sh
fastly-exporter [common flags] -service-shard 1/3
fastly-exporter [common flags] -service-shard 2/3
fastly-exporter [common flags] -service-shard 3/3
Expand Down Expand Up @@ -102,15 +105,15 @@ export metrics whose names ended in bytes_total, but didn't include imgopto.
### Service discovery

Per-service metrics are available via `/metrics?target=<service ID>`. Available
services are enumerated as targets via the `/sd` endpoint, which is compatible
services are enumerated as targets on the `/sd` endpoint, which is compatible
with the [generic HTTP service discovery][httpsd] feature of Prometheus. An
example Prometheus scrape config for the Fastly exporter follows.

[httpsq]: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#http_sd_config

```yaml
scrape_configs:
- job_name: fastly
- job_name: fastly-exporter
http_sd_configs:
- url: http://127.0.0.1:8080/sd
relabel_configs:
Expand Down
2 changes: 1 addition & 1 deletion pkg/prom/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
//
// Writers (i.e. rt.Subscribers) should call MetricsFor with their specific
// service ID, and update the returned set of Prometheus metrics. Readers (i.e.
// Prometheus) can scrape metrics for all services via the `/metrics` endpoint,
// Prometheus) can scrape metrics for all services via the `/metrics` endpoint.
// or a single service via `/metrics?target=<service ID>`.
//
// https://prometheus.io/docs/prometheus/latest/configuration/configuration/#http_sd_config
Expand Down
29 changes: 0 additions & 29 deletions release.fish

This file was deleted.

0 comments on commit 916576b

Please sign in to comment.