Skip to content

Commit bc06a6f

Browse files
committed
chore: write build_go.sh script and dependencies
1 parent 27acb98 commit bc06a6f

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

scripts/build_go.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
3+
# This script builds a single Go binary of Coder with the given parameters.
4+
#
5+
# Usage: ./build_go.sh [--version v1.2.3+devel.abcdef] [--os linux] [--arch amd64] [--output path/to/output] [--slim]
6+
#
7+
# Defaults to linux:amd64 with slim disabled, but can be controlled with GOOS,
8+
# GOARCH and CODER_SLIM_BUILD=1. If no version is specified, defaults to the
9+
# version from ./version.sh.
10+
#
11+
# Unless overridden via --output, the built binary will be dropped in
12+
# "$repo_root/dist/coder(-slim)?_$version_$os_$arch" (with a ".exe" suffix for
13+
# windows builds) and the absolute path to the binary will be printed to stdout
14+
# on completion.
15+
16+
set -euo pipefail
17+
# shellcheck source=lib.sh
18+
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
19+
20+
version=""
21+
os="${GOOS:-linux}"
22+
arch="${GOARCH:-amd64}"
23+
slim="${CODER_SLIM_BUILD:-0}"
24+
output_path=""
25+
26+
args="$(getopt -o "" -l version:,os:,arch:,output:,slim -- "$@")"
27+
eval set -- "$args"
28+
while true; do
29+
case "$1" in
30+
--version)
31+
version="$2"
32+
shift 2
33+
;;
34+
--os)
35+
os="$2"
36+
shift 2
37+
;;
38+
--arch)
39+
arch="$2"
40+
shift 2
41+
;;
42+
--output)
43+
output_path="$(realpath "$2")"
44+
shift 2
45+
;;
46+
--slim)
47+
slim=1
48+
shift
49+
;;
50+
--)
51+
shift
52+
break
53+
;;
54+
*)
55+
error "Unrecognized option: $1"
56+
;;
57+
esac
58+
done
59+
60+
if [[ "$version" == "" ]]; then
61+
cdself
62+
version="$(./version.sh)"
63+
fi
64+
65+
build_args=(
66+
-ldflags "-s -w -X 'github.com/coder/coder/buildinfo.tag=$version'"
67+
)
68+
if [[ "$slim" == 0 ]]; then
69+
build_args+=(-tags embed)
70+
fi
71+
72+
# cd to the root of the repo.
73+
cdroot
74+
75+
# Compute default output path.
76+
if [[ "$output_path" == "" ]]; then
77+
dist_dir="dist"
78+
mkdir -p "$dist_dir"
79+
output_path="${dist_dir}/coder_${version}_${os}_${arch}"
80+
if [[ "$os" == "windows" ]]; then
81+
output_path+=".exe"
82+
fi
83+
output_path="$(realpath "$output_path")"
84+
fi
85+
build_args+=(-o "$output_path")
86+
87+
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build \
88+
"${build_args[@]}" \
89+
./cmd/coder 1>&2
90+
91+
echo -n "$output_path"

scripts/lib.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is meant to be sourced by other scripts. To source this script:
4+
# source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
5+
6+
set -euo pipefail
7+
8+
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
9+
PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)
10+
11+
cdself() {
12+
cd "$SCRIPT_DIR" || error "Could not change directory to '$SCRIPT_DIR'"
13+
}
14+
15+
cdroot() {
16+
cd "$PROJECT_ROOT" || error "Could not change directory to '$PROJECT_ROOT'"
17+
}
18+
19+
# Taken from https://stackoverflow.com/a/3915420 (CC-BY-SA 4.0)
20+
# Fails if the directory doesn't exist.
21+
realpath() {
22+
dir="$(dirname "$1")"
23+
base="$(basename "$1")"
24+
echo "$(
25+
cd "$dir" || error "Could not change directory to '$dir'"
26+
pwd -P
27+
)"/"$base"
28+
}
29+
30+
error() {
31+
echo "ERROR: $*" 1>&2
32+
exit 1
33+
}

scripts/version.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
# This script generates the version string used by Coder, including for dev
4+
# versions. Note: the version returned by this script will NOT include the "v"
5+
# prefix that is included in the Git tag.
6+
7+
set -euo pipefail
8+
# shellcheck source=lib.sh
9+
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
10+
cdroot
11+
12+
# $current will equal $last_tag if we're currently checked out on the tag's
13+
# commit.
14+
last_tag="$(git describe --tags --abbrev=0)"
15+
current="$(git describe --always)"
16+
17+
version="$last_tag"
18+
19+
# If the HEAD has extra commits since the last tag then we are in a dev version.
20+
#
21+
# Dev versions are denoted by the "+dev." suffix with a trailing commit short
22+
# SHA.
23+
if [[ "$last_tag" != "$current" ]]; then
24+
if [[ "${CODER_NO_DEV_VERSION:-}" != "" ]]; then
25+
error "version.sh attemped to generate a dev version string when CODER_NO_DEV_VERSION was set"
26+
fi
27+
version+="+dev.$(git rev-parse --short HEAD)"
28+
fi
29+
30+
# Remove the "v" prefix.
31+
echo -n "${version#v}"

0 commit comments

Comments
 (0)