-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathbuildbuddy_setup_docker_auth.sh
More file actions
executable file
·64 lines (56 loc) · 2.2 KB
/
Copy pathbuildbuddy_setup_docker_auth.sh
File metadata and controls
executable file
·64 lines (56 loc) · 2.2 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
59
60
61
62
63
64
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
mkdir -p "${HOME}/.docker"
config_path="${HOME}/.docker/config.json"
registry="${OCI_REGISTRY:-${GHCR_REGISTRY:-ghcr.io}}"
dockerhub_registry="https://index.docker.io/v1/"
if [[ -f "${config_path}" && -z "${DOCKER_AUTH_CONFIG_JSON:-}" && -z "${OCI_DOCKER_AUTH:-}" && -z "${OCI_USERNAME:-}" && -z "${OCI_TOKEN:-}" && -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 "${OCI_USERNAME:-}" && -n "${OCI_TOKEN:-}" ]]; then
auths["${registry}"]=$(printf '%s:%s' "${OCI_USERNAME}" "${OCI_TOKEN}" | base64 | tr -d '\n')
elif [[ -n "${GHCR_USERNAME:-}" && -n "${GHCR_TOKEN:-}" ]]; then
auths["${registry}"]=$(printf '%s:%s' "${GHCR_USERNAME}" "${GHCR_TOKEN}" | base64 | tr -d '\n')
elif [[ -n "${OCI_DOCKER_AUTH:-}" ]]; then
auths["${registry}"]="${OCI_DOCKER_AUTH}"
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.
* OCI_DOCKER_AUTH: Base64-encoded "username:token" string for ${registry}.
* OCI_USERNAME and OCI_TOKEN environment variables.
* GHCR_DOCKER_AUTH: Base64-encoded "username:token" string for ${registry}.
* 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}"