Replace build script with go version

This commit is contained in:
Craig Peterson 2017-04-19 14:31:09 -06:00
parent 47f4cab449
commit 3af3638902
4 changed files with 65 additions and 35 deletions

2
.gitignore vendored
View file

@ -9,4 +9,4 @@ ExternalDNS
docs/_site
powershell.log
zones/
integrationTest/.env
*.env

View file

@ -1,31 +0,0 @@
if [ ! -z $1 ]
then
SHA=$1
else
SHA=`git rev-parse HEAD`
fi
PKG=github.com/StackExchange/dnscontrol
DATE=`date +%s`
FLAGS="-s -w -X main.SHA=$SHA -X main.BuildTime=$DATE"
echo $FLAGS
set +e
echo 'Building Linux'
go build -o dnscontrol-Linux -ldflags "$FLAGS" $PKG
echo 'Building Windows'
export GOOS=windows
go build -o dnscontrol.exe -ldflags "$FLAGS" $PKG
echo 'Building Darwin'
export GOOS=darwin
go build -o dnscontrol-Darwin -ldflags "$FLAGS" $PKG
if [ "$COMPRESS" = "1" ]
then
echo 'Compressing executables'
upx dnscontrol.exe
upx dnscontrol-Linux
upx dnscontrol-Darwin
fi

64
build/build.go Normal file
View file

@ -0,0 +1,64 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)
var sha = flag.String("sha", "", "SHA of current commit")
func main() {
flag.Parse()
flags := fmt.Sprintf(`-s -w -X main.SHA="%s" -X main.BuildTime=%d`, getVersion(), time.Now().Unix())
pkg := "github.com/StackExchange/dnscontrol"
build := func(out, goos string) {
log.Printf("Building %s", out)
cmd := exec.Command("go", "build", "-o", out, "-ldflags", flags, pkg)
os.Setenv("GOOS", goos)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
build("dnscontrol-Linux", "linux")
build("dnscontrol.exe", "windows")
build("dnscontrol-Darwin", "darwin")
}
func getVersion() string {
if *sha != "" {
return *sha
}
//check teamcity build version
if v := os.Getenv("BUILD_VCS_NUMBER"); v != "" {
return v
}
//check git
cmd := exec.Command("git", "rev-parse", "HEAD")
v, err := cmd.CombinedOutput()
if err != nil {
return ""
}
ver := strings.TrimSpace(string(v))
//see if dirty
cmd = exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
err = cmd.Run()
// exit status 1 indicates dirty tree
if err != nil {
if err.Error() == "exit status 1" {
ver += "[dirty]"
} else {
log.Printf("!%s!", err.Error())
}
}
return ver
}

View file

@ -1,3 +0,0 @@
# Run build.sh in a docker container that is guaranteed to have all of the appropriate tools we need
PKG=github.com/StackExchange/dnscontrol
docker run -v `pwd`:/go/src/$PKG -w /go/src/github.com/StackExchange/dnscontrol captncraig/golang-build /bin/sh /go/src/github.com/StackExchange/dnscontrol/build.sh