Commit fe52e0eb authored by Anoop Mazhavancheri's avatar Anoop Mazhavancheri Committed by Pierre Smeyers
Browse files

feat: build cache can be disabled (configurable; non-default)

parent 482db124
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -283,6 +283,7 @@ It is bound to the `package-build` stage, and uses the following variables:
| `CONTAINER_REGISTRIES_CONFIG_FILE` | The [`registries.conf`](https://www.redhat.com/sysadmin/manage-container-registries) configuration to be used<br>:warning: Used by the `buildah` build only             | _(none)_                       |
| `DOCKER_METADATA`                  | Additional `docker build`/`kaniko` arguments to set label                                                                                                               | OCI Image Format Specification |
| `KANIKO_SNAPSHOT_IMAGE_CACHE`      | Snapshot image repository that will be used to store cached layers<br>:warning: Used by the `kaniko` build only                                                         | `${DOCKER_SNAPSHOT_IMAGE%:*}/cache` |
| `DOCKER_BUILD_CACHE_DISABLED`      | Set to `true` to disable the build cache.<br/>Cache can typically be disabled when there is a network latency between the container registry and the runner. | _none_ (i.e cache enabled) |

This job produces _output variables_ that are propagated to downstream jobs (using [dotenv artifacts](https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#artifactsreportsdotenv)):

+6 −0
Original line number Diff line number Diff line
@@ -113,6 +113,12 @@
      "description": "Snapshot image repository that will be used to store cached layers.\n\n_Used by the `kaniko` build only_",
      "default": "${DOCKER_SNAPSHOT_IMAGE%:*}/cache",
      "advanced": true
    },
    {
      "name": "DOCKER_BUILD_CACHE_DISABLED",
      "description": "Set to `true` to disable the build cache.",
      "type": "boolean",
      "advanced": true
    }
  ],
  "features": [
+21 −8
Original line number Diff line number Diff line
@@ -426,7 +426,11 @@ stages:

  function run_build_kaniko() {
    docker_image=$1
    if [ "$DOCKER_BUILD_CACHE_DISABLED" != "true" ]; then
      kaniko_snapshot_image_cache="${KANIKO_SNAPSHOT_IMAGE_CACHE:-${DOCKER_SNAPSHOT_IMAGE%:*}/cache}"
      kaniko_cache_args="--cache --cache-dir=${CI_PROJECT_DIR}/.cache --cache-repo=${kaniko_snapshot_image_cache}"
      log_info "Build cache enabled; CLI options: ${kaniko_cache_args}"
    fi
    shift
    if [[ -n "$DOCKER_REGISTRY_MIRROR" ]]
    then
@@ -434,9 +438,9 @@ stages:
      kaniko_registry_mirror_option="--registry-mirror $(echo ${DOCKER_REGISTRY_MIRROR} | sed "s|^https*://||")"
    fi
    log_info "Build & deploy image $docker_image"
    log_info "Kaniko command: /kaniko/executor --context $(docker_context_path) --dockerfile $DOCKER_FILE --destination $docker_image --cache --cache-dir=${CI_PROJECT_DIR}/.cache --cache-repo=${kaniko_snapshot_image_cache} --verbosity $DOCKER_KANIKO_VERBOSITY $kaniko_registry_mirror_option $DOCKER_METADATA $DOCKER_BUILD_ARGS $*"
    log_info "Kaniko command: /kaniko/executor --context $(docker_context_path) --dockerfile $DOCKER_FILE --destination $docker_image ${kaniko_cache_args} --verbosity $DOCKER_KANIKO_VERBOSITY $kaniko_registry_mirror_option $DOCKER_METADATA $DOCKER_BUILD_ARGS $*"
    # shellcheck disable=SC2086
    /kaniko/executor --context "$(docker_context_path)" --dockerfile "$DOCKER_FILE" --destination "$docker_image" --cache --cache-dir="${CI_PROJECT_DIR}/.cache" --cache-repo="${kaniko_snapshot_image_cache}" --verbosity $DOCKER_KANIKO_VERBOSITY $kaniko_registry_mirror_option $DOCKER_METADATA $DOCKER_BUILD_ARGS "$@"
    /kaniko/executor --context "$(docker_context_path)" --dockerfile "$DOCKER_FILE" --destination "$docker_image" ${kaniko_cache_args} --verbosity $DOCKER_KANIKO_VERBOSITY $kaniko_registry_mirror_option $DOCKER_METADATA $DOCKER_BUILD_ARGS "$@"
  }

  # Used by containers tools like buildah, skopeo.
@@ -600,8 +604,13 @@ docker-dind-build:
  stage: package-build
  script:
    - docker pull $DOCKER_SNAPSHOT_IMAGE || true
    - |
      if [ "$DOCKER_BUILD_CACHE_DISABLED" != "true" ]; then
        dind_cache_args="--cache-from $DOCKER_SNAPSHOT_IMAGE"
        log_info "Build cache enabled; CLI options: ${dind_cache_args}"
      fi
    # Build using cache if exist
    - docker build --file "$DOCKER_FILE" --cache-from $DOCKER_SNAPSHOT_IMAGE --tag $DOCKER_SNAPSHOT_IMAGE --build-arg http_proxy="$http_proxy" --build-arg https_proxy="$https_proxy" --build-arg no_proxy="$no_proxy" $DOCKER_METADATA $DOCKER_BUILD_ARGS "$(docker_context_path)"
    - docker build --file "$DOCKER_FILE" ${dind_cache_args} --tag $DOCKER_SNAPSHOT_IMAGE --build-arg http_proxy="$http_proxy" --build-arg https_proxy="$https_proxy" --build-arg no_proxy="$no_proxy" $DOCKER_METADATA $DOCKER_BUILD_ARGS "$(docker_context_path)"
    - docker push $DOCKER_SNAPSHOT_IMAGE
    # Display the size of each layer
    - docker history $DOCKER_SNAPSHOT_IMAGE
@@ -633,11 +642,15 @@ docker-buildah-build:
  image: "$DOCKER_BUILDAH_IMAGE"
  script:
    - configure_containers_registries
    # derive buildah_build_cache repository
    - buildah_build_cache="${DOCKER_SNAPSHOT_IMAGE%:*}/cache"
    - log_info "Using ${buildah_build_cache} as build cache repository"
    # Add build cache related parameters.
    - |
      if [ "$DOCKER_BUILD_CACHE_DISABLED" != "true" ]; then
          buildah_build_cache="${DOCKER_SNAPSHOT_IMAGE%:*}/cache"
          buildah_cache_args="--layers --cache-from $buildah_build_cache --cache-to $buildah_build_cache"
          log_info "Build cache enabled; CLI options: ${buildah_cache_args}"
      fi
    # build and push image
    - buildah build --file "$DOCKER_FILE" --tag $DOCKER_SNAPSHOT_IMAGE --layers --cache-from $buildah_build_cache --cache-to $buildah_build_cache --build-arg http_proxy="$http_proxy" --build-arg https_proxy="$https_proxy" --build-arg no_proxy="$no_proxy" $DOCKER_METADATA $DOCKER_BUILD_ARGS "$(docker_context_path)"
    - buildah build --file "$DOCKER_FILE" --tag $DOCKER_SNAPSHOT_IMAGE $buildah_cache_args --build-arg http_proxy="$http_proxy" --build-arg https_proxy="$https_proxy" --build-arg no_proxy="$no_proxy" $DOCKER_METADATA $DOCKER_BUILD_ARGS "$(docker_context_path)"
    - buildah push --digestfile .img-digest.txt "$DOCKER_SNAPSHOT_IMAGE"
    # display digest of the resulting image
    - cat .img-digest.txt