concourse-buildkit/entrypoint.sh

88 lines
1.7 KiB
Bash
Raw Normal View History

2022-03-09 13:40:17 +01:00
#!/usr/bin/env sh
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() {
# 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" \
-e "s|{{REGISTRY_URL}}|https://index.docker.io/v1/|g" \
> "$HOME/.docker/config.json"
}
if [ -n "$username" ]; then
2022-03-09 16:02:03 +01:00
if [ -z "$password" ]; then
fail "need to also give password when logging in"
fi
2022-03-09 14:19:24 +01:00
docker_login
fi
2022-03-09 16:02:03 +01:00
plain() {
buildctl-daemonless.sh "$@"
}
build() {
if [ -z "$repository" ]; then
fail "missing argument: repository"
fi
if [ -z "$tag" ]; then
tag=latest
fi
if [ -z "$push" ]; then
push=false
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="$final_tag,$repository:$line"
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=image,\""$final_tag"\",push="$push"
}
if [ -z "$manual" ]; then
manual=false
fi
if "$manual"; then
plain "$@"
else
build
fi