2016-02-26 02:21:20 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Caddy build script. Automates proper versioning.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
2016-04-05 19:57:50 -05:00
|
|
|
# $ ./build.bash [output_filename] [git_repo]
|
2016-02-26 02:21:20 -05:00
|
|
|
#
|
|
|
|
# Outputs compiled program in current directory.
|
|
|
|
# Default file name is 'ecaddy'.
|
2016-04-05 19:57:50 -05:00
|
|
|
# Default git repo is current directory.
|
|
|
|
# Builds always take place from current directory.
|
2016-03-18 17:10:24 -05:00
|
|
|
|
2016-03-18 17:45:56 -05:00
|
|
|
set -euo pipefail
|
2016-02-26 02:21:20 -05:00
|
|
|
|
2016-03-18 17:45:56 -05:00
|
|
|
: ${output_filename:="${1:-}"}
|
2016-03-18 17:10:24 -05:00
|
|
|
: ${output_filename:="ecaddy"}
|
2016-02-26 02:21:20 -05:00
|
|
|
|
2016-04-05 19:57:50 -05:00
|
|
|
: ${git_repo:="${2:-}"}
|
|
|
|
|
2016-02-26 02:21:20 -05:00
|
|
|
pkg=main
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags=()
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Timestamp of build
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.buildDate"
|
2016-03-20 15:13:50 -05:00
|
|
|
value=$(date -u +"%a %b %d %H:%M:%S %Z %Y")
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Current tag, if HEAD is on a tag
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitTag"
|
2016-02-26 02:21:20 -05:00
|
|
|
set +e
|
2016-04-05 19:57:50 -05:00
|
|
|
value="$(git -C "${git_repo}" describe --exact-match HEAD 2>/dev/null)"
|
2016-02-26 02:21:20 -05:00
|
|
|
set -e
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Nearest tag on branch
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitNearestTag"
|
2016-04-05 19:57:50 -05:00
|
|
|
value="$(git -C "${git_repo}" describe --abbrev=0 --tags HEAD)"
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Commit SHA
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitCommit"
|
2016-04-05 19:57:50 -05:00
|
|
|
value="$(git -C "${git_repo}" rev-parse --short HEAD)"
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
2016-03-18 17:10:24 -05:00
|
|
|
# Summary of uncommitted changes
|
|
|
|
name="${pkg}.gitShortStat"
|
2016-04-05 19:57:50 -05:00
|
|
|
value="$(git -C "${git_repo}" diff-index --shortstat HEAD)"
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# List of modified files
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitFilesModified"
|
2016-04-05 19:57:50 -05:00
|
|
|
value="$(git -C "${git_repo}" diff-index --name-only HEAD)"
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
|
|
|
|
2016-03-20 15:13:50 -05:00
|
|
|
go build -ldflags "${ldflags[*]}" -o "${output_filename}"
|