1
0
Fork 0
mirror of https://github.com/aevea/action-kaniko.git synced 2025-01-30 22:09:37 +01:00
action-kaniko/entrypoint.sh

132 lines
4.1 KiB
Bash
Raw Normal View History

2020-02-15 18:55:26 +01:00
#!/busybox/sh
set -e pipefail
if [ "$INPUT_DEBUG" = "true" ]; then
2022-03-25 15:09:12 +01:00
set -o xtrace
fi
2020-02-15 18:55:26 +01:00
export REGISTRY="${INPUT_REGISTRY:-"docker.io"}"
export IMAGE="$INPUT_IMAGE"
export BRANCH=$(echo "$GITHUB_REF" | sed -E "s/refs\/(heads|tags)\///g" | sed -e "s/\//-/g")
export TAG=${INPUT_TAG:-$([ "$BRANCH" = "master" ] && echo latest || echo "$BRANCH")}
export TAG="${TAG:-"latest"}"
export TAG="${TAG#$INPUT_STRIP_TAG_PREFIX}"
export USERNAME="${INPUT_USERNAME:-$GITHUB_ACTOR}"
export PASSWORD="${INPUT_PASSWORD:-$GITHUB_TOKEN}"
export REPOSITORY="$IMAGE"
export IMAGE="${IMAGE}:${TAG}"
export CONTEXT_PATH="$INPUT_PATH"
if [ "$INPUT_TAG_WITH_LATEST" = "true" ]; then
export IMAGE_LATEST="${REPOSITORY}:latest"
fi
ensure() {
2020-02-15 18:55:26 +01:00
if [ -z "${1}" ]; then
echo >&2 "Unable to find the ${2} variable. Did you set with.${2}?"
2020-02-15 18:55:26 +01:00
exit 1
fi
}
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
if [ "$REGISTRY" = "ghcr.io" ]; then
IMAGE_NAMESPACE="$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')"
# Set `/` separator, unless image is pre-fixed with dash or slash
[ -n "$REPOSITORY" ] && [[ ! "$REPOSITORY" =~ ^[-/] ]] && SEPARATOR="/"
export IMAGE="$IMAGE_NAMESPACE$SEPARATOR$IMAGE"
export REPOSITORY="$IMAGE_NAMESPACE$SEPARATOR$REPOSITORY"
2020-02-15 18:55:26 +01:00
if [ -n "$IMAGE_LATEST" ]; then
export IMAGE_LATEST="${IMAGE_NAMESPACE}/${IMAGE_LATEST}"
fi
if [ -n "$INPUT_CACHE_REGISTRY" ]; then
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
2020-02-15 18:55:26 +01:00
export REGISTRY="index.${REGISTRY}/v1/"
else
export IMAGE="${REGISTRY}/${IMAGE}"
if [ -n "$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"}
export DIGEST="--digest-file /kaniko/digest --image-name-tag-with-digest-file=/kaniko/image-tag-digest"
if [ -n "$INPUT_SKIP_UNCHANGED_DIGEST" ]; then
export DESTINATION="--no-push --tarPath image.tar --destination $IMAGE"
else
export DESTINATION="--destination $IMAGE"
if [ -n "$IMAGE_LATEST" ]; then
export DESTINATION="$DESTINATION --destination $IMAGE_LATEST"
fi
fi
2020-02-15 18:55:26 +01:00
export ARGS="$CACHE $CONTEXT $DOCKERFILE $TARGET $DIGEST $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
# https://github.com/GoogleContainerTools/kaniko/issues/1803
# https://github.com/GoogleContainerTools/kaniko/issues/1349
export IFS=''
kaniko_cmd="/kaniko/executor ${ARGS} --reproducible --force"
echo "Running kaniko command ${kaniko_cmd}"
eval "${kaniko_cmd}"
echo "image=$IMAGE" >> "$GITHUB_OUTPUT"
echo "digest=$(cat /kaniko/digest)" >> "$GITHUB_OUTPUT"
echo "image-tag-digest=$(cat /kaniko/image-tag-digest)" >> "$GITHUB_OUTPUT"
if [ -n "$INPUT_SKIP_UNCHANGED_DIGEST" ]; then
export DIGEST="$(cat /kaniko/digest)"
/kaniko/crane auth login "$REGISTRY" -u "$USERNAME" -p "$PASSWORD"
export REMOTE=$(crane digest "${REGISTRY}/${REPOSITORY}:latest")
if [ "$DIGEST" = "$REMOTE" ]; then
echo "refreshed=false" >> "$GITHUB_OUTPUT"
echo "Digest hasn't changed, skipping, $DIGEST"
echo "Done 🎉️"
exit 0
fi
echo "Pushing image..."
/kaniko/crane push image.tar "$IMAGE"
if [ -n "$IMAGE_LATEST" ]; then
echo "Tagging latest..."
/kaniko/crane tag "$IMAGE" latest
fi
echo "refreshed=false" >> "$GITHUB_OUTPUT"
echo "Done 🎉️"
fi