Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit 015e0a3

Browse files
author
Takashi Matsuo
authored
Use gcr to cache the docker images (#7)
* Use gcr to cache the docker images * Remove sudo from docker command
1 parent d6e90be commit 015e0a3

File tree

3 files changed

+64
-76
lines changed

3 files changed

+64
-76
lines changed

ci/kokoro/create-docker-image.sh

Lines changed: 0 additions & 35 deletions
This file was deleted.

ci/kokoro/define-docker-variables.sh

Lines changed: 0 additions & 24 deletions
This file was deleted.

ci/kokoro/docker/build.sh

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,82 @@ fi
8686
if [[ -z "${PROJECT_ROOT+x}" ]]; then
8787
readonly PROJECT_ROOT="$(cd "$(dirname "$0")/../../.."; pwd)"
8888
fi
89-
source "${PROJECT_ROOT}/ci/kokoro/define-docker-variables.sh"
89+
90+
if [[ -z "${PROJECT_ID+x}" ]]; then
91+
readonly PROJECT_ID="cloud-devrel-kokoro-resources"
92+
fi
93+
94+
# Determine the image name.
95+
readonly IMAGE="gcr.io/${PROJECT_ID}/cpp-cmakefiles/${DISTRO}-${DISTRO_VERSION}"
96+
readonly BUILD_OUTPUT="cmake-out/${BUILD_NAME}"
97+
readonly BUILD_HOME="cmake-out/home/${BUILD_NAME}"
9098

9199
echo "================================================================"
92100
cd "${PROJECT_ROOT}"
93101
echo "Building with $(nproc) cores $(date) on ${PWD}."
94102

95103
echo "================================================================"
96104
echo "Capture Docker version to troubleshoot $(date)."
97-
sudo docker version
105+
docker version
98106
echo "================================================================"
99107

108+
has_cache="false"
109+
110+
if [[ -n "${KOKORO_JOB_NAME:-}" ]]; then
111+
# Download the docker image from the previous build on kokoro for speed.
112+
echo "================================================================"
113+
echo "Downloading Docker image $(date)."
114+
gcloud auth configure-docker
115+
if docker pull "${IMAGE}:latest"; then
116+
echo "Existing image successfully downloaded."
117+
has_cache="true"
118+
fi
119+
echo "================================================================"
120+
fi
121+
122+
docker_build_flags=(
123+
"-t" "${IMAGE}:latest"
124+
)
125+
126+
if [[ -f "ci/kokoro/Dockerfile.${DISTRO}-${DISTRO_VERSION}" ]]; then
127+
docker_build_flags+=("-f" "ci/kokoro/Dockerfile.${DISTRO}-${DISTRO_VERSION}")
128+
else
129+
docker_build_flags+=(
130+
"-f" "ci/kokoro/Dockerfile.${DISTRO}"
131+
"--build-arg" "DISTRO_VERSION=${DISTRO_VERSION}"
132+
)
133+
fi
134+
135+
if "${has_cache}"; then
136+
docker_build_flags+=("--cache-from=${IMAGE}:latest")
137+
fi
138+
139+
update_cache="false"
100140
echo "================================================================"
101141
echo "Creating Docker image with all the development tools $(date)."
102-
# We do not want to print the log unless there is an error, so disable the -e
103-
# flag. Later, we will want to print out the emulator(s) logs *only* if there
104-
# is an error, so disabling from this point on is the right choice.
105-
set +e
106-
mkdir -p "${BUILD_OUTPUT}"
107-
readonly CREATE_DOCKER_IMAGE_LOG="${BUILD_OUTPUT}/create-build-docker-image.log"
108-
echo "Logging to ${CREATE_DOCKER_IMAGE_LOG}"
109-
if ! "${PROJECT_ROOT}/ci/retry-command.sh" \
110-
"${PROJECT_ROOT}/ci/kokoro/create-docker-image.sh" \
111-
>"${CREATE_DOCKER_IMAGE_LOG}" 2>&1 </dev/null; then
112-
cat "${CREATE_DOCKER_IMAGE_LOG}"
113-
exit 1
142+
if ci/retry-command.sh docker build "${docker_build_flags[@]}" ci; then
143+
update_cache="true"
144+
echo "Docker image created $(date)."
145+
docker image ls | grep "${IMAGE}"
146+
else
147+
echo "Failed creating Docker image $(date)."
148+
if "${has_cache}"; then
149+
echo "Continue the build with the cache."
150+
else
151+
exit 1
152+
fi
114153
fi
115-
echo "Docker image created $(date)."
116-
sudo docker image ls
117154
echo "================================================================"
118155

156+
if [[ -n "${KOKORO_JOB_NAME:-}" ]]; then
157+
# Upload the docker image for speeding up the future builds.
158+
echo "================================================================"
159+
echo "Uploading Docker image $(date)."
160+
docker push "${IMAGE}:latest" || true
161+
echo "================================================================"
162+
fi
163+
164+
119165
echo "================================================================"
120166
echo "Running the full build $(date)."
121167
# The default user for a Docker container has uid 0 (root). To avoid creating
@@ -130,6 +176,7 @@ fi
130176

131177
# Make sure the user has a $HOME directory inside the Docker container.
132178
mkdir -p "${BUILD_HOME}"
179+
mkdir -p "${BUILD_OUTPUT}"
133180

134181
# We use an array for the flags so they are easier to document.
135182
docker_flags=(
@@ -213,6 +260,6 @@ if [[ -t 0 ]]; then
213260
docker_flags+=("-it")
214261
fi
215262

216-
sudo docker run "${docker_flags[@]}" "${IMAGE}:tip" \
263+
docker run "${docker_flags[@]}" "${IMAGE}:latest" \
217264
"/v/${in_docker_script}" "${CMAKE_SOURCE_DIR}" \
218265
"${BUILD_OUTPUT}"

0 commit comments

Comments
 (0)