Skip to content

Commit cef0221

Browse files
committed
chore: add scripts for packages
1 parent 4ed419c commit cef0221

File tree

6 files changed

+176
-14
lines changed

6 files changed

+176
-14
lines changed

scripts/archive.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# $AC_APPLICATION_IDENTITY must be set and the signing certificate must be
1616
# imported for this to work. Also, the input binary must already be signed with
1717
# the `codesign` tool.
18+
#
19+
# The absolute output path is printed on success.
1820

1921
set -euo pipefail
2022
# shellcheck source=lib.sh
@@ -93,19 +95,22 @@ ln -s "$(realpath LICENSE)" "$temp_dir/"
9395
# Ensure parent output dir and non-existent output file.
9496
mkdir -p "$(dirname "$output_path")"
9597
if [[ -e "$output_path" ]]; then
96-
error "Output path '$output_path' already exists!"
98+
rm "$output_path"
9799
fi
98100

99101
cd "$temp_dir"
100102
if [[ "$format" == "zip" ]]; then
101-
zip "$output_path" ./*
103+
zip "$output_path" ./* 1>&2
102104
else
103-
tar --dereference -czvf "$output_path" ./*
105+
tar --dereference -czvf "$output_path" ./* 1>&2
104106
fi
105107

108+
cdroot
106109
rm -rf "$temp_dir"
107110

108111
if [[ "$sign_darwin" == 1 ]]; then
109-
echo "Notarizing binary..."
112+
echo "Notarizing archive..."
110113
execrelative ./sign_darwin.sh "$output_path"
111114
fi
115+
116+
echo -n "$output_path"

scripts/build_go.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ slim="${CODER_SLIM_BUILD:-0}"
2929
sign_darwin=0
3030
output_path=""
3131

32-
args="$(getopt -o "" -l version:,os:,arch:,output:,slim -- "$@")"
32+
args="$(getopt -o "" -l version:,os:,arch:,output:,slim,sign-darwin -- "$@")"
3333
eval set -- "$args"
3434
while true; do
3535
case "$1" in
@@ -99,7 +99,7 @@ CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build \
9999
"${build_args[@]}" \
100100
./cmd/coder 1>&2
101101

102-
if [[ "$GOOS" == "darwin" ]] && [[ "$sign_darwin" == 1 ]]; then
102+
if [[ "$sign_darwin" == 1 ]] && [[ "$os" == "darwin" ]]; then
103103
codesign -s "$AC_APPLICATION_IDENTITY" -f -v --timestamp --options runtime "$output_path"
104104
fi
105105

scripts/build_go_matrix.sh

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# This script builds multiple Go binaries for Coder with the given OS and
44
# architecture combinations.
55
#
6-
# Usage: ./build_go_matrix.sh [--version 1.2.3+devel.abcdef] [--output dist/] [--slim] [--sign-darwin] os1:arch1,arch2 os2:arch1 os1:arch3
6+
# Usage: ./build_go_matrix.sh [--version 1.2.3+devel.abcdef] [--output dist/] [--slim] [--sign-darwin] [--archive] [--package-linux] os1:arch1,arch2 os2:arch1 os1:arch3
77
#
88
# If no OS:arch combinations are provided, nothing will happen and no error will
99
# be returned. Slim builds are disabled by default. If no version is specified,
@@ -22,6 +22,14 @@
2222
# If the --sign-darwin parameter is specified, all darwin binaries will be
2323
# signed using the `codesign` utility. $AC_APPLICATION_IDENTITY must be set and
2424
# the signing certificate must be imported for this to work.
25+
#
26+
# If the --archive parameter is specified, all binaries will be archived using
27+
# ./archive.sh. The --sign-darwin parameter will be carried through, and all
28+
# archive files will be dropped in the output directory with the same name as
29+
# the binary and the .zip (for windows and darwin) or .tar.gz extension.
30+
#
31+
# If the --package-linux parameter is specified, all linux binaries will be
32+
# packaged using ./package.sh. Requires the nfpm binary.
2533

2634
set -euo pipefail
2735
# shellcheck source=lib.sh
@@ -31,8 +39,10 @@ version=""
3139
output_path=""
3240
slim=0
3341
sign_darwin=0
42+
archive=0
43+
package_linux=0
3444

35-
args="$(getopt -o "" -l version:,output:,slim,sign-darwin -- "$@")"
45+
args="$(getopt -o "" -l version:,output:,slim,sign-darwin,archive,package-linux -- "$@")"
3646
eval set -- "$args"
3747
while true; do
3848
case "$1" in
@@ -57,6 +67,14 @@ while true; do
5767
sign_darwin=1
5868
shift
5969
;;
70+
--archive)
71+
archive=1
72+
shift
73+
;;
74+
--package-linux)
75+
package_linux=1
76+
shift
77+
;;
6078
--)
6179
shift
6280
break
@@ -72,7 +90,8 @@ if [[ "$output_path" == "" ]]; then
7290
# Input paths are relative, so we don't cdroot at the top, but for this case
7391
# we want it to be relative to the root.
7492
cdroot
75-
output_path="dist/coder_{version}_{os}_{arch}"
93+
mkdir -p dist
94+
output_path="$(realpath "dist/coder_{version}_{os}_{arch}")"
7695
elif [[ "$output_path" == */ ]]; then
7796
output_path="${output_path}coder_{version_{os}_{arch}"
7897
else
@@ -102,10 +121,13 @@ for spec in "$@"; do
102121
error "Could not parse matrix build spec '$spec': invalid architecture '$spec_arch'"
103122
fi
104123

