From bc61c2dc7ed0aa3a83fedabd61c28b52c70efe62 Mon Sep 17 00:00:00 2001 From: Chris Marshall Date: Sun, 22 Nov 2020 11:35:28 -0500 Subject: [PATCH] Add push-image action, small fixes (#21) * Add push-image action * Update example --- .github/workflows/push-image.yaml | 77 +++++++++++++++++++++++++++++++ Makefile | 2 +- README.md | 2 + example.yaml | 16 +++---- internal/quake/content/router.go | 14 ++++-- 5 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/push-image.yaml diff --git a/.github/workflows/push-image.yaml b/.github/workflows/push-image.yaml new file mode 100644 index 0000000..fd0a1e2 --- /dev/null +++ b/.github/workflows/push-image.yaml @@ -0,0 +1,77 @@ +name: Push Image + +on: + push: + # Sequence of patterns matched against refs/tags + tags: + - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + +jobs: + push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Prepare + id: prep + run: | + DOCKER_IMAGE=${GITHUB_REPOSITORY} + VERSION=noop + if [ "${{ github.event_name }}" = "schedule" ]; then + VERSION=nightly + elif [[ $GITHUB_REF == refs/tags/* ]]; then + VERSION=${GITHUB_REF#refs/tags/} + elif [[ $GITHUB_REF == refs/heads/* ]]; then + VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g') + if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then + VERSION=edge + fi + elif [[ $GITHUB_REF == refs/pull/* ]]; then + VERSION=pr-${{ github.event.number }} + fi + TAGS="${DOCKER_IMAGE}:${VERSION}" + if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then + MINOR=${VERSION%.*} + MAJOR=${MINOR%.*} + TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest" + elif [ "${{ github.event_name }}" = "push" ]; then + TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}" + fi + echo ::set-output name=version::${VERSION} + echo ::set-output name=tags::${TAGS} + echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ') + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.prep.outputs.tags }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache diff --git a/Makefile b/Makefile index d95efbe..1130200 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ q3: gen gen: ## Generate and embed templates @go run tools/genstatic.go public public -VERSION ?= v1.0.5 +VERSION ?= latest IMAGE ?= docker.io/criticalstack/quake:$(VERSION) .PHONY: build diff --git a/README.md b/README.md index ea980fb..222ac24 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![Build Status](https://github.com/criticalstack/quake-kube/workflows/Push%20Image/badge.svg) + # QuakeKube QuakeKube is a Kubernetes-ified version of [QuakeJS](https://github.com/inolen/quakejs) that runs a dedicated [Quake 3](https://en.wikipedia.org/wiki/Quake_III_Arena) server in a Kubernetes Deployment, and then allow clients to connect via QuakeJS in the browser. diff --git a/example.yaml b/example.yaml index 89d6991..1fe6ff6 100644 --- a/example.yaml +++ b/example.yaml @@ -1,16 +1,16 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: quakejs + name: quake spec: selector: matchLabels: - run: quakejs + run: quake replicas: 1 template: metadata: labels: - run: quakejs + run: quake annotations: prometheus.io/scrape: 'true' prometheus.io/port: '8080' @@ -20,9 +20,9 @@ spec: - q3 - server - --config=/config/config.yaml - - --content-server=http://localhost:9090 + - --content-server=http://127.0.0.1:9090 - --agree-eula - image: docker.io/criticalstack/quake:v1.0.5 + image: docker.io/criticalstack/quake:latest name: server ports: - containerPort: 8080 @@ -40,7 +40,7 @@ spec: - q3 - content - --seed-content-url=http://content.quakejs.com - image: docker.io/criticalstack/quake:v1.0.5 + image: docker.io/criticalstack/quake:latest name: content-server ports: - containerPort: 9090 @@ -57,11 +57,11 @@ spec: apiVersion: v1 kind: Service metadata: - name: quakejs + name: quake spec: type: NodePort selector: - run: quakejs + run: quake ports: - port: 8080 targetPort: 8080 diff --git a/internal/quake/content/router.go b/internal/quake/content/router.go index 3ca3dc9..90eb8f8 100644 --- a/internal/quake/content/router.go +++ b/internal/quake/content/router.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "path/filepath" + "regexp" "strings" "github.com/labstack/echo/v4" @@ -55,10 +56,7 @@ func NewRouter(cfg *Config) (*echo.Echo, error) { return c.JSONPretty(http.StatusOK, files, " ") }) e.GET("/assets/*", func(c echo.Context) error { - path := filepath.Join(cfg.AssetsDir, c.Param("*")) - d, f := filepath.Split(path) - f = f[strings.Index(f, "-")+1:] - path = filepath.Join(d, f) + path := filepath.Join(cfg.AssetsDir, trimAssetName(c.Param("*"))) if _, err := os.Stat(path); os.IsNotExist(err) { return c.String(http.StatusNotFound, "file not found") } @@ -131,3 +129,11 @@ func NewRouter(cfg *Config) (*echo.Echo, error) { }) return e, nil } + +var assetPattern = regexp.MustCompile(`(\d+-)`) + +// trimAssetName returns a path string that has been prefixed with a crc32 +// checksum. +func trimAssetName(s string) string { + return assetPattern.ReplaceAllLiteralString(s, "") +}