diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..9ebe89ba --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +node_modules +.cache +.idea +dist +.git +static diff --git a/.github/golang/Dockerfile b/.github/golang/Dockerfile deleted file mode 100644 index 1c4188c1..00000000 --- a/.github/golang/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM golang:1.14 - -COPY entrypoint.sh /entrypoint.sh - -ENTRYPOINT ["/entrypoint.sh"] -CMD [""] diff --git a/.github/golang/entrypoint.sh b/.github/golang/entrypoint.sh deleted file mode 100755 index 78eb9f99..00000000 --- a/.github/golang/entrypoint.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -set -e - -go test -cover ./... diff --git a/.github/goreleaser/Dockerfile b/.github/goreleaser/Dockerfile deleted file mode 100644 index c77d6b52..00000000 --- a/.github/goreleaser/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM golang:1.14-alpine - -RUN apk --no-cache add git nodejs-current nodejs-npm make g++ bash bzr curl docker rpm && \ - npm i -g npm && \ - wget https://github.com/goreleaser/goreleaser/releases/latest/download/goreleaser_Linux_x86_64.tar.gz && \ - tar -xvzf *.tar.gz -C /usr/local/bin && \ - rm *.gz && \ - go get -u github.com/gobuffalo/packr/packr - -COPY entrypoint.sh /entrypoint.sh - -ENTRYPOINT ["/entrypoint.sh"] -CMD [""] diff --git a/.github/goreleaser/entrypoint.sh b/.github/goreleaser/entrypoint.sh deleted file mode 100755 index 9937822d..00000000 --- a/.github/goreleaser/entrypoint.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash - -if [ -n "$DOCKER_USERNAME" ] && [ -n "$DOCKER_PASSWORD" ]; then - echo "Login to the docker..." - docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY -fi - -# Workaround for github actions when access to different repositories is needed. -# Github actions provides a GITHUB_TOKEN secret that can only access the current -# repository and you cannot configure it's value. -# Access to different repositories is needed by brew for example. - -if [ -n "$GORELEASER_GITHUB_TOKEN" ] ; then - export GITHUB_TOKEN=$GORELEASER_GITHUB_TOKEN -fi - -npm ci -goreleaser $@ diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..aca05996 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,45 @@ +on: + push: + tags: + - "v*" +name: Test and Release +jobs: + npm-test: + name: npm test + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: npm test + uses: actions/setup-node@v1 + - name: npm it + run: npm it + go-test: + name: go test + runs-on: ubuntu-latest + steps: + - name: Install Go + uses: actions/setup-go@v1 + with: + go-version: "1.14.x" + - name: Checkout code + uses: actions/checkout@v2 + - name: Test + run: go test -cover ./... + buildx: + needs: [go-test, npm-test] + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Set up Docker Buildx + id: buildx + uses: crazy-max/ghaction-docker-buildx@v1 + with: + version: latest + - name: Available platforms + run: echo ${{ steps.buildx.outputs.platforms }} + - name: Run Buildx + run: docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + - name: Run Buildx + run: make publish diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 7ff228c8..6c28fa3c 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -1,23 +1,25 @@ on: push -name: Build, Test and Release +name: Test jobs: - build: + npm-test: name: npm test runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - name: Checkout code + uses: actions/checkout@v2 - name: npm test uses: actions/setup-node@v1 - - run: npm it - - name: go test - uses: ./.github/golang/ - - name: Release - uses: ./.github/goreleaser/ - env: - DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} - DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REF: ${{ github. ref }} + - name: npm it + run: npm it + go-test: + name: go test + runs-on: ubuntu-latest + steps: + - name: Install Go + uses: actions/setup-go@v1 with: - args: release - if: contains(github.ref, 'tags') + go-version: "1.14.x" + - name: Checkout code + uses: actions/checkout@v2 + - name: Test + run: go test -cover ./... diff --git a/Dockerfile b/Dockerfile index f5d72f50..dd61face 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,56 @@ -FROM alpine:latest as certs -RUN apk --update add ca-certificates +# Build assets +FROM node:13-alpine as node + +RUN apk add --no-cache git openssh python make g++ util-linux + +WORKDIR /build + +# Install dependencies +COPY package*.json ./ +RUN npm install + +# Copy assets to build +COPY assets ./assets + +# Do the build +RUN npm run build + + +FROM golang:alpine AS builder + +RUN apk add --no-cache git ca-certificates +RUN mkdir /dozzle + +WORKDIR /dozzle + +# Needed for assets +RUN go get -u github.com/gobuffalo/packr/packr + +# Copy go mod files +COPY go.* ./ +RUN go mod download + +# Copy assets built with node +COPY --from=node /build/static ./static + +# Copy all other files +COPY . . + +# Compile static files +RUN packr -z + +# Args +ARG TAG=dev + +# Build binary +RUN CGO_ENABLED=0 go build -ldflags "-s -w -X main.version=$TAG" -o dozzle FROM scratch + ENV PATH=/bin ENV DOCKER_API_VERSION 1.38 -COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt -COPY dozzle / + +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=builder /dozzle/dozzle /dozzle + ENTRYPOINT ["/dozzle"] diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..aa7ae373 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +TAG := $(shell git describe --tags) +PLATFROMS := linux/amd64,linux/arm64,linux/arm/v7 + +.PHONY: publish +publish: + docker buildx build --build-arg TAG=$(TAG) --platform $(PLATFROMS) -t amir20/test:latest -t amir20/test:$(TAG) --push . diff --git a/main.go b/main.go index a0f11c09..bc3de4f9 100644 --- a/main.go +++ b/main.go @@ -138,7 +138,10 @@ func (h *handler) index(w http.ResponseWriter, req *http.Request) { if h.box.Has(req.URL.Path) && req.URL.Path != "" && req.URL.Path != "/" { fileServer.ServeHTTP(w, req) } else { - text, _ := h.box.FindString("index.html") + text, err := h.box.FindString("index.html") + if err != nil { + panic(err) + } text = strings.Replace(text, "__BASE__", "{{ .Base }}", -1) tmpl, err := template.New("index.html").Parse(text) if err != nil {