Skip to content

Commit 998bc31

Browse files
committed
chore: add docker multi-arch script and release script
1 parent 64f9648 commit 998bc31

10 files changed

+337
-62
lines changed

scripts/archive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ cdroot
109109
rm -rf "$temp_dir"
110110

111111
if [[ "$sign_darwin" == 1 ]]; then
112-
echo "Notarizing archive..."
112+
log "Notarizing archive..."
113113
execrelative ./sign_darwin.sh "$output_path"
114114
fi
115115

scripts/build_docker.sh

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,37 @@
33
# This script builds a Docker image of Coder containing the given binary, for
44
# the given architecture. Only linux binaries are supported at this time.
55
#
6-
# Usage: ./build_docker.sh --arch amd64 --tags v1.2.4-rc.1-arm64,v1.2.3+devel.abcdef [--version 1.2.3] [--push]
6+
# Usage: ./build_docker.sh --arch amd64 [--version 1.2.3] [--push]
77
#
88
# The --arch parameter is required and accepts a Golang arch specification. It
9-
# will be automatically mapped to a suitable architecture that Docker accepts.
9+
# will be automatically mapped to a suitable architecture that Docker accepts
10+
# before being passed to `docker buildx build`.
1011
#
11-
# The image will be built and tagged against all supplied tags. At least one tag
12-
# must be supplied. All tags will be sanitized to remove invalid characters like
13-
# plus signs.
12+
# The image will be built and tagged against the image tag returned by
13+
# ./image_tag.sh.
1414
#
1515
# If no version is specified, defaults to the version from ./version.sh.
1616
#
17-
# If the --push parameter is supplied, all supplied tags will be pushed.
17+
# If the --push parameter is supplied, the image will be pushed.
18+
#
19+
# Prints the image tag on success.
1820

1921
set -euo pipefail
2022
# shellcheck source=lib.sh
2123
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
2224

23-
image="ghcr.io/coder/coder"
2425
arch=""
25-
tags_str=""
2626
version=""
2727
push=0
2828

29-
args="$(getopt -o "" -l arch:,tags:,version:,push -- "$@")"
29+
args="$(getopt -o "" -l arch:,version:,push -- "$@")"
3030
eval set -- "$args"
3131
while true; do
3232
case "$1" in
3333
--arch)
3434
arch="$2"
3535
shift 2
3636
;;
37-
--tags)
38-
tags_str="$2"
39-
shift 2
40-
;;
4137
--version)
4238
version="$2"
4339
shift 2
@@ -60,16 +56,14 @@ if [[ "$arch" == "" ]]; then
6056
error "The --arch parameter is required"
6157
fi
6258

63-
tags=()
64-
for tag in $(echo "$tags_str" | tr "," "\n"); do
65-
# Docker images don't support plus signs, which devel versions may contain.
66-
tag="${tag//+/-}"
67-
tags+=("$tag")
68-
done
69-
if [[ "${#tags[@]}" == 0 ]]; then
70-
error "At least one tag must be supplied through --tags"
59+
# Remove the "v" prefix.
60+
version="${version#v}"
61+
if [[ "$version" == "" ]]; then
62+
version="$(execrelative ./version.sh)"
7163
fi
7264

65+
image_tag="$(execrelative ./image_tag.sh --arch "$arch" --version="$version")"
66+
7367
if [[ "$#" != 1 ]]; then
7468
error "Exactly one argument must be provided to this script, $# were supplied"
7569
fi
@@ -78,12 +72,6 @@ if [[ ! -f "$1" ]]; then
7872
fi
7973
input_file="$(realpath "$1")"
8074

