Skip to content

Commit 77d4ff6

Browse files
committed
add a build script, that creates github releases. includes 2 types of
linux binaries, one for glibc and one for musl libc (for alpine linux)
1 parent 1ec9f46 commit 77d4ff6

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM alpine:3.4
2+
3+
RUN apk add -U --repository http://dl-3.alpinelinux.org/alpine/edge/community/ go=1.7.1-r0
4+
5+
COPY . /tmp
6+
7+
RUN mkdir -p /tmp/go
8+
9+
ENV GOPATH /tmp/go

build_upload.sh

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/bash
2+
3+
# Handle the building and uploading of binaries to GitHub releases.
4+
5+
# There are 2 build processes:
6+
# - one on any Linux machine (assume glibc), using GOOS.
7+
# - the other is within an Alpine Linux container, so it builds against musl libc as an interpreter.
8+
9+
target_platforms="linux_amd64 linux_musl_amd64"
10+
11+
if [ $# -ne 1 ]; then
12+
echo "Usage: $0 version_number"
13+
exit 1
14+
fi
15+
16+
which github-release >/dev/null 2>&1
17+
if [ $? -ne 0 ]; then
18+
echo "Install github-release first, run: go get github.com/aktau/github-release"
19+
exit 1
20+
fi
21+
22+
which zip >/dev/null 2>&1
23+
if [ $? -ne 0 ]; then
24+
echo "zip utility not found, cannot proceed."
25+
exit 1
26+
fi
27+
28+
which docker >/dev/null 2>&1
29+
if [ $? -ne 0 ]; then
30+
echo "docker not found, cannot proceed."
31+
exit 1
32+
fi
33+
34+
github_user=$(jq -e -r .user ~/.github_credentials.json)
35+
github_token=$(jq -e -r .token ~/.github_credentials.json)
36+
if [[ -z "$github_user" || "$github_user" == "null" ]]; then
37+
echo "Cannot find your GitHub username, do you have a ~/.github_credentials.json file? It should have a key called 'user'."
38+
exit 1
39+
fi
40+
if [[ -z "$github_token" || "$github_token" == "null" ]]; then
41+
echo "Cannot find your GitHub token, do you have a ~/.github_credentials.json file? It should have a key called 'token'."
42+
exit 1
43+
fi
44+
45+
# TODOLATER: validate version# matches semver standards
46+
new_version="$1"
47+
echo "New Version: ${new_version}"
48+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
49+
50+
set -e
51+
52+
# Adjust version.go
53+
echo "$(date) : Adjusting version.go to reflect new version..."
54+
if [[ $OSTYPE =~ ^darwin ]]; then
55+
sed -i .bak "s/ecs_discoverer_version = \".*\"$/ecs_discoverer_version = \"${new_version}\"/" "${DIR}/version.go"
56+
rm -f "${DIR}/version.go.bak"
57+
else
58+
sed -i "s/ecs_discoverer_version = \".*\"$/ecs_discoverer_version = \"${new_version}\"/" "${DIR}/version.go"
59+
fi
60+
if [ ! -z "$(git status -s | grep "version.go$")" ]; then
61+
git pull
62+
git commit -m "Bump version.go for new release ${new_version}" version.go
63+
git push
64+
fi
65+
echo "$(date) : Done."
66+
67+
# Remove any old binaries and zips first.
68+
rm -f "${DIR}/bin/*"
69+
70+
# Build for various OSes/archs
71+
echo "$(date) : Building for Linux (glibc)..."
72+
GOOS=linux GOARCH=amd64 go build -o "${DIR}/bin/ecs-discoverer-${new_version}-linux_amd64"
73+
echo "$(date) : Build for Linux completed"
74+
#
75+
echo "$(date) : Building for Alpine Linux (musl libc)..."
76+
build_image="ecs_discoverer_build"
77+
build_container="ecs_discoverer_builder"
78+
docker build -t "$build_image" .
79+
docker run -d --name "$build_container" "$build_image" tail -f /dev/null
80+
docker exec -it "$build_container" sh -c "cd /tmp && go get -d && GOOS=linux GOARCH=amd64 go build"
81+
docker cp "$build_container:/tmp/tmp" "${DIR}/bin/ecs-discoverer-${new_version}-linux_musl_amd64"
82+
docker stop -t 1 "$build_container"
83+
docker rm "$build_container"
84+
docker rmi "$build_image"
85+
echo "$(date) : Build for Alpine Linux completed"
86+
87+
# Create git tag
88+
echo "$(date) : Tagging and pushing"
89+
git tag "${new_version}"
90+
git push --tags
91+
echo "$(date) : Tagged and pushed"
92+
93+
echo "$(date) : Generate SHA256 hashes of new binaries..."
94+
binary_sha256s="Notes go here.
95+
\`\`\`
96+
"
97+
for i in $target_platforms; do
98+
binary_suffix=${i/-/_}
99+
echo "$binary_suffix"
100+
binary_sha256s="${binary_sha256s}ecs-discoverer-${new_version}-${binary_suffix}
101+
$(shasum -a 256 "${DIR}/bin/ecs-discoverer-${new_version}-${binary_suffix}" | awk '{print $1}')
102+
103+
"
104+
done
105+
binary_sha256s="${binary_sha256s}\`\`\`"
106+
echo "$(date) : Hashes generated."
107+
108+
# Create GitHub release
109+
echo "$(date) : Creating release"
110+
github-release release --security-token "$github_token" --user CpuID --repo ecs-discoverer --tag "${new_version}" --name "ecs-discoverer ${new_version}" --description "${binary_sha256s}" --pre-release
111+
echo "$(date) : Release created"
112+
113+
# Zip up binaries
114+
echo "$(date) : Zip up binaries for upload to GitHub"
115+
cd "${DIR}/bin"
116+
for i in $target_platforms; do
117+
binary_suffix=${i/-/_}
118+
echo "$binary_suffix"
119+
zip "ecs-discoverer-${new_version}-${binary_suffix}.zip" "ecs-discoverer-${new_version}-${binary_suffix}"
120+
done
121+
cd "$DIR"
122+
echo "$(date) : Zip files of binaries created"
123+
124+
# Push binaries up to GitHub
125+
echo "$(date) : Pushing binaries (zipped) to GitHub release"
126+
for i in $target_platforms; do
127+
binary_suffix=${i/-/_}
128+
echo "$binary_suffix"
129+
# Was getting random 502's on these uploads in the past, add a sleep to see if it helps.
130+
sleep 1
131+
github-release upload --security-token "$github_token" --user CpuID --repo ecs-discoverer --tag "${new_version}" --name "ecs-discoverer-${new_version}-${binary_suffix}.zip" --file "${DIR}/bin/ecs-discoverer-${new_version}-${binary_suffix}.zip"
132+
done
133+
echo "$(date) : Binaries pushed to GitHub"

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func parseFlags(c *cli.Context) (bool, string, string, string, bool) {
339339
func main() {
340340
app := cli.NewApp()
341341
app.Name = "ecs-discoverer"
342-
app.Version = "0.3.2"
342+
app.Version = ecs_discoverer_version
343343
app.Usage = "Discovery tool for Private IPs of ECS EC2 Container Instances for a given Service/Cluster"
344344
app.Action = func(c *cli.Context) {
345345
current_cluster, aws_region, cluster, service, debug := parseFlags(c)

version.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
// NOTE: the script build_upload.sh manipulates this file with sed, ensure it still works
4+
// if this file is changed
5+
6+
const ecs_discoverer_version = "0.3.3"

0 commit comments

Comments
 (0)