Merge pull request #1014 from smarterclayton/set_commit_with_ldflags

Use -ldflags to set git commit version
This commit is contained in:
Joe Beda
2014-08-26 10:56:56 -07:00
6 changed files with 33 additions and 72 deletions

View File

@@ -28,8 +28,8 @@ hackdir=$(CDPATH="" cd $(dirname $0); pwd)
# Go to the top of the tree.
cd "${KUBE_REPO_ROOT}"
# Update the version.
"${hackdir}/version-gen.sh"
# Fetch the version.
version=$(gitcommit)
if [[ $# == 0 ]]; then
# Update $@ with the default list of targets to build.
@@ -41,4 +41,4 @@ for arg; do
binaries+=("${KUBE_GO_PACKAGE}/${arg}")
done
go install "${binaries[@]}"
go install -ldflags "-X github.com/GoogleCloudPlatform/kubernetes/pkg/version.commitFromGit '${version}'" "${binaries[@]}"

View File

@@ -17,6 +17,27 @@
# This script sets up a go workspace locally and builds all go components.
# You can 'source' this file if you want to set up GOPATH in your local shell.
# gitcommit prints the current Git commit information
function gitcommit() {
set -o errexit
set -o nounset
set -o pipefail
topdir=$(dirname "$0")/..
cd "${topdir}"
# TODO: when we start making tags, switch to git describe?
if git_commit=$(git rev-parse --short "HEAD^{commit}" 2>/dev/null); then
# Check if the tree is dirty.
if ! dirty_tree=$(git status --porcelain) || [[ -n "${dirty_tree}" ]]; then
echo "${git_commit}-dirty"
fi
else
echo "(none)"
fi
return 0
}
if [[ -z "$(which go)" ]]; then
echo "Can't find 'go' in PATH, please fix and retry." >&2
echo "See http://golang.org/doc/install for installation instructions." >&2

View File

@@ -1,41 +0,0 @@
#!/bin/bash
# Copyright 2014 Google Inc. All rights reserved.
#
# 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.
set -o errexit
set -o nounset
set -o pipefail
topdir=$(dirname "$0")/..
cd "${topdir}"
# TODO: when we start making tags, switch to git describe?
if git_commit=$(git rev-parse --short "HEAD^{commit}" 2>/dev/null); then
# Remove any invalid characters that might confuse "sed".
git_commit=${git_commit//[^a-f0-9]/}
# Check if the tree is dirty.
if ! dirty_tree=$(git status --porcelain) || [[ -n "${dirty_tree}" ]]; then
git_commit="${git_commit}-dirty"
fi
else
git_commit="(none)"
echo "WARNING: unable to find git commit, falling back to commitFromGit = \`${git_commit}\`" >&2
fi
# TODO: Instead of using an autogenerated file, we could pass this variable
# to the source through Go's -X ldflag.
sed "s/@@GIT_COMMIT@@/${git_commit}/g" \
pkg/version/template.go.tmpl >pkg/version/autogenerated.go