105-
specs+=("$spec_os $spec_arch")
124+
specs+=("$spec_os:$spec_arch")
106125
done
107126
done
108127

128+
# Remove duplicate specs while maintaining the same order.
129+
mapfile -t specs < <(echo "${specs[@]}" | tr " " "\n" | awk '!a[$0]++')
130+
109131
build_args=()
110132
if [[ "$slim" == 1 ]]; then
111133
build_args+=(--slim)
@@ -116,8 +138,8 @@ fi
116138

117139
# Build each spec.
118140
for spec in "${specs[@]}"; do
119-
spec_os="$(echo "$spec" | cut -d " " -f 1)"
120-
spec_arch="$(echo "$spec" | cut -d " " -f 2)"
141+
spec_os="$(echo "$spec" | cut -d ":" -f 1)"
142+
spec_arch="$(echo "$spec" | cut -d ":" -f 2)"
121143

122144
# Craft output path from the template.
123145
spec_output="$output_path"
@@ -142,4 +164,34 @@ for spec in "${specs[@]}"; do
142164
"${build_args[@]}"
143165
echo
144166
echo
167+
168+
if [[ "$archive" == 1 ]]; then
169+
spec_archive_format="tar.gz"
170+
if [[ "$spec_os" == "windows" ]] || [[ "$spec_os" == "darwin" ]]; then
171+
spec_archive_format="zip"
172+
fi
173+
spec_output_archive="$spec_output.$spec_archive_format"
174+
175+
archive_args=()
176+
if [[ "$sign_darwin" == 1 ]] && [[ "$spec_os" == "darwin" ]]; then
177+
archive_args+=(--sign-darwin)
178+
fi
179+
180+
echo "--- Creating archive for $spec_os $spec_arch ($spec_output_archive)"
181+
execrelative ./archive.sh \
182+
--format "$spec_archive_format" \
183+
--output "$spec_output_archive" \
184+
"${archive_args[@]}" \
185+
"$spec_output_binary"
186+
echo
187+
echo
188+
fi
189+
190+
if [[ "$package_linux" == 1 ]] && [[ "$spec_os" == "linux" ]]; then
191+
execrelative ./package.sh \
192+
--arch "$spec_arch" \
193+
--version "$version" \
194+
"$spec_output_binary"
195+
echo
196+
fi
145197
done

