From 8736198b8ea9f94c63b396bd962be705bfb6d0a5 Mon Sep 17 00:00:00 2001 From: Pierre Zemb Date: Sat, 19 Jun 2021 11:28:56 +0200 Subject: [PATCH] chore: CI, multi-arch docker buildx (Fix #44) --- .github/workflows/build.yml | 34 ++++++++++++++++++++++++++++ .github/workflows/docker-push.yml | 37 +++++++++++++++++++++++++++++++ Dockerfile | 16 +++++++++++-- Dockerfile.arm5 | 20 ----------------- Dockerfile.arm6 | 20 ----------------- Dockerfile.arm64 | 20 ----------------- Dockerfile.arm7 | 20 ----------------- README.md | 25 ++++++++++++++------- go.mod | 3 +++ 9 files changed, 105 insertions(+), 90 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/docker-push.yml delete mode 100644 Dockerfile.arm5 delete mode 100644 Dockerfile.arm6 delete mode 100644 Dockerfile.arm64 delete mode 100644 Dockerfile.arm7 create mode 100644 go.mod diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..61cb8fb --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,34 @@ +name: build + +on: + pull_request: + branches: [ master ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... + + - name: build docker image on multiple platform + id: docker_build + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + platforms: linux/amd64,linux/arm64,linux/arm/v5,linux/arm/v6,linux/arm/v7,darwin/amd64,darwin/arm64,windows/amd64 + push: false diff --git a/.github/workflows/docker-push.yml b/.github/workflows/docker-push.yml new file mode 100644 index 0000000..e406e41 --- /dev/null +++ b/.github/workflows/docker-push.yml @@ -0,0 +1,37 @@ +name: CI to Docker Hub + +on: + push: + branches: [ master ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + + - name: Check Out Repo + uses: actions/checkout@v2 + + - name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + context: ./ + file: ./Dockerfile + platforms: linux/amd64,linux/arm64,linux/arm/v5,linux/arm/v6,linux/arm/v7,darwin/amd64,darwin/arm64,windows/amd64 + push: true + tags: ${{ secrets.DOCKER_HUB_USERNAME }}/gostatic:latest + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index cb60690..b3f6b9d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,22 @@ # stage 0 -FROM golang:latest as builder +FROM --platform=$BUILDPLATFORM golang:latest as builder + +ARG TARGETPLATFORM + WORKDIR /go/src/github.com/PierreZ/goStatic COPY . . + RUN mkdir ./bin && \ apt-get update && apt-get install -y upx && \ - CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags="-s" -tags netgo -installsuffix netgo -o ./bin/goStatic && \ + + # getting right vars from docker buildx + # especially to handle linux/arm/v6 for example + GOOS=$(echo $TARGETPLATFORM | cut -f1 -d/) && \ + GOARCH=$(echo $TARGETPLATFORM | cut -f2 -d/) && \ + GOARM=$(echo $TARGETPLATFORM | cut -f3 -d/ | sed "s/v//" ) && \ + + CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build ${BUILD_ARGS} -ldflags="-s" -tags netgo -installsuffix netgo -o ./bin/goStatic && \ + mkdir ./bin/etc && \ ID=$(shuf -i 100-9999 -n 1) && \ upx -9 ./bin/goStatic && \ diff --git a/Dockerfile.arm5 b/Dockerfile.arm5 deleted file mode 100644 index 96b24ab..0000000 --- a/Dockerfile.arm5 +++ /dev/null @@ -1,20 +0,0 @@ -# stage 0 -FROM golang:latest as builder -WORKDIR /go/src/github.com/PierreZ/goStatic -COPY . . -RUN mkdir ./bin && \ - apt-get update && apt-get install -y upx && \ - CGO_ENABLED=0 GOARCH=arm GOARM=5 GOOS=linux go build -ldflags="-s" -tags netgo -installsuffix netgo -o ./bin/goStatic && \ - mkdir ./bin/etc && \ - ID=$(shuf -i 100-9999 -n 1) && \ - upx -9 ./bin/goStatic && \ - echo $ID && \ - echo "appuser:x:$ID:$ID::/sbin/nologin:/bin/false" > ./bin/etc/passwd && \ - echo "appgroup:x:$ID:appuser" > ./bin/etc/group - -# stage 1 -FROM scratch -WORKDIR / -COPY --from=builder /go/src/github.com/PierreZ/goStatic/bin/ . -USER appuser -ENTRYPOINT ["/goStatic"] diff --git a/Dockerfile.arm6 b/Dockerfile.arm6 deleted file mode 100644 index 3e8fcfb..0000000 --- a/Dockerfile.arm6 +++ /dev/null @@ -1,20 +0,0 @@ -# stage 0 -FROM golang:latest as builder -WORKDIR /go/src/github.com/PierreZ/goStatic -COPY . . -RUN mkdir ./bin && \ - apt-get update && apt-get install -y upx && \ - CGO_ENABLED=0 GOARCH=arm GOARM=6 GOOS=linux go build -ldflags="-s" -tags netgo -installsuffix netgo -o ./bin/goStatic && \ - mkdir ./bin/etc && \ - ID=$(shuf -i 100-9999 -n 1) && \ - upx -9 ./bin/goStatic && \ - echo $ID && \ - echo "appuser:x:$ID:$ID::/sbin/nologin:/bin/false" > ./bin/etc/passwd && \ - echo "appgroup:x:$ID:appuser" > ./bin/etc/group - -# stage 1 -FROM scratch -WORKDIR / -COPY --from=builder /go/src/github.com/PierreZ/goStatic/bin/ . -USER appuser -ENTRYPOINT ["/goStatic"] diff --git a/Dockerfile.arm64 b/Dockerfile.arm64 deleted file mode 100644 index 20d91b9..0000000 --- a/Dockerfile.arm64 +++ /dev/null @@ -1,20 +0,0 @@ -# stage 0 -FROM golang:latest as builder -WORKDIR /go/src/github.com/PierreZ/goStatic -COPY . . -RUN mkdir ./bin && \ - apt-get update && apt-get install -y upx && \ - CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -ldflags="-s" -tags netgo -installsuffix netgo -o ./bin/goStatic && \ - mkdir ./bin/etc && \ - ID=$(shuf -i 100-9999 -n 1) && \ - upx -9 ./bin/goStatic && \ - echo $ID && \ - echo "appuser:x:$ID:$ID::/sbin/nologin:/bin/false" > ./bin/etc/passwd && \ - echo "appgroup:x:$ID:appuser" > ./bin/etc/group - -# stage 1 -FROM scratch -WORKDIR / -COPY --from=builder /go/src/github.com/PierreZ/goStatic/bin/ . -USER appuser -ENTRYPOINT ["/goStatic"] diff --git a/Dockerfile.arm7 b/Dockerfile.arm7 deleted file mode 100644 index 14506ea..0000000 --- a/Dockerfile.arm7 +++ /dev/null @@ -1,20 +0,0 @@ -# stage 0 -FROM golang:latest as builder -WORKDIR /go/src/github.com/PierreZ/goStatic -COPY . . -RUN mkdir ./bin && \ - apt-get update && apt-get install -y upx && \ - CGO_ENABLED=0 GOARCH=arm GOARM=7 GOOS=linux go build -ldflags="-s" -tags netgo -installsuffix netgo -o ./bin/goStatic && \ - mkdir ./bin/etc && \ - ID=$(shuf -i 100-9999 -n 1) && \ - upx -9 ./bin/goStatic && \ - echo $ID && \ - echo "appuser:x:$ID:$ID::/sbin/nologin:/bin/false" > ./bin/etc/passwd && \ - echo "appgroup:x:$ID:appuser" > ./bin/etc/group - -# stage 1 -FROM scratch -WORKDIR / -COPY --from=builder /go/src/github.com/PierreZ/goStatic/bin/ . -USER appuser -ENTRYPOINT ["/goStatic"] diff --git a/README.md b/README.md index 5c5ed8b..be5e8cb 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # goStatic [![Docker Pulls](https://img.shields.io/docker/pulls/pierrezemb/gostatic.svg?style=plastic)](https://hub.docker.com/r/pierrezemb/gostatic/) [![Docker Build](https://img.shields.io/docker/build/pierrezemb/gostatic.svg?style=plastic)](https://hub.docker.com/r/pierrezemb/gostatic/) [![Build Status](https://travis-ci.org/PierreZ/goStatic.svg?branch=master)](https://travis-ci.org/PierreZ/goStatic) [![GoDoc](https://godoc.org/github.com/PierreZ/goStatic?status.svg)](https://godoc.org/github.com/PierreZ/goStatic) -A really small static web server for Docker +A really small, multi-arch, static web server for Docker -### The goal +## The goal My goal is to create to smallest docker container for my web static files. The advantage of Go is that you can generate a fully static binary, so that you don't need anything else. ### Wait, I've been using old versions of GoStatic and things have changed! Yeah, decided to drop support of unsecured HTTPS. Two-years ago, when I started GoStatic, there was no automatic HTTPS available. Nowadays, thanks to Let's Encrypt, it's really easy to do so. If you need HTTPS, I recommend [caddy](https://caddyserver.com). -### Features - * A fully static web server in 6MB +## Features + * A fully static web server embedded in a `SCRATCH` image * No framework * Web server built for Docker * Light container @@ -17,7 +17,7 @@ Yeah, decided to drop support of unsecured HTTPS. Two-years ago, when I started * Log enabled * Specify custom response headers per path and filetype [(info)](./docs/header-config.md) -### Why? +## Why? Because the official Golang image is wayyyy too big (around 1/2Gb as you can see below) and could be insecure. [![](https://badge.imagelayers.io/golang:latest.svg)](https://imagelayers.io/?images=golang:latest 'Get your own badge on imagelayers.io') @@ -30,12 +30,12 @@ Many links should provide you with additional info to see my point of view: * [Building Docker Images for Static Go Binaries](https://medium.com/@kelseyhightower/optimizing-docker-images-for-static-binaries-b5696e26eb07) * [Small Docker Images For Go Apps](https://www.ctl.io/developers/blog/post/small-docker-images-for-go-apps) -### How to use +## How to use ``` docker run -d -p 80:8043 -v path/to/website:/srv/http --name goStatic pierrezemb/gostatic ``` -### Usage +## Usage ``` ./goStatic --help @@ -68,7 +68,7 @@ Usage of ./goStatic: Define the basic auth. Form must be user:password ``` -#### Fallback +### Fallback The fallback option is principally useful for single-page applications (SPAs) where the browser may request a file, but where part of the path is in fact an internal route in the application, not a file on disk. goStatic supports two possible usages of this option: @@ -76,3 +76,12 @@ The fallback option is principally useful for single-page applications (SPAs) wh 2. Using a relative file, which searches up the tree for the specified file The second case is useful if you have multiple SPAs within the one filesystem. e.g., */* and */admin*. + + +## Build + +### Docker images +```bash +docker buildx create --use --name=cross +docker buildx build --platform=linux/amd64,linux/arm64,linux/arm/v5,linux/arm/v6,linux/arm/v7,darwin/amd64,darwin/arm64,windows/amd64 . +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0d42a78 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/PierreZ/goStatic + +go 1.16