From e807ce32e3d4a14c8dbbf9a9ed297e54afdf9830 Mon Sep 17 00:00:00 2001 From: Tam Mach Date: Mon, 26 Oct 2020 20:13:25 +1100 Subject: [PATCH 1/2] build(docker): Fix version details in docker image As part of #1383, multi-arch docker build was supported. However, ldflags for version details was missing. This commit is to add -ldflags as part of Docker build. Fixes #1468 Signed-off-by: Tam Mach --- .github/workflows/tag.yml | 54 +++++++++++++++++++++++++++++++-------- build/Dockerfile | 6 ++++- build/Dockerfile.alpine | 11 +++++++- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index b970de5b25cb..a88769cdbe4b 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -17,8 +17,7 @@ jobs: go-version: 1.15 - name: Unshallow run: git fetch --prune --unshallow - - name: Login do docker.io - run: docker login -u golangci -p ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }} + - name: Create release uses: goreleaser/goreleaser-action@v2 with: @@ -26,18 +25,44 @@ jobs: args: release --rm-dist env: GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }} + + docker-release: + needs: [ release ] + runs-on: ubuntu-latest + env: + GOLANGCI_LINT_DOCKER_TOKEN: ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }} + steps: + - uses: actions/checkout@v2 + + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: 1.15 + - name: Unshallow + run: git fetch --prune --unshallow + - name: Prepare id: prepare run: | TAG=${GITHUB_REF#refs/tags/} MAJOR=${TAG%.*} + SHORT_COMMIT=${GITHUB_SHA::8} + DATE=$(date '+%Y-%m-%dT%H:%M:%SZ') echo ::set-output name=tag_name::${TAG} echo ::set-output name=major_tag::${MAJOR} + echo ::set-output name=short_commit::${SHORT_COMMIT} + echo ::set-output name=date::${DATE} + - name: Set up QEMU uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - name: build and publish main image + + - name: Login do docker.io + run: docker login -u sayboras -p ${{ env.GOLANGCI_LINT_DOCKER_TOKEN }} + + - name: Build and publish main image id: docker_build uses: docker/build-push-action@v2 with: @@ -45,19 +70,28 @@ jobs: file: build/Dockerfile platforms: linux/amd64,linux/arm64 push: true + build-args: | + VERSION=${{ steps.prepare.outputs.tag_name }} + SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }} + DATE=${{ steps.prepare.outputs.date }} tags: | - golangci/golangci-lint:${{ steps.prepare.outputs.tag_name }} - golangci/golangci-lint:${{ steps.prepare.outputs.major_tag }} - golangci/golangci-lint:latest - - name: build and publish alpine image + sayboras/golangci-lint:${{ steps.prepare.outputs.tag_name }} + sayboras/golangci-lint:${{ steps.prepare.outputs.major_tag }} + sayboras/golangci-lint:latest + + - name: Build and publish alpine image id: docker_build_alpine uses: docker/build-push-action@v2 with: context: . file: build/Dockerfile.alpine platforms: linux/amd64,linux/arm64 + build-args: | + VERSION=${{ steps.prepare.outputs.tag_name }} + SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }} + DATE=${{ steps.prepare.outputs.date }} push: true tags: | - golangci/golangci-lint:${{ steps.prepare.outputs.tag_name }}-alpine - golangci/golangci-lint:${{ steps.prepare.outputs.major_tag }}-alpine - golangci/golangci-lint:latest-alpine + sayboras/golangci-lint:${{ steps.prepare.outputs.tag_name }}-alpine + sayboras/golangci-lint:${{ steps.prepare.outputs.major_tag }}-alpine + sayboras/golangci-lint:latest-alpine diff --git a/build/Dockerfile b/build/Dockerfile index b9dc78c1d508..a067f26df226 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,9 +1,13 @@ # stage 1 building the code FROM golang:1.15 as builder +ARG VERSION +ARG SHORT_COMMIT +ARG DATE + COPY / /golangci WORKDIR /golangci -RUN go build -o golangci-lint ./cmd/golangci-lint/main.go +RUN CGO_ENABLED=0 go build -ldflags "-s -w -X main.version=$VERSION -X main.commit=$SHORT_COMMIT -X main.date=$DATE" -o golangci-lint ./cmd/golangci-lint/main.go # stage 2 FROM golang:1.15 diff --git a/build/Dockerfile.alpine b/build/Dockerfile.alpine index 065c6596db6b..59274d653053 100644 --- a/build/Dockerfile.alpine +++ b/build/Dockerfile.alpine @@ -1,9 +1,18 @@ # stage 1 building the code FROM golang:1.15-alpine as builder +ARG VERSION +ARG SHORT_COMMIT +ARG DATE + COPY / /golangci WORKDIR /golangci -RUN CGO_ENABLED=0 go build -o golangci-lint ./cmd/golangci-lint/main.go + +# gcc is required to support cgo; +# git and mercurial are needed most times for go get`, etc. +# See https://github.com/docker-library/golang/issues/80 +RUN apk --no-cache add gcc musl-dev git mercurial +RUN CGO_ENABLED=0 go build -ldflags "-s -w -X main.version=$VERSION -X main.commit=$SHORT_COMMIT -X main.date=$DATE" -o golangci-lint ./cmd/golangci-lint/main.go # stage 2 FROM golang:1.15-alpine From 13dadd63c3e2b4de86852a639591844038836d49 Mon Sep 17 00:00:00 2001 From: Tam Mach Date: Mon, 26 Oct 2020 21:02:28 +1100 Subject: [PATCH 2/2] build(github): Refactor github action job Refactor docker release job for readability Signed-off-by: Tam Mach --- .github/workflows/tag.yml | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index a88769cdbe4b..8184e2c9778c 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -29,8 +29,11 @@ jobs: docker-release: needs: [ release ] runs-on: ubuntu-latest - env: - GOLANGCI_LINT_DOCKER_TOKEN: ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }} + strategy: + matrix: + target: + - Dockerfile: build/Dockerfile + - Dockerfile: build/Dockerfile.alpine steps: - uses: actions/checkout@v2 @@ -38,6 +41,7 @@ jobs: uses: actions/setup-go@v2 with: go-version: 1.15 + - name: Unshallow run: git fetch --prune --unshallow @@ -60,14 +64,13 @@ jobs: uses: docker/setup-buildx-action@v1 - name: Login do docker.io - run: docker login -u sayboras -p ${{ env.GOLANGCI_LINT_DOCKER_TOKEN }} + run: docker login -u golangci -p ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }} - - name: Build and publish main image - id: docker_build + - name: Build and publish ${{ matrix.target.Dockerfile }} uses: docker/build-push-action@v2 with: context: . - file: build/Dockerfile + file: ${{ matrix.target.Dockerfile }} platforms: linux/amd64,linux/arm64 push: true build-args: | @@ -75,23 +78,6 @@ jobs: SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }} DATE=${{ steps.prepare.outputs.date }} tags: | - sayboras/golangci-lint:${{ steps.prepare.outputs.tag_name }} - sayboras/golangci-lint:${{ steps.prepare.outputs.major_tag }} - sayboras/golangci-lint:latest - - - name: Build and publish alpine image - id: docker_build_alpine - uses: docker/build-push-action@v2 - with: - context: . - file: build/Dockerfile.alpine - platforms: linux/amd64,linux/arm64 - build-args: | - VERSION=${{ steps.prepare.outputs.tag_name }} - SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }} - DATE=${{ steps.prepare.outputs.date }} - push: true - tags: | - sayboras/golangci-lint:${{ steps.prepare.outputs.tag_name }}-alpine - sayboras/golangci-lint:${{ steps.prepare.outputs.major_tag }}-alpine - sayboras/golangci-lint:latest-alpine + golangci/golangci-lint:${{ steps.prepare.outputs.tag_name }} + golangci/golangci-lint:${{ steps.prepare.outputs.major_tag }} + golangci/golangci-lint:latest