forked from GeoNet/fdsn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·90 lines (67 loc) · 3.5 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash -e
# Builds Docker images for the arg list. These must be project directories
# where this script is executed.
#
# Builds a statically linked executable and adds it to the container.
# Adds the assets dir from each project to the container e.g., origin/assets
# It is not an error for the assets dir to not exist.
# Any assets needed by the application should be read from the assets dir
# relative to the executable.
#
# usage: ./build.sh project [project]
if [ $# -eq 0 ]; then
echo Error: please supply a project to build. Usage: ./build.sh project [project]
exit 1
fi
# code will be compiled in this container
BUILD_CONTAINER=golang:1.12.1-alpine
# dependent resources will be gathered from this container
RESOURCE_CONTAINER=alpine:3.9
DOCKER_TMP=docker-build-tmp
mkdir -p $DOCKER_TMP
chmod +s $DOCKER_TMP
sudo rm -rf $DOCKER_TMP/*
VERSION='git-'`git rev-parse --short HEAD`
# The current working dir to use in GOBIN etc e.g., geonet-web
CWD=${PWD##*/}
mkdir -p ${DOCKER_TMP}/etc/ssl/certs
mkdir -p ${DOCKER_TMP}/usr/share
# Assemble common resource for ssl and timezones from the build container
docker run --rm -v ${PWD}:${PWD} ${RESOURCE_CONTAINER} \
/bin/ash -c "apk add --update ca-certificates tzdata && \
cp /etc/ssl/certs/ca-certificates.crt ${PWD}/${DOCKER_TMP}/etc/ssl/certs && \
cp -Ra /usr/share/zoneinfo ${PWD}/${DOCKER_TMP}/usr/share"
# Assemble common resource for user.
echo "nobody:x:65534:65534:Nobody:/:" > ${DOCKER_TMP}/etc/passwd
for i in "$@"
do
# only enable Cgo for executables that require it
enable_cgo=0
if [ ${i} = "fdsn-ws" ] || [ ${i} = "fdsn-holdings-consumer" ] || [ ${i} = "fdsn-slink-db" ]; then
enable_cgo=1
fi
# install dependencies to compile libmseed and libslink, and compile/install with the -static flag to statically link with C libs (if applicable)
docker run -e "GOBIN=/usr/src/go/src/github.com/GeoNet/${CWD}/${DOCKER_TMP}" -e "CGO_ENABLED=${enable_cgo}" -e "GOPATH=/usr/src/go" -e "GOOS=linux" -e "BUILD=$BUILD" --rm \
-v "$PWD":/usr/src/go/src/github.com/GeoNet/${CWD} \
-w /usr/src/go/src/github.com/GeoNet/${CWD} ${BUILD_CONTAINER} \
/bin/ash -c "apk add --update ca-certificates tzdata gcc make musl-dev && \
make -B -C /usr/src/go/src/github.com/GeoNet/${CWD}/vendor/github.com/GeoNet/kit/cvendor/libmseed && \
make -B -C /usr/src/go/src/github.com/GeoNet/${CWD}/vendor/github.com/GeoNet/kit/cvendor/libslink && \
go install -a -ldflags \"-X main.Prefix=${i}/${VERSION} -extldflags -static\" -installsuffix cgo ./cmd/${i}"
rm -rf $DOCKER_TMP/assets
mkdir $DOCKER_TMP/assets
rsync --archive --quiet --ignore-missing-args cmd/${i}/assets docker-build-tmp/
# Add a default Dockerfile
rm -f $DOCKER_TMP/Dockerfile
echo "FROM scratch" > docker-build-tmp/Dockerfile
echo "ADD ./ /" >> docker-build-tmp/Dockerfile
echo "USER nobody" >> docker-build-tmp/Dockerfile
echo "EXPOSE 8080" >> docker-build-tmp/Dockerfile
echo "CMD [\"/${i}\"]" >> docker-build-tmp/Dockerfile
# If a project specifies a Dockerfile then copy it over the top of the default file.
rsync --ignore-missing-args cmd/${i}/Dockerfile docker-build-tmp/
docker build -t 862640294325.dkr.ecr.ap-southeast-2.amazonaws.com/${i}:$VERSION -f docker-build-tmp/Dockerfile docker-build-tmp
# tag latest. Makes it easier to test with compose.
docker tag 862640294325.dkr.ecr.ap-southeast-2.amazonaws.com/${i}:$VERSION 862640294325.dkr.ecr.ap-southeast-2.amazonaws.com/${i}:latest
rm -f $DOCKER_TMP/$i
done