81-
# Remove the "v" prefix.
82-
version="${version#v}"
83-
if [[ "$version" == "" ]]; then
84-
version="$(execrelative ./version.sh)"
85-
fi
86-
8775
# Remap the arch from Golang to Docker.
8876
declare -A arch_map=(
8977
[amd64]="linux/amd64"
@@ -111,21 +99,18 @@ build_args=(
11199
"--label=org.opencontainers.image.source=https://github.com/coder/coder"
112100
"--label=org.opencontainers.image.version=$version"
113101
"--label=org.opencontainers.image.licenses=AGPL-3.0"
102+
"--tag=$image_tag"
114103
)
115-
for tag in "${tags[@]}"; do
116-
build_args+=(--tag "$image:$tag")
117-
done
118104

119-
echo "--- Building Docker image for $arch"
120-
docker buildx build "${build_args[@]}" .
105+
log "--- Building Docker image for $arch ($image_tag)"
106+
docker buildx build "${build_args[@]}" . 1>&2
121107

122108
cdroot
123109
rm -rf "$temp_dir"
124110

125111
if [[ "$push" == 1 ]]; then
126-
echo "--- Pushing Docker images for $arch"
127-
for tag in "${tags[@]}"; do
128-
echo "Pushing $image:$tag"
129-
docker push "$image:$tag"
130-
done
112+
log "--- Pushing Docker image for $arch ($image_tag)"
113+
docker push "$image_tag"
131114
fi
115+
116+
echo -n "$image_tag"

scripts/build_docker_multiarch.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
3+
# This script merges Coder Docker images of different architectures together
4+
# into the archless image tag returned by ./image_tag.sh.
5+
#
6+
# Usage: ./build_docker_multiarch.sh [--version 1.2.3] [--push] image1:tag1 image2:tag2
7+
#
8+
# The supplied images must already be pushed to the registry or this will fail.
9+
# Also, the source images cannot be in a different registry than the target
10+
# image generated by ./image_tag.sh.
11+
#
12+
# If no version is specified, defaults to the version from ./version.sh.
13+
#
14+
# If the --push parameter is supplied, all supplied tags will be pushed.
15+
#
16+
# Returns the merged image tag.
17+
18+
set -euo pipefail
19+
# shellcheck source=lib.sh
20+
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
21+
22+
version=""
23+
push=0
24+
25+
args="$(getopt -o "" -l version:,push -- "$@")"
26+
eval set -- "$args"
27+
while true; do
28+
case "$1" in
29+
--version)
30+
version="$2"
31+
shift 2
32+
;;
33+
--push)
34+
push=1
35+
shift
36+
;;
37+
--)
38+
shift
39+
break
40+
;;
41+
*)
42+
error "Unrecognized option: $1"
43+
;;
44+
esac
45+
done
46+
47+
# Remove the "v" prefix.
48+
version="${version#v}"
49+
if [[ "$version" == "" ]]; then
50+
version="$(execrelative ./version.sh)"
51+
fi
52+
53+
if [[ "$#" == 0 ]]; then
54+
error "At least one argument must be provided to this script, $# were supplied"
55+
fi
56+
57+
create_args=()
58+
for image_tag in "$@"; do
59+
create_args+=(--amend "$image_tag")
60+
done
61+
62+
output_tag="$(execrelative ./image_tag.sh --version "$version")"
63+
log "--- Creating multi-arch Docker image ($output_tag)"
64+
docker manifest create \
65+
"$output_tag" \
66+
"${create_args[@]}"
67+
68+
if [[ "$push" == 1 ]]; then
69+
log "--- Pushing multi-arch Docker image ($output_tag)"
70+
docker push "$output_tag"
71+
fi
72+
73+
echo -n "$output_tag"

scripts/build_go_matrix.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ for spec in "${specs[@]}"; do
155155
# Ensure parent dir.
156156
mkdir -p "$(dirname "$spec_output")"
157157

158-
echo "--- Building coder for $spec_os $spec_arch ($spec_output_binary)"
158+
log "--- Building coder for $spec_os $spec_arch ($spec_output_binary)"
159159
execrelative ./build_go.sh \
160160
--version "$version" \
161161
--os "$spec_os" \
162162
--arch "$spec_arch" \
163163
--output "$spec_output_binary" \
164164
"${build_args[@]}"
165-
echo
166-
echo
165+
log
166+
log
167167

168168
if [[ "$archive" == 1 ]]; then
169169
spec_archive_format="tar.gz"
@@ -177,21 +177,21 @@ for spec in "${specs[@]}"; do
177177
archive_args+=(--sign-darwin)
178178
fi
179179

