This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
concourse-buildkit/entrypoint.sh

124 lines
3 KiB
Bash
Raw Normal View History

2022-03-09 13:40:17 +01:00
#!/usr/bin/env sh
2022-03-10 13:51:06 +01:00
DEFAULT_DOMAIN=docker.io
LEGACY_DEFAULT_DOMAIN=index.docker.io
2022-03-09 14:19:24 +01:00
DOCKER_LOGIN_FILE_TMPL='{
"auths": {
"{{REGISTRY_URL}}": {
"auth": "{{BASE64_UNAME_PW}}"
}
}
}'
2022-03-09 13:40:17 +01:00
2022-03-09 16:02:03 +01:00
fail() {
echo "Error:" "$@" 1>&2
exit 1
}
2022-03-09 16:09:23 +01:00
echo_and_run() {
echo "$@"
"$@"
}
2022-03-09 14:19:24 +01:00
docker_login() {
2022-03-10 13:51:06 +01:00
login_name="$1"
if [ -z "$login_name" ]; then
2022-03-10 14:04:27 +01:00
login_name="$DEFAULT_DOMAIN"
2022-03-10 13:51:06 +01:00
fi
2022-03-10 14:04:27 +01:00
if [ "$login_name" = "$DEFAULT_DOMAIN" ]; then
login_name="$LEGACY_DEFAULT_DOMAIN/v1/"
fi
login_name="https://$login_name"
2022-03-09 14:19:24 +01:00
# TODO: detect registry url
mkdir -p "$HOME/.docker"
echo "$DOCKER_LOGIN_FILE_TMPL" | \
sed -e "s|{{BASE64_UNAME_PW}}|$(printf '%s:%s' "$username" "$password" | base64)|g" \
2022-03-10 13:51:06 +01:00
-e "s|{{REGISTRY_URL}}|$login_name|g" \
2022-03-09 14:19:24 +01:00
> "$HOME/.docker/config.json"
}
2022-03-09 16:02:03 +01:00
plain() {
buildctl-daemonless.sh "$@"
}
2022-03-10 13:51:06 +01:00
split_repo_domain() {
domain_part="$(echo "$1" | sed -n 's|^\([^/]*\)/.*$|\1|p')"
other_part="$(echo "$1" | sed -n "s|^$domain_part/\?\(.*\)$|\1|p")"
if [ -z "$domain_part" ]; then
domain_part="$DEFAULT_DOMAIN"
other_part="$other_part"
elif echo "$domain_part" | grep -Evq '\.|:' && [ "$domain_part" != 'localhost' ]; then
# ^ docker sourcecode checks if $domain_part == $domain_part.lower() in effect checking if all is lower case
domain_part="$DEFAULT_DOMAIN"
other_part="$1" # we deviate here from the reference docker implementation
fi
if [ "$domain_part" = "$LEGACY_DEFAULT_DOMAIN" ]; then
domain_part="$DEFAULT_DOMAIN"
fi
if [ "$domain_part" = "$DEFAULT_DOMAIN" ] && echo "$other_part" | grep -vq /; then
other_part="library/$other_part"
fi
echo "$domain_part"
echo "$other_part"
}
2022-03-09 16:02:03 +01:00
build() {
if [ -z "$repository" ]; then
fail "missing argument: repository"
fi
if [ -z "$tag" ]; then
tag=latest
fi
if [ -z "$dest" ]; then
fail "missing argument: dest"
2022-03-09 16:02:03 +01:00
fi
if [ -z "$context" ]; then
context=.
fi
if [ -z "$platform" ]; then
platform=""
else
platform="--opt platform=$platform"
fi
final_tag="$repository:$tag"
2022-03-09 16:17:48 +01:00
if [ -n "$additional_tags" ]; then
2022-03-09 16:02:03 +01:00
while read -r line; do
if [ -z "$line" ]; then
continue
fi
final_tag="$repository:$line,$final_tag"
2022-03-09 16:02:03 +01:00
done < "$additional_tags"
fi
2022-03-09 16:09:23 +01:00
echo_and_run buildctl-daemonless.sh \
2022-03-09 16:02:03 +01:00
build \
--frontend dockerfile.v0 \
--local context="$context" \
--local dockerfile="$context" \
$platform \
--output type=oci,dest=\"$dest\"
2022-03-09 16:02:03 +01:00
}
2022-03-10 13:51:06 +01:00
if [ -n "$username" ]; then
if [ -z "$password" ]; then
fail "need to also give password when logging in"
fi
if [ -z "$repository" ]; then
2022-03-10 14:04:27 +01:00
docker_login ''
2022-03-10 13:51:06 +01:00
else
docker_login "$(split_repo_domain "$repository" | head -n1)"
fi
fi
2022-03-09 16:02:03 +01:00
if [ -z "$manual" ]; then
manual=false
fi
if "$manual"; then
plain "$@"
else
build
fi