diff --git a/packer.go b/packer.go index 4fc3cf138..d5fbe7439 100644 --- a/packer.go +++ b/packer.go @@ -27,7 +27,9 @@ func main() { runtime.GOMAXPROCS(runtime.NumCPU()) } - log.Printf("Packer Version: %s %s", packer.Version, packer.VersionPrerelease) + log.Printf( + "Packer Version: %s %s %s", + packer.Version, packer.VersionPrerelease, packer.GitCommit) log.Printf("Packer Target OS/Arch: %s %s", runtime.GOOS, runtime.GOARCH) config, err := loadConfig() diff --git a/packer/version.go b/packer/version.go index 54b98da0b..e351a9fa1 100644 --- a/packer/version.go +++ b/packer/version.go @@ -5,6 +5,10 @@ import ( "fmt" ) +// The git commit that is being compiled. This will be filled in by the +// compiler for source builds. +var GitCommit string + // The version of packer. const Version = "0.1.6" @@ -29,6 +33,10 @@ func (versionCommand) Run(env Environment, args []string) int { fmt.Fprintf(&versionString, ".%s", VersionPrerelease) } + if GitCommit != "" { + fmt.Fprintf(&versionString, " (%s)", GitCommit) + } + env.Ui().Say(versionString.String()) return 0 } diff --git a/scripts/build.sh b/scripts/build.sh index 148082a9c..439231c1f 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,4 +1,6 @@ #!/bin/bash +# +# This script only builds the application from source. set -e NO_COLOR="\x1b[0m" @@ -14,13 +16,23 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" # Change into that directory cd $DIR +# Get the git commit +GIT_COMMIT=$(git rev-parse --short HEAD) +GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES") + # Compile the main Packer app echo -e "${OK_COLOR}--> Compiling Packer${NO_COLOR}" -go build -v -o bin/packer . +go build \ + -ldflags "-X github.com/mitchellh/packer/packer.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ + -v \ + -o bin/packer . # Go over each plugin and build it for PLUGIN in $(find ./plugin -mindepth 1 -maxdepth 1 -type d); do PLUGIN_NAME=$(basename ${PLUGIN}) echo -e "${OK_COLOR}--> Compiling Plugin: ${PLUGIN_NAME}${NO_COLOR}" - go build -v -o bin/packer-${PLUGIN_NAME} ${PLUGIN} + go build \ + -ldflags "-X github.com/mitchellh/packer/packer.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ + -v \ + -o bin/packer-${PLUGIN_NAME} ${PLUGIN} done