2020-02-15 18:55:26 +01:00
|
|
|
#!/busybox/sh
|
|
|
|
set -e pipefail
|
2022-03-25 15:09:12 +01:00
|
|
|
if [[ "$INPUT_DEBUG" == "true" ]]; then
|
|
|
|
set -o xtrace
|
|
|
|
fi
|
2020-02-15 18:55:26 +01:00
|
|
|
|
|
|
|
export REGISTRY=${INPUT_REGISTRY:-"docker.io"}
|
|
|
|
export IMAGE=${INPUT_IMAGE}
|
2020-02-17 00:11:52 +01:00
|
|
|
export BRANCH=$(echo ${GITHUB_REF} | sed -E "s/refs\/(heads|tags)\///g" | sed -e "s/\//-/g")
|
2020-02-15 18:55:26 +01:00
|
|
|
export TAG=${INPUT_TAG:-$([ "$BRANCH" == "master" ] && echo latest || echo $BRANCH)}
|
|
|
|
export TAG=${TAG:-"latest"}
|
2020-02-24 22:11:13 +01:00
|
|
|
export TAG=${TAG#$INPUT_STRIP_TAG_PREFIX}
|
2020-02-15 18:55:26 +01:00
|
|
|
export USERNAME=${INPUT_USERNAME:-$GITHUB_ACTOR}
|
|
|
|
export PASSWORD=${INPUT_PASSWORD:-$GITHUB_TOKEN}
|
2020-06-20 19:07:45 +02:00
|
|
|
export REPOSITORY=$IMAGE
|
2020-02-15 18:55:26 +01:00
|
|
|
export IMAGE=$IMAGE:$TAG
|
2020-06-20 08:41:59 +02:00
|
|
|
export CONTEXT_PATH=${INPUT_PATH}
|
2020-02-15 18:55:26 +01:00
|
|
|
|
2022-01-17 02:38:57 +01:00
|
|
|
if [[ "$INPUT_TAG_WITH_LATEST" == "true" ]]; then
|
2022-03-25 14:46:48 +01:00
|
|
|
export IMAGE_LATEST="$REPOSITORY:latest"
|
2022-01-17 02:38:57 +01:00
|
|
|
fi
|
|
|
|
|
2020-06-19 13:14:45 +02:00
|
|
|
function ensure() {
|
2020-02-15 18:55:26 +01:00
|
|
|
if [ -z "${1}" ]; then
|
2020-06-19 13:14:45 +02:00
|
|
|
echo >&2 "Unable to find the ${2} variable. Did you set with.${2}?"
|
2020-02-15 18:55:26 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-06-19 13:14:45 +02:00
|
|
|
ensure "${REGISTRY}" "registry"
|
|
|
|
ensure "${USERNAME}" "username"
|
|
|
|
ensure "${PASSWORD}" "password"
|
|
|
|
ensure "${IMAGE}" "image"
|
|
|
|
ensure "${TAG}" "tag"
|
2020-06-20 08:41:59 +02:00
|
|
|
ensure "${CONTEXT_PATH}" "path"
|
2020-02-15 18:55:26 +01:00
|
|
|
|
2022-02-21 15:19:19 +01:00
|
|
|
if [ "$REGISTRY" == "ghcr.io" ]; then
|
2020-04-07 11:39:12 +02:00
|
|
|
IMAGE_NAMESPACE="$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')"
|
|
|
|
export IMAGE="$IMAGE_NAMESPACE/$IMAGE"
|
2020-06-20 19:07:45 +02:00
|
|
|
export REPOSITORY="$IMAGE_NAMESPACE/$REPOSITORY"
|
2020-02-15 18:55:26 +01:00
|
|
|
|
2020-06-20 13:37:54 +02:00
|
|
|
if [ ! -z $IMAGE_LATEST ]; then
|
|
|
|
export IMAGE_LATEST="$IMAGE_NAMESPACE/$IMAGE_LATEST"
|
|
|
|
fi
|
|
|
|
|
2020-02-16 22:52:06 +01:00
|
|
|
if [ ! -z $INPUT_CACHE_REGISTRY ]; then
|
2020-04-07 11:39:12 +02:00
|
|
|
export INPUT_CACHE_REGISTRY="$REGISTRY/$IMAGE_NAMESPACE/$INPUT_CACHE_REGISTRY"
|
2020-02-15 18:55:26 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$REGISTRY" == "docker.io" ]; then
|
|
|
|
export REGISTRY="index.${REGISTRY}/v1/"
|
|
|
|
else
|
|
|
|
export IMAGE="$REGISTRY/$IMAGE"
|
2020-06-20 13:37:54 +02:00
|
|
|
|
|
|
|
if [ ! -z $IMAGE_LATEST ]; then
|
|
|
|
export IMAGE_LATEST="$REGISTRY/$IMAGE_LATEST"
|
|
|
|
fi
|
2020-02-15 18:55:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
export CACHE=${INPUT_CACHE:+"--cache=true"}
|
|
|
|
export CACHE=$CACHE${INPUT_CACHE_TTL:+" --cache-ttl=$INPUT_CACHE_TTL"}
|
|
|
|
export CACHE=$CACHE${INPUT_CACHE_REGISTRY:+" --cache-repo=$INPUT_CACHE_REGISTRY"}
|
|
|
|
export CACHE=$CACHE${INPUT_CACHE_DIRECTORY:+" --cache-dir=$INPUT_CACHE_DIRECTORY"}
|
2020-06-20 08:41:59 +02:00
|
|
|
export CONTEXT="--context $GITHUB_WORKSPACE/$CONTEXT_PATH"
|
|
|
|
export DOCKERFILE="--dockerfile $CONTEXT_PATH/${INPUT_BUILD_FILE:-Dockerfile}"
|
2020-06-20 15:31:09 +02:00
|
|
|
export TARGET=${INPUT_TARGET:+"--target=$INPUT_TARGET"}
|
2020-06-19 13:54:58 +02:00
|
|
|
|
|
|
|
if [ ! -z $INPUT_SKIP_UNCHANGED_DIGEST ]; then
|
2021-06-08 14:52:09 +02:00
|
|
|
export DESTINATION="--digest-file digest --no-push --tarPath image.tar --destination $IMAGE"
|
2020-06-19 13:54:58 +02:00
|
|
|
else
|
|
|
|
export DESTINATION="--destination $IMAGE"
|
2020-06-20 13:37:54 +02:00
|
|
|
if [ ! -z $IMAGE_LATEST ]; then
|
2022-06-24 13:41:58 +02:00
|
|
|
export DESTINATION="$DESTINATION --destination $IMAGE_LATEST"
|
2020-06-20 13:37:54 +02:00
|
|
|
fi
|
2020-06-19 13:54:58 +02:00
|
|
|
fi
|
2020-02-15 18:55:26 +01:00
|
|
|
|
2020-06-20 15:31:09 +02:00
|
|
|
export ARGS="$CACHE $CONTEXT $DOCKERFILE $TARGET $DESTINATION $INPUT_EXTRA_ARGS"
|
2020-02-15 18:55:26 +01:00
|
|
|
|
|
|
|
cat <<EOF >/kaniko/.docker/config.json
|
|
|
|
{
|
|
|
|
"auths": {
|
|
|
|
"https://${REGISTRY}": {
|
|
|
|
"username": "${USERNAME}",
|
|
|
|
"password": "${PASSWORD}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2022-06-24 13:41:58 +02:00
|
|
|
# https://github.com/GoogleContainerTools/kaniko/issues/1803
|
2020-07-15 19:03:56 +02:00
|
|
|
# https://github.com/GoogleContainerTools/kaniko/issues/1349
|
2022-06-24 13:41:58 +02:00
|
|
|
export IFS=''
|
|
|
|
kaniko_cmd="/kaniko/executor ${ARGS} --reproducible --force"
|
|
|
|
echo "Running kaniko command ${kaniko_cmd}"
|
|
|
|
eval "${kaniko_cmd}"
|
|
|
|
|
2023-02-01 08:28:51 +01:00
|
|
|
echo "image=$IMAGE" >> $GITHUB_OUTPUT
|
2020-06-19 13:19:32 +02:00
|
|
|
|
|
|
|
if [ ! -z $INPUT_SKIP_UNCHANGED_DIGEST ]; then
|
2020-06-19 13:54:58 +02:00
|
|
|
export DIGEST=$(cat digest)
|
2020-06-20 19:07:45 +02:00
|
|
|
|
2022-04-29 11:12:49 +02:00
|
|
|
/kaniko/crane auth login $REGISTRY -u $USERNAME -p $PASSWORD
|
|
|
|
|
|
|
|
export REMOTE=$(crane digest $REGISTRY/${REPOSITORY}:latest)
|
2020-06-19 13:54:58 +02:00
|
|
|
|
2020-06-19 13:19:32 +02:00
|
|
|
if [ "$DIGEST" == "$REMOTE" ]; then
|
|
|
|
echo "Digest hasn't changed, skipping, $DIGEST"
|
2020-06-19 13:54:58 +02:00
|
|
|
echo "Done 🎉️"
|
2020-06-19 13:19:32 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-06-19 13:54:58 +02:00
|
|
|
echo "Pushing image..."
|
2022-06-24 13:41:58 +02:00
|
|
|
|
2020-06-29 14:26:53 +02:00
|
|
|
/kaniko/crane push image.tar $IMAGE
|
2020-06-19 13:19:32 +02:00
|
|
|
|
2020-06-29 14:26:53 +02:00
|
|
|
if [ ! -z $IMAGE_LATEST ]; then
|
|
|
|
echo "Tagging latest..."
|
2022-06-24 13:41:58 +02:00
|
|
|
/kaniko/crane tag $IMAGE latest
|
2020-06-29 14:26:53 +02:00
|
|
|
fi
|
2022-06-24 13:41:58 +02:00
|
|
|
|
2020-06-19 13:54:58 +02:00
|
|
|
echo "Done 🎉️"
|
|
|
|
fi
|