-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·117 lines (103 loc) · 3.39 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash -e
set -euf -o pipefail
BINARY_NAME="${INPUT_binary_name}"
PACKAGE_NAME="${INPUT_package_name}"
COPY_FILES="${INPUT_copy_files_after_build}"
BUILD_COMMAND="${INPUT_build_command}"
DEPENDS=${INPUT_escape_go_dependencies}
VENDOR=${INPUT_go_vendor_path}
DOCKER_PACKAGE_PATH="/go/src/${PACKAGE_NAME}"
DOCKER_PACKAGE_PARENT_PATH=$(dirname "${DOCKER_PACKAGE_PATH}")
GOLANG_DOCKER_IMAGE=${INPUT_go_docker_image}
VOLUME_NAME="src$$"
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) OS=Linux;;
Darwin*) OS=Mac;;
CYGWIN*) OS=Cygwin;;
MINGW*) OS=MinGw;;
*) OS="UNKNOWN:${unameOut}"
esac
cleanup_docker() {
echo -n "Removing Docker data volume ${VOLUME_NAME}..."
docker rm -v ${VOLUME_NAME} 1>/dev/null 2>&1 || true
echo "OK"
}
copy_dep_to_vendor() {
local dep="deps/${1}"
local target="${VENDOR}/${2}"
echo -n "Copying Escape dependency '$dep' to '$target'..."
rm -rf "$target"
cp -r "$dep" "$target"
rm -rf "${target}/vendor/"
echo "OK"
echo -n "Deleting binaries bigger than 1M from ${target}..."
if [ "${OS}" = "Mac" ] ; then
find "${target}" -type f -perm +111 -size +1M -delete
else
find "${target}" -type f -executable -size +1M -delete
fi
echo "OK"
}
install_escape_go_deps() {
echo $DEPENDS | jq -r '.[]' | while read line ; do
local arrDepends=(${line//:/ })
copy_dep_to_vendor ${arrDepends[0]} ${arrDepends[1]}
done
}
realpath() {
path=`eval echo "$1"`
folder=$(dirname "$path")
echo $(cd "$folder"; pwd)/$(basename "$path");
}
prepare_volume() {
echo -n "Preparing Docker data volume..."
docker create -v "${DOCKER_PACKAGE_PARENT_PATH}" --name "${VOLUME_NAME}" "${GOLANG_DOCKER_IMAGE}" mkdir /code 1>/dev/null 2>&1
docker cp "$(realpath $PWD)" "${VOLUME_NAME}:${DOCKER_PACKAGE_PARENT_PATH}/tmp" 1>/dev/null
docker_run "${DOCKER_PACKAGE_PARENT_PATH}" "mv tmp ${DOCKER_PACKAGE_PATH}" 1>/dev/null
echo "OK"
}
docker_run() {
local cwd=$1
local cmd=$2
echo "Running '${cmd}' in Docker directory '${cwd}' (image ${GOLANG_DOCKER_IMAGE}):"
local dockerCmd="docker run --rm --volumes-from ${VOLUME_NAME} -w '$cwd'"
for var in $(env) ; do
if [[ $var == INPUT_* ]] || [[ $var == OUTPUT_* ]] || [[ $var == METADATA_* ]] ; then
arrIN=(${var//=/ })
if [ "${arrIN[1]+set}" == "set" ] ; then
dockerCmd="$dockerCmd -e \"${var}\""
fi
fi
done
cmd=${cmd//\'/'"'"'"'"'}
dockerCmd="$dockerCmd ${GOLANG_DOCKER_IMAGE} bash -c '$cmd'"
eval $dockerCmd
}
copy_binary_out_of_volume() {
local binary_name=$1
if [ "$binary_name" = "" ] ; then
return
fi
docker cp "${VOLUME_NAME}:${DOCKER_PACKAGE_PATH}/${binary_name}" "${binary_name}"
}
copy_files_out_of_volume() {
echo $COPY_FILES | jq -r '.[]' | while read line ; do
local arrDepends=(${line//:/ })
docker cp "${VOLUME_NAME}:${DOCKER_PACKAGE_PATH}/${arrDepends[0]}" "${arrDepends[1]}"
done
}
main() {
install_escape_go_deps
if [ "${BINARY_NAME}" = "" ] && [ "${BUILD_COMMAND}" = "" ] ; then
echo "No binary name specified. Skipping build."
exit 0
fi
cleanup_docker
prepare_volume
docker_run "${DOCKER_PACKAGE_PATH}" "${BUILD_COMMAND:-"go build -v"}"
copy_binary_out_of_volume "${BINARY_NAME}"
copy_files_out_of_volume
}
trap cleanup_docker EXIT
main