From cab5df2ead0d51639f8a9312175fc0b576c78f2b Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 8 Nov 2020 01:16:33 +0100 Subject: [PATCH] Optimize build time --- .dockerignore | 2 +- .github/workflows/build.yml | 24 +++++++--------- .github/workflows/docs.yml | 2 -- .github/workflows/test.yml | 57 +++++++++++++++++++++++++++++++++++++ .gitignore | 2 -- Dockerfile | 34 ++++++++++------------ 6 files changed, 83 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.dockerignore b/.dockerignore index 093e1654..d0e83c33 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,7 @@ /.idea /*.iml -/.dev +.dev /.git /.github /.res diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 00b5048f..b802b5dd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,7 +4,6 @@ on: push: branches: - 'master' - - 'v*' tags: - 'v*' paths-ignore: @@ -15,7 +14,6 @@ on: pull_request: branches: - 'master' - - 'v*' paths-ignore: - '**.md' - '.github/workflows/docs.yml' @@ -55,17 +53,6 @@ jobs: with: version: latest args: release --skip-publish --rm-dist - - - name: Test - run: | - go test -coverprofile=coverage.txt -covermode=atomic -race ./... - - - name: Upload coverage - uses: codecov/codecov-action@v1 - if: success() - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: ./coverage.txt - name: GitHub Release uses: softprops/action-gh-release@v1 @@ -82,7 +69,6 @@ jobs: docker: runs-on: ubuntu-latest - needs: go env: DOCKERHUB_SLUG: crazymax/diun GHCR_SLUG: ghcr.io/crazy-max/diun @@ -109,6 +95,14 @@ jobs: uses: docker/setup-buildx-action@v1 with: buildkitd-flags: "--debug" + - + 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 if: github.event_name != 'pull_request' @@ -140,6 +134,8 @@ jobs: org.opencontainers.image.title=Diun org.opencontainers.image.description=Docker image update notifier org.opencontainers.image.vendor=CrazyMax + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache - name: Check manifest if: github.event_name != 'pull_request' diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 970484df..d03ee09b 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -4,7 +4,6 @@ on: push: branches: - 'master' - - 'v*' tags: - 'v*' paths: @@ -15,7 +14,6 @@ on: pull_request: branches: - 'master' - - 'v*' paths: - '.github/workflows/docs.yml' - 'docs/**' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..379722c9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,57 @@ +name: test + +on: + push: + branches: + - 'master' + tags: + - 'v*' + paths-ignore: + - '**.md' + - '.github/workflows/docs.yml' + - 'docs/**' + - 'mkdocs.yml' + pull_request: + branches: + - 'master' + paths-ignore: + - '**.md' + - '.github/workflows/docs.yml' + - 'docs/**' + - 'mkdocs.yml' + +jobs: + test: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + - + name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.13 + - + name: Cache Go modules + uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - + name: Go mod + run: | + go mod download + - + name: Test + run: | + go test -coverprofile=coverage.txt -covermode=atomic -race ./... + - + name: Upload coverage + uses: codecov/codecov-action@v1 + if: success() + with: + token: ${{ secrets.CODECOV_TOKEN }} + file: ./coverage.txt diff --git a/.gitignore b/.gitignore index b1ff764f..bf0cf150 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,3 @@ .dev /bin /dist - -/site diff --git a/Dockerfile b/Dockerfile index 4d3eed1d..44669383 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,25 @@ -FROM --platform=${BUILDPLATFORM:-linux/amd64} tonistiigi/xx:golang AS xgo -FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.13-alpine AS builder - +ARG GO_VERSION=1.13 ARG VERSION=dev -ENV CGO_ENABLED 0 -ENV GO111MODULE on -ENV GOPROXY https://goproxy.io,direct +FROM --platform=${BUILDPLATFORM:-linux/amd64} tonistiigi/xx:golang AS xgo + +FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:${GO_VERSION}-alpine AS base +RUN apk add --no-cache curl gcc git musl-dev COPY --from=xgo / / +WORKDIR /src -RUN apk --update --no-cache add \ - build-base \ - gcc \ - git \ - && rm -rf /tmp/* /var/cache/apk/* - -WORKDIR /app - -COPY . ./ +FROM base AS gomod +COPY . . RUN go mod download +FROM gomod AS build ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH -RUN go env -RUN go build -ldflags "-w -s -X 'main.version=${VERSION}'" -v -o diun cmd/main.go +ARG VERSION +ENV CGO_ENABLED 0 +ENV GOPROXY https://goproxy.io,direct +RUN go build -ldflags "-w -s -X 'main.version=${VERSION}'" -v -o /opt/diun cmd/main.go FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:latest LABEL maintainer="CrazyMax" @@ -33,8 +29,8 @@ RUN apk --update --no-cache add \ libressl \ && rm -rf /tmp/* /var/cache/apk/* -COPY --from=builder /app/diun /usr/local/bin/diun -COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip +COPY --from=build /opt/diun /usr/local/bin/diun +COPY --from=build /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip RUN diun --version ENV DIUN_DB_PATH="/data/diun.db"