180-
echo "--- Creating archive for $spec_os $spec_arch ($spec_output_archive)"
180+
log "--- Creating archive for $spec_os $spec_arch ($spec_output_archive)"
181181
execrelative ./archive.sh \
182182
--format "$spec_archive_format" \
183183
--output "$spec_output_archive" \
184184
"${archive_args[@]}" \
185185
"$spec_output_binary"
186-
echo
187-
echo
186+
log
187+
log
188188
fi
189189

190190
if [[ "$package_linux" == 1 ]] && [[ "$spec_os" == "linux" ]]; then
191191
execrelative ./package.sh \
192192
--arch "$spec_arch" \
193193
--version "$version" \
194194
"$spec_output_binary"
195-
echo
195+
log
196196
fi
197197
done

scripts/image_tag.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
3+
# This script prints the image tag to use for the given arch and version
4+
# combination.
5+
#
6+
# Usage: ./image_tag.sh [--arch amd64] [--version 1.2.3]
7+
#
8+
# The --arch parameter accepts a Golang arch specification. If not specified,
9+
# the image tag for the multi-arch image will be returned instead.
10+
#
11+
# If no version is specified, defaults to the version from ./version.sh.
12+
#
13+
# The returned tag will be sanitized to remove invalid characters like the plus
14+
# sign.
15+
16+
set -euo pipefail
17+
# shellcheck source=lib.sh
18+
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
19+
20+
arch=""
21+
version=""
22+
23+
args="$(getopt -o "" -l arch:,version: -- "$@")"
24+
eval set -- "$args"
25+
while true; do
26+
case "$1" in
27+
--arch)
28+
arch="$2"
29+
shift 2
30+
;;
31+
--version)
32+
version="$2"
33+
shift 2
34+
;;
35+
--)
36+
shift
37+
break
38+
;;
39+
*)
40+
error "Unrecognized option: $1"
41+
;;
42+
esac
43+
done
44+
45+
# Remove the "v" prefix.
46+
version="${version#v}"
47+
if [[ "$version" == "" ]]; then
48+
version="$(execrelative ./version.sh)"
49+
fi
50+
51+
image="${CODER_IMAGE_BASE:-ghcr.io/coder/coder}"
52+
tag="v$version"
53+
if [[ "$arch" != "" ]]; then
54+
tag+="-$arch"
55+
fi
56+
57+
tag="${tag//+/-}"
58+
echo -n "$image:$tag"

scripts/lib.sh

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,32 @@ realpath() {
5959
)"/"$base"
6060
}
6161

62+
# maybedryrun prints the given program and flags, and then, if the first
63+
# argument is 0, executes it. The reason the first argument should be 0 is that
64+
# it is expected that you have a dry_run variable in your script that is set to
65+
# 0 by default (i.e. do not dry run) and set to 1 if the --dry-run flag is
66+
# specified.
67+
#
68+
# Usage: maybedryrun 1 gh release create ...
69+
# Usage: maybedryrun 0 docker push ghcr.io/coder/coder:latest
70+
maybedryrun() {
71+
if [[ "$1" == 1 ]]; then
72+
shift
73+
log "DRYRUN: $*"
74+
else
75+
shift
76+
log $ "$@"
77+
"$@"
78+
fi
79+
}
80+
81+
# log prints a message to stderr.
82+
log() {
83+
echo "$*" 1>&2
84+
}
85+
6286
# error prints an error message and returns an error exit code.
6387
error() {
64-
echo "ERROR: $*" 1>&2
88+
log "ERROR: $*"
6589
exit 1
6690
}

scripts/package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ cd "$temp_dir"
7070
formats=(apk deb rpm)
7171
for format in "${formats[@]}"; do
7272
output_path="$input_file.$format"
73-
echo "--- Building $format package ($output_path)"
73+
log "--- Building $format package ($output_path)"
7474
nfpm package \
7575
-f nfpm.yaml \
7676
-p "$format" \

0 commit comments

Comments
 (0)