git-version: script detection of git tag/commit

This commit is contained in:
Brian Waldon 2015-12-16 14:28:20 -08:00
parent bf13c3bc37
commit c3dd998441
3 changed files with 26 additions and 5 deletions

2
build
View file

@ -25,7 +25,7 @@ mkdir -p $GOPATH/src/github.com/coreos/
# Only attempt to link dex into godeps if it isn't already there # Only attempt to link dex into godeps if it isn't already there
[ -d $GOPATH/src/github.com/coreos/dex ] || ln -s ${PWD} $GOPATH/src/github.com/coreos/dex [ -d $GOPATH/src/github.com/coreos/dex ] || ln -s ${PWD} $GOPATH/src/github.com/coreos/dex
LD_FLAGS="-X main.version=$(git rev-parse HEAD)" LD_FLAGS="-X main.version=$(./git-version)"
go build -o bin/dex-worker -ldflags="$LD_FLAGS" github.com/coreos/dex/cmd/dex-worker go build -o bin/dex-worker -ldflags="$LD_FLAGS" github.com/coreos/dex/cmd/dex-worker
go build -o bin/dexctl github.com/coreos/dex/cmd/dexctl go build -o bin/dexctl github.com/coreos/dex/cmd/dexctl
go build -o bin/dex-overlord -ldflags="$LD_FLAGS" github.com/coreos/dex/cmd/dex-overlord go build -o bin/dex-overlord -ldflags="$LD_FLAGS" github.com/coreos/dex/cmd/dex-overlord

View file

@ -12,9 +12,9 @@ else
docker login --username="$DOCKER_USER" --password="$DOCKER_PASSWORD" --email="dex@example.com" $DOCKER_REGISTRY docker login --username="$DOCKER_USER" --password="$DOCKER_PASSWORD" --email="dex@example.com" $DOCKER_REGISTRY
fi fi
git_sha=$(git rev-parse HEAD) version=$(./git-version)
docker build -q --rm=true -t $repo:$git_sha . docker build -q --rm=true -t $repo:$version .
docker tag -f $repo:$git_sha $repo:latest docker tag -f $repo:$version $repo:latest
docker push $repo:$git_sha docker push $repo:$version
docker push $repo:latest docker push $repo:latest

21
git-version Executable file
View file

@ -0,0 +1,21 @@
#!/bin/bash -e
# pull the current git commit hash
COMMIT=`git rev-parse HEAD`
# check if the current commit has a matching tag
TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)
# use the matching tag as the version, if available
if [ -z "$TAG" ]; then
VERSION=$COMMIT
else
VERSION=$TAG
fi
# check for changed files (not untracked files)
if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then
VERSION="${VERSION}+dirty"
fi
echo $VERSION