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
commit bce7a4bd90
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

View File

@ -1,23 +0,0 @@
/*
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.
*/
package version
// This file is the template for the machine-edited autogenerated.go.
// Do not modify this file without also modifying hack/version-gen.sh.
var (
commitFromGit = `@@GIT_COMMIT@@`
)

View File

@ -20,6 +20,10 @@ import (
"fmt"
)
// commitFromGit is a constant representing the source version that
// generated this build. It should be set during build via -ldflags.
var commitFromGit string
// Info contains versioning information.
// TODO: Add []string of api versions supported? It's still unclear
// how we'll want to distribute that information.
@ -41,5 +45,9 @@ func Get() Info {
// String returns info as a human-friendly version string.
func (info Info) String() string {
return fmt.Sprintf("version %s.%s, build %s", info.Major, info.Minor, info.GitCommit)
commit := info.GitCommit
if commit == "" {
commit = "(unknown)"
}
return fmt.Sprintf("version %s.%s, build %s", info.Major, info.Minor, commit)
}

View File

@ -27,10 +27,6 @@ INSTANCE_PREFIX=$1
KUBE_DIR=$SCRIPT_DIR/..
# First ensure the version pkg is complete and is up to date
HACK_DIR=$KUBE_DIR/hack
$HACK_DIR/version-gen.sh
# Next build the release tar. This gets copied on to the master and installed
# from there. It includes the go source for the necessary servers along with
# the salt configs.