build(docker): Fix version details in docker image (#1471)
Some checks failed
Extra / Vulnerability scanner (push) Failing after 41s
CI / go-mod (push) Failing after 17m9s
CI / golangci-lint (push) Failing after 15m36s
Release a tag / release (push) Failing after 15m34s
CI / tests-on-windows (push) Has been cancelled
CI / tests-on-macos (push) Has been cancelled
CI / tests-on-unix (1.13) (push) Has been cancelled
CI / tests-on-unix (1.14) (push) Has been cancelled
CI / tests-on-unix (1.15) (push) Has been cancelled
CI / check_generated (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile.alpine]) (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile]) (push) Has been cancelled

* 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. I take this chance
to refactor github action as well.

Fixes #1468

Signed-off-by: Tam Mach <sayboras@yahoo.com>
This commit is contained in:
Tam Mach 2020-10-28 09:03:51 +11:00 committed by GitHub
parent 55e35d27e3
commit dc2d6b5119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 19 deletions

View File

@ -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,38 +25,59 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }}
docker-release:
needs: [ release ]
runs-on: ubuntu-latest
strategy:
matrix:
target:
- Dockerfile: build/Dockerfile
- Dockerfile: build/Dockerfile.alpine
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
id: docker_build
- name: Login do docker.io
run: docker login -u golangci -p ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }}
- 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: |
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
id: docker_build_alpine
uses: docker/build-push-action@v2
with:
context: .
file: build/Dockerfile.alpine
platforms: linux/amd64,linux/arm64
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

View File

@ -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

View File

@ -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