-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildbuddy_setup_docker_auth.sh
More file actions
58 lines (50 loc) · 1.73 KB
/
buildbuddy_setup_docker_auth.sh
File metadata and controls
58 lines (50 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
mkdir -p "${HOME}/.docker"
config_path="${HOME}/.docker/config.json"
registry="${GHCR_REGISTRY:-ghcr.io}"
dockerhub_registry="https://index.docker.io/v1/"
if [[ -f "${config_path}" && -z "${DOCKER_AUTH_CONFIG_JSON:-}" && -z "${GHCR_DOCKER_AUTH:-}" && -z "${GHCR_USERNAME:-}" && -z "${GHCR_TOKEN:-}" ]]; then
echo "Docker config already present at ${config_path}; nothing to do." >&2
exit 0
fi
if [[ -n "${DOCKER_AUTH_CONFIG_JSON:-}" ]]; then
printf '%s\n' "${DOCKER_AUTH_CONFIG_JSON}" > "${config_path}"
exit 0
fi
declare -A auths
if [[ -n "${GHCR_USERNAME:-}" && -n "${GHCR_TOKEN:-}" ]]; then
auths["${registry}"]=$(printf '%s:%s' "${GHCR_USERNAME}" "${GHCR_TOKEN}" | base64 | tr -d '\n')
elif [[ -n "${GHCR_DOCKER_AUTH:-}" ]]; then
auths["${registry}"]="${GHCR_DOCKER_AUTH}"
fi
if [[ -n "${DOCKERHUB_USERNAME:-}" && -n "${DOCKERHUB_TOKEN:-}" ]]; then
auths["${dockerhub_registry}"]=$(printf '%s:%s' "${DOCKERHUB_USERNAME}" "${DOCKERHUB_TOKEN}" | base64 | tr -d '\n')
fi
if (( ${#auths[@]} == 0 )); then
cat >&2 <<'EOF_ERR'
Missing registry credentials.
Provide one of the following before running this script:
* DOCKER_AUTH_CONFIG_JSON: Full docker config JSON.
* GHCR_DOCKER_AUTH: Base64-encoded "username:token" string for ghcr.io.
* GHCR_USERNAME and GHCR_TOKEN environment variables.
Optional (helps avoid Docker Hub rate limits):
* DOCKERHUB_USERNAME and DOCKERHUB_TOKEN environment variables.
EOF_ERR
exit 1
fi
{
printf '{ "auths": {'
first=1
for reg in "${!auths[@]}"; do
auth="${auths[$reg]}"
if (( first == 0 )); then
printf ','
fi
first=0
printf '"%s": {"auth":"%s"}' "${reg}" "${auth}"
done
printf '} }\n'
} > "${config_path}"