Skip to content

Commit

Permalink
Make build and dist scripts generic
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Apr 29, 2016
1 parent 02e0c21 commit 18f822d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 53 deletions.
64 changes: 38 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
TEST?=./...
NAME = $(shell awk -F\" '/^const Name/ { print $$2; exit }' main.go)
NAME?=$(shell basename "${CURDIR}")
VERSION = $(shell awk -F\" '/^const Version/ { print $$2; exit }' main.go)

default: test

# bin generates the releasable binaries for Consul Template
# bin generates the binaries for all platforms.
bin: generate
@sh -c "'$(CURDIR)/scripts/build.sh'"
@sh -c "'${CURDIR}/scripts/build.sh' '${NAME}'"

# dev creates binares for testing locally. There are put into ./bin and $GOPAHT
# dev creates binares for testing locally - they are put into ./bin and $GOPATH.
dev: generate
@CT_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'"
@DEV=1 sh -c "'${CURDIR}/scripts/build.sh' '${NAME}'"

# dist creates the binaries for distibution
dist: bin
@sh -c "'$(CURDIR)/scripts/dist.sh' $(VERSION)"
# dist creates the binaries for distibution.
dist:
@sh -c "'${CURDIR}/scripts/dist.sh' '${NAME}' '${VERSION}'"

# test runs the test suite and vets the code
# test runs the test suite and vets the code.
test: generate
go test $(TEST) $(TESTARGS) -timeout=30s -parallel=4
@echo "==> Running tests..."
@go list $(TEST) \
| grep -v "github.com/hashicorp/${NAME}/vendor" \
| xargs -n1 go test -timeout=60s -parallel=10 ${TESTARGS}

# testrace runs the race checker
testrace: generate
go test -race $(TEST) $(TESTARGS)
@echo "==> Running tests (race)..."
@go list $(TEST) \
| grep -v "github.com/hashicorp/${NAME}/vendor" \
| xargs -n1 go test -timeout=60s -race ${TESTARGS}

# updatedeps installs all the dependencies Consul Template needs to run and
# build
# updatedeps installs all the dependencies needed to run and build.
updatedeps:
go get -u github.com/mitchellh/gox
go get -f -t -u ./...
go list ./... \
| xargs go list -f '{{join .Deps "\n"}}' \
| grep -v github.com/hashicorp/envconsul \
| grep -v '/internal/' \
| sort -u \
| xargs go get -f -u

# generate runs `go generate` to build the dynamically generated
# source files.
@echo "==> Updating dependencies..."
@echo " Cleaning previous dependencies..."
@rm -rf vendor/
@echo " Updating to newest dependencies..."
@go get -f -t -u ./...
@echo " Saving dependencies..."
godep save

# generate runs `go generate` to build the dynamically generated source files.
generate:
@echo "==> Generating..."
@find . -type f -name '.DS_Store' -delete
go generate ./...
@go list ./... \
| grep -v "github.com/hashicorp/${NAME}/vendor" \
| xargs -n1 go generate

# bootstrap installs the necessary go tools for development/build.
bootstrap:
@echo "==> Bootstrapping..."
go get -u github.com/tools/godep
go get -u github.com/mitchellh/gox

.PHONY: default bin dev dist test testrace updatedeps vet generate
.PHONY: default bin dev dist test testrace updatedeps vet generate bootstrap
26 changes: 10 additions & 16 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
# This script builds the application from source for multiple platforms.
set -e

# Get the name from the command line
NAME=$1
if [ -z $NAME ]; then
echo "Please specify a name."
exit 1
fi

# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
Expand All @@ -17,7 +24,7 @@ GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)

# Determine the arch/os combos we're building for
XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
XC_OS=${XC_OS:-darwin freebsd linux netbsd openbsd solaris windows }
XC_OS=${XC_OS:-"darwin freebsd linux netbsd openbsd solaris windows"}

# Install dependencies
echo "==> Getting dependencies..."
Expand All @@ -30,7 +37,7 @@ rm -rf pkg/*
mkdir -p bin/

# If its dev mode, only build for ourself
if [ "${CT_DEV}x" != "x" ]; then
if [ "${DEV}x" != "x" ]; then
XC_OS=$(go env GOOS)
XC_ARCH=$(go env GOARCH)
fi
Expand All @@ -41,7 +48,7 @@ gox \
-os="${XC_OS}" \
-arch="${XC_ARCH}" \
-ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
-output "pkg/{{.OS}}_{{.Arch}}/envconsul" \
-output "pkg/{{.OS}}_{{.Arch}}/${NAME}" \
.

# Move all the compiled things to the $GOPATH/bin
Expand All @@ -62,19 +69,6 @@ for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
cp ${F} ${MAIN_GOPATH}/bin/
done

if [ "${CT_DEV}x" = "x" ]; then
# Zip and copy to the dist dir
echo "==> Packaging..."
for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
OSARCH=$(basename ${PLATFORM})
echo "--> ${OSARCH}"

pushd $PLATFORM >/dev/null 2>&1
zip ../${OSARCH}.zip ./*
popd >/dev/null 2>&1
done
fi

# Done!
echo
echo "==> Results:"
Expand Down
57 changes: 46 additions & 11 deletions scripts/dist.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,69 @@
#!/bin/bash
set -e

# Get the version from the command line
VERSION=$1
# Get the name from the command line.
NAME=$1
if [ -z $NAME ]; then
echo "Please specify a name."
exit 1
fi

# Get the version from the command line.
VERSION=$2
if [ -z $VERSION ]; then
echo "Please specify a version."
exit 1
echo "Please specify a version."
exit 1
fi

# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"

# Change into that dir because we expect that
cd $DIR
# Change into that dir because we expect that.
cd "$DIR"

# Build
if [ -z $NOBUILD ]; then
echo "==> Building..."
docker run \
--rm \
--workdir="/go/src/github.com/hashicorp/${NAME}" \
--volume="$(pwd):/go/src/github.com/hashicorp/${NAME}" \
golang:1.6.2 /bin/sh -c "make bootstrap && make bin"
fi

# Generate the tag.
if [ -z $NOTAG ]; then
echo "==> Tagging..."
git commit --allow-empty -a --gpg-sign=348FFC4C -m "Release v$VERSION"
git tag -a -m "Version $VERSION" -s -u 348FFC4C "v${VERSION}" master
fi

# Zip all the files.
echo "==> Packaging..."
for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
OSARCH=$(basename ${PLATFORM})
echo "--> ${OSARCH}"

pushd $PLATFORM >/dev/null 2>&1
zip ../${OSARCH}.zip ./*
popd >/dev/null 2>&1
done

# Zip all the files
# Move everything into dist.
rm -rf ./pkg/dist
mkdir -p ./pkg/dist
for FILENAME in $(find ./pkg -mindepth 1 -maxdepth 1 -type f); do
FILENAME=$(basename $FILENAME)
cp ./pkg/${FILENAME} ./pkg/dist/envconsul_${VERSION}_${FILENAME}
cp ./pkg/${FILENAME} ./pkg/dist/${NAME}_${VERSION}_${FILENAME}
done

# Make the checksums
# Make and sign the checksums.
pushd ./pkg/dist
shasum -a256 * > ./envconsul_${VERSION}_SHA256SUMS
shasum -a256 * > ./${NAME}_${VERSION}_SHA256SUMS
if [ -z $NOSIGN ]; then
echo "==> Signing..."
gpg --default-key 348FFC4C --detach-sig ./envconsul_${VERSION}_SHA256SUMS
gpg --default-key 348FFC4C --detach-sig ./${NAME}_${VERSION}_SHA256SUMS
fi
popd

0 comments on commit 18f822d

Please sign in to comment.