Skip to content

Commit

Permalink
Version-tag builds
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Jul 1, 2016
1 parent 237b91d commit 986e23f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions auth_server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ca-certificates.crt
auth_server
Godeps/
version.*
12 changes: 10 additions & 2 deletions auth_server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ MAKEFLAGS += --warn-undefined-variables
IMAGE ?= cesanta/docker_auth
COMPRESS_BINARY ?= false
CA_BUNDLE = /etc/ssl/certs/ca-certificates.crt
VERSION = $(shell cat version.txt)

BUILDER_IMAGE ?= centurylink/golang-builder
BUILDER_IMAGE_EXTRA-build-cross = -cross
Expand All @@ -15,24 +16,31 @@ local: build-local

update-deps:
go get -v -u -f github.com/tools/godep github.com/jteeuwen/go-bindata/...
go generate ./...

godep:
godep save

build-local: update-deps
go generate ./...
go build

ca-certificates.crt:
cp $(CA_BUNDLE) .

docker-build:
docker run --rm -v $(PWD):/src -e COMPRESS_BINARY=$(COMPRESS_BINARY) $(BUILDER_OPTS-$@) $(BUILDER_IMAGE)$(BUILDER_IMAGE_EXTRA-$@) $(IMAGE)
@echo === Built version $(VERSION) ===

build build-cross: update-deps godep ca-certificates.crt docker-build

docker-tag:
docker tag $(IMAGE):latest $(IMAGE):$(VERSION)

docker-tag-%:
docker tag -f $(IMAGE):latest $(IMAGE):$*
docker tag $(IMAGE):latest $(IMAGE):$*

docker-push:
docker push $(IMAGE):latest $(IMAGE):$(VERSION)

docker-push-%: docker-tag-%
docker push $(IMAGE):$*
Expand Down
60 changes: 60 additions & 0 deletions auth_server/gen_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

import datetime
import sys

# Debian/Ubuntu: apt-get install python-git
# PIP: pip install GitPython
import git

repo = git.Repo('.', search_parent_directories=True)


def get_tag_for_commit(repo, commit):
for tag in repo.tags:
if tag.commit == commit:
return tag.name
return None


if repo.head.is_detached:
branch_or_tag = get_tag_for_commit(repo, repo.head.commit)
if branch_or_tag is None:
branch_or_tag = '?'
else:
branch_or_tag = repo.active_branch

dirty = repo.is_dirty()

ts = datetime.datetime.utcnow()
build_id = '%s/%s@%s%s' % (ts.strftime('%Y%m%d-%H%M%S'),
branch_or_tag,
str(repo.head.commit)[:8],
'+' if dirty else '')

version = None
if not dirty:
version = get_tag_for_commit(repo, repo.head.commit)
if version is None:
version = ts.strftime('%Y%m%d%H%M%S')


if len(sys.argv) == 1 or sys.argv[1] == '-':
f = sys.stdout
else:
f = open(sys.argv[1], 'w')

with open('version.go', 'w') as f:
f.write("""\
package main
const (
\tVersion = "{version}"
\tBuildId = "{build_id}"
)
""".format(version=version, build_id=build_id))

with open('version.txt', 'w') as f:
f.write(version)

f.close()
4 changes: 4 additions & 0 deletions auth_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
limitations under the License.
*/

//go:generate ./gen_version.py

package main // import "github.com/cesanta/docker_auth/auth_server"

import (
Expand Down Expand Up @@ -158,6 +160,8 @@ func main() {
rand.Seed(time.Now().UnixNano())
glog.CopyStandardLogTo("INFO")

glog.Infof("docker_auth %s build %s", Version, BuildId)

cf := flag.Arg(0)
if cf == "" {
glog.Exitf("Config file not specified")
Expand Down

0 comments on commit 986e23f

Please sign in to comment.