From 90c031846dd44a583142058c4894073494dce213 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 21 Jul 2024 20:32:59 +0200 Subject: [PATCH] ci: new docker release workflows --- .github/workflows/ci.yml | 88 ----------------------------------- .github/workflows/docker.yml | 73 +++++++++++++++++++++++++++++ .github/workflows/release.yml | 83 --------------------------------- Dockerfile | 22 +++++++-- cmd/version.go | 4 +- 5 files changed, 93 insertions(+), 177 deletions(-) delete mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/docker.yml delete mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index a42298d..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,88 +0,0 @@ -# Used as inspiration: https://github.com/mvdan/github-actions-golang - -name: Tests - -on: - push: - branches: - - master - pull_request: - branches: - - master - -jobs: - test: - strategy: - # Default is true, cancels jobs for other platforms in the matrix if one fails - fail-fast: false - matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] - go: [ '1.16' ] - - runs-on: ${{ matrix.os }} - - steps: - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - - name: Checkout code - uses: actions/checkout@v2 - - - name: Print Go version and environment - id: vars - run: | - printf "Using go at: $(which go)\n" - printf "Go version: $(go version)\n" - printf "\n\nGo environment:\n\n" - go env - printf "\n\nSystem environment:\n\n" - env - echo "::set-output name=go_cache::$(go env GOCACHE)" - - - name: Cache the build cache - uses: actions/cache@v2 - with: - path: ${{ steps.vars.outputs.go_cache }} - key: ${{ runner.os }}-${{ matrix.go }}-go-ci-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-${{ matrix.go }}-go-ci - - - name: Get dependencies - run: go get -v -t -d ./... - - # To ensure the project compiles, and not just its tests are passing - - name: Build webdav - working-directory: . - env: - CGO_ENABLED: 0 - run: go build -trimpath -ldflags="-w -s" -v - - - name: Run tests - run: go test -v -race ./... - - golangci-lint: - name: runner / golangci-lint - runs-on: ubuntu-latest - steps: - - name: Checkout code into the Go module directory - uses: actions/checkout@v2 - - - name: Run golangci-lint - uses: reviewdog/action-golangci-lint@v1 - with: - github_token: ${{ secrets.github_token }} - - goreleaser-check: - # goreleaser --rm-dist --skip-validate --skip-publish - runs-on: ubuntu-latest - steps: - - name: checkout - uses: actions/checkout@v2 - - uses: goreleaser/goreleaser-action@v2 - with: - version: latest - args: check - env: - TAG: ${{ steps.vars.outputs.version_tag }} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..da469f9 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,73 @@ +name: Docker + +on: + workflow_dispatch: + push: + branches: + - 'main' + tags: + - 'v*' + +jobs: + docker: + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + attestations: write + id-token: write + steps: + - name: Check out the repo + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: | + hacdias/webdav + ghcr.io/hacdias/webdav + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern=v{{version}} + type=semver,pattern=v{{major}}.{{minor}} + type=semver,pattern=v{{major}}.{{minor}}.{{patch}} + + - name: Build and push Docker image + id: push + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm64,linux/arm/v7 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: ${{ steps.meta.outputs.args }} + + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v1 + with: + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 4c312a1..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: Release - -on: - push: - tags: - - 'v*.*.*' - -jobs: - release: - name: Release - strategy: - matrix: - os: [ ubuntu-latest ] - go: [ '1.17' ] - runs-on: ${{ matrix.os }} - - steps: - - name: Install Go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - - name: Checkout code - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v1 - - # https://github.community/t5/GitHub-Actions/How-to-get-just-the-tag-name/m-p/32167/highlight/true#M1027 - - name: Print Go version and environment - id: vars - run: | - printf "Using go at: $(which go)\n" - printf "Go version: $(go version)\n" - printf "\n\nGo environment:\n\n" - go env - printf "\n\nSystem environment:\n\n" - env - echo "::set-output name=short_sha::$(git rev-parse --short HEAD)" - echo "::set-output name=go_cache::$(go env GOCACHE)" - - # Parse semver - TAG=${GITHUB_REF/refs\/tags\//} - SEMVER_RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z\.-]*\)' - TAG_MAJOR=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\1#"` - TAG_MINOR=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\2#"` - TAG_PATCH=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\3#"` - TAG_SPECIAL=`echo ${TAG#v} | sed -e "s#$SEMVER_RE#\4#"` - echo "::set-output name=tag_major::${TAG_MAJOR}" - echo "::set-output name=tag_minor::${TAG_MINOR}" - echo "::set-output name=tag_patch::${TAG_PATCH}" - echo "::set-output name=tag_special::${TAG_SPECIAL}" - - - name: Cache the build cache - uses: actions/cache@v2 - with: - path: ${{ steps.vars.outputs.go_cache }} - key: ${{ runner.os }}-go${{ matrix.go }}-release-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go${{ matrix.go }}-release - - - name: Docker Login - if: success() && startsWith(github.ref, 'refs/tags/v') - env: - DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} - DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} - run: | - echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin - - # GoReleaser will take care of publishing those artifacts into the release - - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v2 - with: - version: latest - args: release --rm-dist - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Dockerfile b/Dockerfile index 32bf3cc..6025820 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,24 @@ -FROM alpine:latest as certs +FROM golang:1.22-alpine3.20 AS build + +ARG DOCKER_META_VERSION="untracked" + RUN apk --update add ca-certificates +WORKDIR /webdav/ + +COPY ./go.mod ./ +COPY ./go.sum ./ +RUN go mod download + +COPY . /webdav/ +RUN go build -o main -ldflags="-X 'github.com/hacdias/webdav/v4/cmd.version=$DOCKER_META_VERSION'" . + FROM scratch -COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt + +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=build /webdav/main /bin/webdav EXPOSE 80 -COPY webdav /webdav -ENTRYPOINT [ "/webdav" ] +ENTRYPOINT [ "webdav" ] +CMD [ "-p", "80" ] diff --git a/cmd/version.go b/cmd/version.go index ddbd84b..c277dbb 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -6,14 +6,14 @@ import ( "github.com/spf13/cobra" ) -var version = "(untracked)" +var version = "untracked" func init() { rootCmd.AddCommand(&cobra.Command{ Use: "version", Short: "Print the version number", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("WebDAV version " + version) + fmt.Printf("WebDAV version: %q", version) }, }) }