diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..2e1b785e0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +# Folders +.git/ +.github/ +changelog/ +doc/ +docker/ +helpers/ + +# Files +.gitignore +.golangci.yml +*.md diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c21c9c4bd..e11eff353 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -269,3 +269,44 @@ jobs: echo "check if go.mod and go.sum are up to date" go mod tidy git diff --exit-code go.mod go.sum + + docker: + name: docker + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v2 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + # list of Docker images to use as base name for tags + images: | + restic/restic + # generate Docker tags based on the following events/attributes + tags: | + type=schedule + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + push: false + context: . + file: docker/Dockerfile + pull: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/docker/Dockerfile b/docker/Dockerfile index 3268f987b..dacb02542 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,7 +1,18 @@ -FROM alpine:latest +FROM golang:1.16-alpine AS builder -COPY restic /usr/bin +WORKDIR /go/src/github.com/restic/restic + +# Caching dependencies +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN go run build.go + +FROM alpine:latest AS restic RUN apk add --update --no-cache ca-certificates fuse openssh-client tzdata +COPY --from=builder /go/src/github.com/restic/restic/restic /usr/bin + ENTRYPOINT ["/usr/bin/restic"] diff --git a/docker/build.sh b/docker/build.sh index 729b837e8..bd4477951 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -2,10 +2,12 @@ set -e -echo "Build binary using golang docker image" -docker run --rm -ti \ - -v "`pwd`":/go/src/github.com/restic/restic \ - -w /go/src/github.com/restic/restic golang:1.16-alpine go run build.go +export DOCKER_BUILDKIT=${DOCKER_BUILDKIT-1} echo "Build docker image restic/restic:latest" -docker build --rm -t restic/restic:latest -f docker/Dockerfile . +docker build \ + --rm \ + --pull \ + --file docker/Dockerfile \ + --tag restic/restic:latest \ + .