scripts/lib.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
set -euo pipefail
77

8-
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
9-
PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)
8+
SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
9+
PROJECT_ROOT="$(cd "$SCRIPT_DIR" && realpath "$(git rev-parse --show-toplevel)")"
1010

1111
# pushd is a silent alternative to the real pushd shell command.
1212
pushd() {

scripts/nfpm.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: coder
2+
platform: linux
3+
arch: "${GOARCH}"
4+
version: "${CODER_VERSION}"
5+
version_schema: semver
6+
release: 1
7+
8+
vendor: Coder
9+
homepage: https://coder.com
10+
maintainer: Coder <support@coder.com>
11+
description: |
12+
Provision development environments with infrastructure with code
13+
license: AGPL-3.0
14+
15+
suggests:
16+
- postgresql
17+
18+
contents:
19+
- src: coder
20+
dst: /usr/bin/coder
21+
- src: coder.env
22+
dst: /etc/coder.d/coder.env
23+
type: "config|noreplace"
24+
- src: coder.service
25+
dst: /usr/lib/systemd/system/coder.service

scripts/package.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
3+
# This script creates Linux packages for the given binary. It will output a
4+
# .rpm, .deb and .apk file in the same directory as the input file with the same
5+
# filename (except the package format suffix).
6+
#
7+
# ./package.sh --arch amd64 [--version 1.2.3] path/to/coder
8+
#
9+
# The --arch parameter is required. If no version is specified, defaults to the
10+
# version from ./version.sh.
11+
12+
set -euo pipefail
13+
# shellcheck source=lib.sh
14+
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
15+
16+
version=""
17+
arch=""
18+
19+
args="$(getopt -o "" -l arch:,version: -- "$@")"
20+
eval set -- "$args"
21+
while true; do
22+
case "$1" in
23+
--arch)
24+
arch="$2"
25+
shift 2
26+
;;
27+
--version)
28+
version="$2"
29+
shift 2
30+
;;
31+
--)
32+
shift
33+
break
34+
;;
35+
*)
36+
error "Unrecognized option: $1"
37+
;;
38+
esac
39+
done
40+
41+
if [[ "$arch" == "" ]]; then
42+
error "--arch is a required parameter"
43+
fi
44+
45+
if [[ "$#" != 1 ]]; then
46+
error "Exactly one argument must be provided to this script, $# were supplied"
47+
fi
48+
if [[ ! -f "$1" ]]; then
49+
error "File '$1' does not exist or is not a regular file"
50+
fi
51+
input_file="$(realpath "$1")"
52+
53+
# Remove the "v" prefix.
54+
version="${version#v}"
55+
if [[ "$version" == "" ]]; then
56+
version="$(execrelative ./version.sh)"
57+
fi
58+
59+
# Make temporary dir where all source files intended to be in the package will
60+
# be hardlinked from.
61+
cdroot
62+
temp_dir="$(TMPDIR="$(dirname "$input_file")" mktemp -d)"
63+
ln -P "$input_file" "$temp_dir/coder"
64+
ln -P "$(realpath coder.env)" "$temp_dir/"
65+
ln -P "$(realpath coder.service)" "$temp_dir/"
66+
ln -P "$(realpath scripts/nfpm.yaml)" "$temp_dir/"
67+
68+
cd "$temp_dir"
69+
70+
formats=(apk deb rpm)
71+
for format in "${formats[@]}"; do
72+
output_path="$input_file.$format"
73+
echo "--- Building $format package ($output_path)"
74+
nfpm package \
75+
-f nfpm.yaml \
76+
-p "$format" \
77+
-t "$output_path"
78+
done
79+
80+
rm -rf "$temp_dir"

0 commit comments

Comments
 (0)