...
1#!/usr/bin/env bash
2
3# Copyright 2022 The Sigstore Authors
4#
5# Licensed under the Apache License, Version 2.0 (the "License"";
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -o errexit
18set -o nounset
19set -o pipefail
20
21# Output LDFlAGS for a given environment. LDFLAGS are applied to all go binary
22# builds.
23#
24# Args: env
25function ldflags() {
26 local GIT_VERSION=$(git describe --tags --always --dirty)
27 local GIT_COMMIT=$(git rev-parse HEAD)
28
29 local GIT_TREESTATE="clean"
30 if [[ $(git diff --stat) != '' ]]; then
31 GIT_TREESTATE="dirty"
32 fi
33
34 local DATE_FMT="+%Y-%m-%dT%H:%M:%SZ"
35 local BUILD_DATE=$(date "$DATE_FMT")
36 local SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
37 if [ $SOURCE_DATE_EPOCH ]
38 then
39 local BUILD_DATE=$(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")
40 fi
41
42 echo "-buildid= -X sigs.k8s.io/release-utils/version.gitVersion=$GIT_VERSION \
43 -X sigs.k8s.io/release-utils/version.gitCommit=$GIT_COMMIT \
44 -X sigs.k8s.io/release-utils/version.gitTreeState=$GIT_TREESTATE \
45 -X sigs.k8s.io/release-utils/version.buildDate=$BUILD_DATE"
46}
View as plain text