cfg/bin/gen-dockerfile-1.6
Knut Ahlers 93857029b0
Move from deprecated MAINTAINER instruction
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2017-03-28 09:45:08 +02:00

77 lines
1.5 KiB
Bash
Executable file

#!/bin/bash
PWD=$(pwd)
PACKAGE=${PWD/${GOPATH}\/src\//}
BINARY=$(basename ${PACKAGE})
GIT_NAME=$(git config --get user.name)
GIT_MAIL=$(git config --get user.email)
if [ -e "${PWD}/Godeps/_workspace/src" ]; then
OVERRIDE_GOPATH="/go:/go/src/${PACKAGE}/Godeps/_workspace"
fi
EXPOSE="EXPOSE"
while getopts ":e:t:v:" opt; do
case $opt in
e)
EXPOSE="${EXPOSE} ${OPTARG}"
;;
t)
TIMEZONE=${OPTARG}
;;
v)
VOLUME="${VOLUME}, \"${OPTARG}\""
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Header
cat <<EOF
FROM golang:alpine
LABEL maintainer "${GIT_NAME} <${GIT_MAIL}>"
EOF
# Support old (pre-1.6) Godep style
[ -z "${OVERRIDE_GOPATH}" ] || echo -e "ENV GOPATH ${OVERRIDE_GOPATH}\n"
# Allow setting timezone using `-t Europe/Berlin`
[ -z "${TIMEZONE}" ] || cat <<EOF
RUN set -ex \\
&& apk add --update tzdata \\
&& cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \\
&& echo "${TIMEZONE}" > /etc/timezone \\
&& apk del --purge tzdata
EOF
# Build binary
cat <<EOF
ADD . /go/src/${PACKAGE}
WORKDIR /go/src/${PACKAGE}
RUN set -ex \\
&& apk add --update git ca-certificates \\
&& go install -ldflags "-X main.version=\$(git describe --tags || git rev-parse --short HEAD || echo dev)" \\
&& apk del --purge git
EOF
# Allow exposing ports using `-e 3000`
( test "EXPOSE" != "${EXPOSE}" ) && echo -e "${EXPOSE}\n"
# Allow
[ -z "${VOLUME}" ] || echo -e "VOLUME [${VOLUME/, /}]\n"
# Execution information
cat <<EOF
ENTRYPOINT ["/go/bin/${BINARY}"]
CMD ["--"]
EOF