|
86 | 86 | if [[ -z "${PROJECT_ROOT+x}" ]]; then |
87 | 87 | readonly PROJECT_ROOT="$(cd "$(dirname "$0")/../../.."; pwd)" |
88 | 88 | 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}" |
90 | 98 |
|
91 | 99 | echo "================================================================" |
92 | 100 | cd "${PROJECT_ROOT}" |
93 | 101 | echo "Building with $(nproc) cores $(date) on ${PWD}." |
94 | 102 |
|
95 | 103 | echo "================================================================" |
96 | 104 | echo "Capture Docker version to troubleshoot $(date)." |
97 | | -sudo docker version |
| 105 | +docker version |
98 | 106 | echo "================================================================" |
99 | 107 |
|
| 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" |
100 | 140 | echo "================================================================" |
101 | 141 | 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 |
114 | 153 | fi |
115 | | -echo "Docker image created $(date)." |
116 | | -sudo docker image ls |
117 | 154 | echo "================================================================" |
118 | 155 |
|
| 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 | + |
119 | 165 | echo "================================================================" |
120 | 166 | echo "Running the full build $(date)." |
121 | 167 | # The default user for a Docker container has uid 0 (root). To avoid creating |
|
130 | 176 |
|
131 | 177 | # Make sure the user has a $HOME directory inside the Docker container. |
132 | 178 | mkdir -p "${BUILD_HOME}" |
| 179 | +mkdir -p "${BUILD_OUTPUT}" |
133 | 180 |
|
134 | 181 | # We use an array for the flags so they are easier to document. |
135 | 182 | docker_flags=( |
@@ -213,6 +260,6 @@ if [[ -t 0 ]]; then |
213 | 260 | docker_flags+=("-it") |
214 | 261 | fi |
215 | 262 |
|
216 | | -sudo docker run "${docker_flags[@]}" "${IMAGE}:tip" \ |
| 263 | +docker run "${docker_flags[@]}" "${IMAGE}:latest" \ |
217 | 264 | "/v/${in_docker_script}" "${CMAKE_SOURCE_DIR}" \ |
218 | 265 | "${BUILD_OUTPUT}" |
0 commit comments