Skip to content

Commit 9744e3a

Browse files
committed
build(docker): Fix version details in docker image
As part of golangci#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 golangci#1468 Signed-off-by: Tam Mach <[email protected]>
1 parent f414375 commit 9744e3a

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

.github/workflows/tag.yml

+50-16
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,81 @@ 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 }}
22-
- name: Create release
23-
uses: goreleaser/goreleaser-action@v2
20+
21+
# - name: Create release
22+
# uses: goreleaser/goreleaser-action@v2
23+
# with:
24+
# version: latest
25+
# args: release --rm-dist
26+
# env:
27+
# GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }}
28+
29+
docker-release:
30+
needs: [ release ]
31+
runs-on: ubuntu-latest
32+
env:
33+
GOLANGCI_LINT_DOCKER_TOKEN: ${{ secrets.GOLANGCI_LINT_DOCKER_TOKEN }}
34+
steps:
35+
- uses: actions/checkout@v2
36+
37+
- name: Install Go
38+
uses: actions/setup-go@v2
2439
with:
25-
version: latest
26-
args: release --rm-dist
27-
env:
28-
GITHUB_TOKEN: ${{ secrets.GOLANGCI_LINT_TOKEN }}
40+
go-version: 1.15
41+
- name: Unshallow
42+
run: git fetch --prune --unshallow
43+
2944
- name: Prepare
3045
id: prepare
3146
run: |
3247
TAG=${GITHUB_REF#refs/tags/}
3348
MAJOR=${TAG%.*}
49+
SHORT_COMMIT=${GITHUB_SHA::8}
50+
DATE=$(date '+%Y-%m-%dT%H:%M:%SZ')
3451
echo ::set-output name=tag_name::${TAG}
3552
echo ::set-output name=major_tag::${MAJOR}
53+
echo ::set-output name=short_commit::${SHORT_COMMIT}
54+
echo ::set-output name=date::${DATE}
55+
3656
- name: Set up QEMU
3757
uses: docker/setup-qemu-action@v1
58+
3859
- name: Set up Docker Buildx
3960
uses: docker/setup-buildx-action@v1
40-
- name: build and publish main image
61+
62+
- name: Login do docker.io
63+
run: docker login -u sayboras -p ${{ env.GOLANGCI_LINT_DOCKER_TOKEN }}
64+
65+
- name: Build and publish main image
4166
id: docker_build
4267
uses: docker/build-push-action@v2
4368
with:
4469
context: .
4570
file: build/Dockerfile
4671
platforms: linux/amd64,linux/arm64
4772
push: true
73+
build-args: |
74+
VERSION=${{ steps.prepare.outputs.tag_name }}
75+
SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }}
76+
DATE=${{ steps.prepare.outputs.date }}
4877
tags: |
49-
golangci/golangci-lint:${{ steps.prepare.outputs.tag_name }}
50-
golangci/golangci-lint:${{ steps.prepare.outputs.major_tag }}
51-
golangci/golangci-lint:latest
52-
- name: build and publish alpine image
78+
sayboras/golangci-lint:${{ steps.prepare.outputs.tag_name }}
79+
sayboras/golangci-lint:${{ steps.prepare.outputs.major_tag }}
80+
sayboras/golangci-lint:latest
81+
82+
- name: Build and publish alpine image
5383
id: docker_build_alpine
5484
uses: docker/build-push-action@v2
5585
with:
5686
context: .
5787
file: build/Dockerfile.alpine
5888
platforms: linux/amd64,linux/arm64
89+
build-args: |
90+
VERSION=${{ steps.prepare.outputs.tag_name }}
91+
SHORT_COMMIT=${{ steps.prepare.outputs.short_commit }}
92+
DATE=${{ steps.prepare.outputs.date }}
5993
push: true
6094
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
95+
sayboras/golangci-lint:${{ steps.prepare.outputs.tag_name }}-alpine
96+
sayboras/golangci-lint:${{ steps.prepare.outputs.major_tag }}-alpine
97+
sayboras/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 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 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)