-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerbuild.sh
executable file
·84 lines (73 loc) · 2.88 KB
/
dockerbuild.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
#!/usr/bin/env bash
#
# dockerbuild.sh
#
# Build the software inside a Docker container
#
# @author Nicola Asuni <[email protected]>
# ------------------------------------------------------------------------------
set -e -u +x
# NOTES:
# This script requires Docker
# EXAMPLE USAGE:
# CVSPATH=project VENDOR=vendorname PROJECT=projectname MAKETARGET=buildall ./dockerbuild.sh
# Variables (parameters).
: ${CVSPATH:=project}
: ${VENDOR:=vendor}
: ${PROJECT:=project}
: ${DOCKERTAG:=dev}
: ${MAKETARGET:=format clean ensuretarget mod deps gendoc generate qa build}
: ${SSH_PRIVATE_KEY:=$(cat ~/.ssh/id_rsa || cat ~/.ssh/id_ed25519)}
: ${SSH_PUBLIC_KEY:=$(cat ~/.ssh/id_rsa.pub || cat ~/.ssh/id_ed25519.pub)}
: ${DOCKER:=$(which docker)}
: ${DOCKERDEV:=${VENDOR}/dev_${PROJECT}:${DOCKERTAG}}
# Build the base environment and keep it cached locally.
${DOCKER} build --pull --tag ${DOCKERDEV} --file ./resources/docker/Dockerfile.dev ./resources/docker/
# Define the project root path.
PRJPATH=/root/src/${CVSPATH}/${PROJECT}
# Generate a temporary Dockerfile to build and test the project
# NOTE: The exit status of the RUN command is stored to be returned later,
# so in case of error we can continue without interrupting this script.
cat > Dockerfile.test <<- EOM
FROM ${DOCKERDEV}
ARG SSH_PRIVATE_KEY=""
ARG SSH_PUBLIC_KEY=""
RUN \\
mkdir -p /root/.ssh \\
&& echo "\${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa \\
&& echo "\${SSH_PUBLIC_KEY}" > /root/.ssh/id_rsa.pub \\
&& echo "Host *" >> /root/.ssh/config \\
&& echo " StrictHostKeyChecking no" >> /root/.ssh/config \\
&& echo " GlobalKnownHostsFile /dev/null" >> /root/.ssh/config \\
&& echo " UserKnownHostsFile /dev/null" >> /root/.ssh/config \\
&& chmod 600 /root/.ssh/id_rsa \\
&& chmod 644 /root/.ssh/id_rsa.pub \\
&& echo "[user]" >> /root/.gitconfig \\
&& echo " email = [email protected]" >> /root/.gitconfig \\
&& echo " name = godevlocaltestuser" >> /root/.gitconfig \\
&& echo "[url \"ssh://git@${CVSPATH}\"]" >> /root/.gitconfig \\
&& echo " insteadOf = https://${CVSPATH}" >> /root/.gitconfig \\
&& mkdir -p ${PRJPATH}
COPY ./ ${PRJPATH}
WORKDIR ${PRJPATH}
RUN make ${MAKETARGET} || (echo \$? > target/make.exit)
HEALTHCHECK CMD go version || exit 1
EOM
# Define the temporary Docker image name.
DOCKER_IMAGE_NAME=${VENDOR}/build_${PROJECT}:${DOCKERTAG}
# Build the Docker image.
BUILDKIT_PROGRESS=plain \
${DOCKER} build \
--no-cache \
--build-arg SSH_PRIVATE_KEY="${SSH_PRIVATE_KEY}" \
--build-arg SSH_PUBLIC_KEY="${SSH_PUBLIC_KEY}" \
--tag ${DOCKER_IMAGE_NAME} \
--file Dockerfile.test .
# Start a container using the newly created Docker image.
CONTAINER_ID=$(docker run -d ${DOCKER_IMAGE_NAME})
# Copy all build/test artifacts back to the host.
${DOCKER} cp ${CONTAINER_ID}:"${PRJPATH}/target" ./
# Remove the temporary container and image.
rm -f Dockerfile.test
${DOCKER} rm -f ${CONTAINER_ID} || true
${DOCKER} rmi -f ${DOCKER_IMAGE_NAME} || true