#!/busybox/sh set -euo pipefail set -f export PATH=$PATH:/kaniko/ NL=' ' REGISTRY=${PLUGIN_REGISTRY:-index.docker.io} # strip leading/trailing spaces from a single list item trim() { t=$1 while [ "${t# }" != "$t" ]; do t=${t# }; done while [ "${t% }" != "$t" ]; do t=${t% }; done printf '%s' "$t" } if [ -n "${PLUGIN_USERNAME:-}" ] || [ -n "${PLUGIN_PASSWORD:-}" ]; then if [ -z "${PLUGIN_USERNAME:-}" ] || [ -z "${PLUGIN_PASSWORD:-}" ]; then echo "error: username and password must both be set (missing Drone secret?)" >&2 exit 1 fi DOCKER_AUTH=$(printf '%s' "${PLUGIN_USERNAME}:${PLUGIN_PASSWORD}" | base64 | tr -d "\n") # kaniko resolves Docker Hub credentials under the legacy key # https://index.docker.io/v1/, so write it alongside the plain hostname AUTHS="\"${REGISTRY}\": {\"auth\": \"${DOCKER_AUTH}\"}" if [ "${REGISTRY}" = "index.docker.io" ]; then AUTHS="${AUTHS}, \"https://index.docker.io/v1/\": {\"auth\": \"${DOCKER_AUTH}\"}" fi cat > /kaniko/.docker/config.json < /kaniko/gcr.json export GOOGLE_APPLICATION_CREDENTIALS=/kaniko/gcr.json fi DOCKERFILE=${PLUGIN_DOCKERFILE:-Dockerfile} CONTEXT=${PLUGIN_CONTEXT:-$PWD} LOG=${PLUGIN_LOG:-info} # kaniko arguments accumulate in "$@" so that values containing spaces # reach the executor as single arguments set -- -v "${LOG}" --context="${CONTEXT}" --dockerfile="${DOCKERFILE}" if [ -n "${PLUGIN_TARGET:-}" ]; then set -- "$@" --target="${PLUGIN_TARGET}" fi if [ "${PLUGIN_SKIP_TLS_VERIFY:-}" = "true" ]; then set -- "$@" --skip-tls-verify=true fi if [ "${PLUGIN_SKIP_UNUSED_STAGES:-}" = "true" ]; then set -- "$@" --skip-unused-stages=true fi # snapshot_mode: full (default, most robust), redo (up to 50% faster: # mtime, size, mode, uid, gid), time (fastest: mtime only) if [ -n "${PLUGIN_SNAPSHOT_MODE:-}" ]; then case "${PLUGIN_SNAPSHOT_MODE}" in full|redo|time) set -- "$@" --snapshot-mode="${PLUGIN_SNAPSHOT_MODE}" ;; *) echo "error: snapshot_mode must be one of: full, redo, time (got '${PLUGIN_SNAPSHOT_MODE}')" >&2 exit 1 ;; esac fi if [ -n "${PLUGIN_BUILD_ARGS:-}" ]; then IFS=",${NL}" for build_arg in ${PLUGIN_BUILD_ARGS}; do build_arg=$(trim "${build_arg}") if [ -n "${build_arg}" ]; then set -- "$@" --build-arg="${build_arg}" fi done unset IFS fi if [ -n "${PLUGIN_BUILD_ARGS_FROM_ENV:-}" ]; then IFS=",${NL}" for env_name in ${PLUGIN_BUILD_ARGS_FROM_ENV}; do env_name=$(trim "${env_name}") if [ -z "${env_name}" ]; then continue fi case "${env_name}" in [0-9]*|*[!A-Za-z0-9_]*) echo "warning: build_args_from_env: '${env_name}' is not a valid variable name, skipping" >&2 continue ;; esac if eval "[ -n \"\${${env_name}+set}\" ]"; then eval "env_value=\${${env_name}}" set -- "$@" --build-arg="${env_name}=${env_value}" else echo "warning: build_args_from_env: \$${env_name} is not set, skipping" >&2 fi done unset IFS fi # auto_tag, if set auto_tag: true, auto generate .tags file # support format Major.Minor.Release or start with `v` # docker tags: Major, Major.Minor, Major.Minor.Release and latest if [ "${PLUGIN_AUTO_TAG:-}" = "true" ]; then TAG=${DRONE_TAG:-} TAG=${TAG#v} case "${TAG}" in '') echo "latest" > .tags ;; *[!0-9.]*|*.*.*.*|.*|*.|*..*) # not a plain Major.Minor.Release version: use the tag as-is echo "${TAG},latest" > .tags ;; *) major=${TAG%%.*} rest=${TAG#"${major}"}; rest=${rest#.} minor=${rest%%.*} rest=${rest#"${minor}"}; rest=${rest#.} release=${rest} minor=${minor:-0} release=${release:-0} echo "${major},${major}.${minor},${major}.${minor}.${release},latest" > .tags ;; esac fi TAGS="" if [ -n "${PLUGIN_TAGS:-}" ]; then TAGS=${PLUGIN_TAGS} elif [ -f .tags ]; then TAGS=$(cat .tags) fi if [ -n "${PLUGIN_REPO:-}" ]; then if [ -n "${TAGS}" ]; then IFS=",${NL}" for tag in ${TAGS}; do tag=$(trim "${tag}") if [ -n "${tag}" ]; then set -- "$@" --destination="${REGISTRY}/${PLUGIN_REPO}:${tag}" fi done unset IFS else set -- "$@" --destination="${REGISTRY}/${PLUGIN_REPO}:latest" fi if [ "${PLUGIN_CACHE:-}" = "true" ]; then set -- "$@" --cache=true fi if [ -n "${PLUGIN_CACHE_REPO:-}" ]; then set -- "$@" --cache-repo="${REGISTRY}/${PLUGIN_CACHE_REPO}" fi if [ -n "${PLUGIN_CACHE_TTL:-}" ]; then set -- "$@" --cache-ttl="${PLUGIN_CACHE_TTL}" fi else if [ -n "${TAGS}" ]; then echo "warning: tags are set but repo is not, building with --no-push" >&2 fi # Cache is not valid with --no-push set -- "$@" --no-push fi exec /kaniko/executor "$@"