Skip to content

Commit dc2d6b5

Browse files
authored
build(docker): Fix version details in docker image (#1471)
* 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 <[email protected]>
1 parent 55e35d2 commit dc2d6b5

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

.github/workflows/tag.yml

+37-17
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,67 @@ jobs:
1717
go-version: 1.15
1818
- name: Unshallow
1919
run: git fetch --prune --unshallow
20-
- name: Login do docker.io
21-
run: docker login -u golangci -p ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }}
20+
2221
- name: Create release
2322
uses: goreleaser/goreleaser-action@v2
2423
with:
2524
version: latest
2625
args: release --rm-dist
2726
env:
2827
GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }}
28+
29+
docker-release:
30+
needs: [ release ]
31+
runs-on: ubuntu-latest
32+
strategy:
33+
matrix:
34+
target:
35+
- Dockerfile: build/Dockerfile
36+
- Dockerfile: build/Dockerfile.alpine
37+
steps:
38+
- uses: actions/checkout@v2
39+
40+
- name: Install Go
41+
uses: actions/setup-go@v2
42+
with:
43+
go-version: 1.15
44+
45+
- name: Unshallow
46+
run: git fetch --prune --unshallow
47+
2948
- name: Prepare
3049
id: prepare
3150
run: |
3251
TAG=${GITHUB_REF#refs/tags/}
3352
MAJOR=${TAG%.*}
53+
SHORT_COMMIT=${GITHUB_SHA::8}
54+
DATE=$(date '+%Y-%m-%dT%H:%M:%SZ')
3455
echo ::set-output name=tag_name::${TAG}
3556
echo ::set-output name=major_tag::${MAJOR}
57+
echo ::set-output name=short_commit::${SHORT_COMMIT}
58+
echo ::set-output name=date::${DATE}
59+
3660
- name: Set up QEMU
3761
uses: docker/setup-qemu-action@v1
62+
3863
- name: Set up Docker Buildx
3964
uses: docker/setup-buildx-action@v1
40-
- name: build and publish main image
41-
id: docker_build
65+
66+
- name: Login do docker.io
67+
run: docker login -u golangci -p ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }}
68+
69+
- name: Build and publish ${{ matrix.target.Dockerfile }}
4270
uses: docker/build-push-action@v2
4371
with:
4472
context: .
45-
file: build/Dockerfile
73+
file: ${{ matrix.target.Dockerfile }}
4674
platforms: linux/amd64,linux/arm64
4775
push: true
76+
build-args: |
77+
VERSION=${{ steps.prepare.outputs.tag_name }}
78+
SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }}
79+
DATE=${{ steps.prepare.outputs.date }}
4880
tags: |
4981
golangci/golangci-lint:${{ steps.prepare.outputs.tag_name }}
5082
golangci/golangci-lint:${{ steps.prepare.outputs.major_tag }}
5183
golangci/golangci-lint:latest
52-
- name: build and publish alpine image
53-
id: docker_build_alpine
54-
uses: docker/build-push-action@v2
55-
with:
56-
context: .
57-
file: build/Dockerfile.alpine
58-
platforms: linux/amd64,linux/arm64
59-
push: true
60-
tags: |
61-
golangci/golangci-lint:${{ steps.prepare.outputs.tag_name }}-alpine
62-
golangci/golangci-lint:${{ steps.prepare.outputs.major_tag }}-alpine
63-
golangci/golangci-lint:latest-alpine

build/Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# stage 1 building the code
22
FROM golang:1.15 as builder
33

4+
ARG VERSION
5+
ARG SHORT_COMMIT
6+
ARG DATE
7+
48
COPY / /golangci
59
WORKDIR /golangci
6-
RUN go build -o golangci-lint ./cmd/golangci-lint/main.go
10+
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
711

812
# stage 2
913
FROM golang:1.15

build/Dockerfile.alpine

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# stage 1 building the code
22
FROM golang:1.15-alpine as builder
33

4+
ARG VERSION
5+
ARG SHORT_COMMIT
6+
ARG DATE
7+
48
COPY / /golangci
59
WORKDIR /golangci
6-
RUN CGO_ENABLED=0 go build -o golangci-lint ./cmd/golangci-lint/main.go
10+
11+
# gcc is required to support cgo;
12+
# git and mercurial are needed most times for go get`, etc.
13+
# See https://github.com/docker-library/golang/issues/80
14+
RUN apk --no-cache add gcc musl-dev git mercurial
15+
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
716

817
# stage 2
918
FROM golang:1.15-alpine

0 commit comments

Comments
 (0)