Also add version info to goreleaser (#822)

- shared configuration generation in ./scripts/version-ldflags

Signed-off-by: Appu Goundan <appu@google.com>
This commit is contained in:
Appu 2021-08-09 14:22:30 -04:00 committed by GitHub
parent 2931d91e23
commit 8534836923
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 21 deletions

View File

@ -33,6 +33,10 @@ jobs:
uses: actions/setup-go@37335c7bb261b353407cff977110895fa0b4f7d8 # v2.1.3 uses: actions/setup-go@37335c7bb261b353407cff977110895fa0b4f7d8 # v2.1.3
with: with:
go-version: 1.16 go-version: 1.16
-
name: Configure ldflags
id: ldflags
run: echo "::set-output name=version_flags::$(./scripts/version-ldflags)"
- -
name: Import GPG key name: Import GPG key
id: import_gpg id: import_gpg
@ -49,3 +53,4 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
VERSION_LDFLAGS: ${{ steps.ldflags.outputs.version_flags }}

View File

@ -9,13 +9,16 @@ builds:
- linux - linux
- windows - windows
- darwin - darwin
# List of combinations of GOOS + GOARCH + GOARM to ignore. # List of combinations of GOOS + GOARCH + GOARM to ignore.
# Default is empty. # Default is empty.
ignore: ignore:
- goos: darwin - goos: darwin
goarch: 386 goarch: 386
- goos: darwin - goos: darwin
goarch: arm64 goarch: arm64
# CI should set VERSION_LDFLAGS to the output of ./scripts/version_ldflags
ldflags:
- -s -w {{.Env.VERSION_LDFLAGS}}
archives: archives:
- replacements: - replacements:
linux: Linux linux: Linux

View File

@ -1,6 +1,7 @@
SHELL := /bin/bash SHELL := /bin/bash
GOPATH := $(go env GOPATH) GOPATH := $(go env GOPATH)
GINKGO := ginkgo GINKGO := ginkgo
GIT_HASH := $(git rev-parse HEAD)
GOLANGGCI_LINT := golangci-lint GOLANGGCI_LINT := golangci-lint
PROTOC_GEN_GO := protoc-gen-go PROTOC_GEN_GO := protoc-gen-go
PROTOC := $(shell which protoc) PROTOC := $(shell which protoc)
@ -8,26 +9,7 @@ IMAGE_NAME = scorecard
OUTPUT = output OUTPUT = output
IGNORED_CI_TEST="E2E TEST:blob|E2E TEST:executable" IGNORED_CI_TEST="E2E TEST:blob|E2E TEST:executable"
# generate VERSION_LDFLAGS VERSION_LDFLAGS=$(shell ./scripts/version-ldflags)
GIT_VERSION ?= $(shell git describe --tags --always --dirty)
GIT_HASH ?= $(shell git rev-parse HEAD)
DATE_FMT = +'%Y-%m-%dT%H:%M:%SZ'
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
else
BUILD_DATE ?= $(shell date "$(DATE_FMT)")
endif
GIT_TREESTATE = clean
DIFF = $(shell git diff --quiet >/dev/null 2>&1; if [ $$? -eq 1 ]; then echo "1"; fi)
ifeq ($(DIFF), 1)
GIT_TREESTATE = dirty
endif
# version should be injected in the cmd package
PKG=$(shell go list -m | head -n1)/cmd
VERSION_LDFLAGS=-X $(PKG).gitVersion=$(GIT_VERSION) -X $(PKG).gitCommit=$(GIT_HASH) -X $(PKG).gitTreeState=$(GIT_TREESTATE) -X $(PKG).buildDate=$(BUILD_DATE)
############################### make help ##################################### ############################### make help #####################################
.PHONY: help .PHONY: help

25
scripts/version-ldflags Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
# Copyright 2021 Security Scorecard Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# shared by Makefile and .goreleaser.yml to inject
# version info into cmd/version.go via ldflags
GIT_VERSION=$(git describe --tags --always --dirty)
GIT_HASH=$(git rev-parse HEAD)
BUILD_DATE=$(date +'%Y-%m-%dT%H:%M:%SZ')
GIT_TREESTATE=$(if git diff --quiet; then echo "clean"; else echo "dirty"; fi)
PKG=$(go list -m | head -n1)/cmd
echo "-X $PKG.gitVersion=$GIT_VERSION -X $PKG.gitCommit=$GIT_HASH -X $PKG.gitTreeState=$GIT_TREESTATE -X $PKG.buildDate=$BUILD_DATE"