Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active January 18, 2024 10:48
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mislav/e154d707db230dc882d7194ec85d79f6 to your computer and use it in GitHub Desktop.
Save mislav/e154d707db230dc882d7194ec85d79f6 to your computer and use it in GitHub Desktop.
Experiment in using GitHub CLI to authenticate fetching docker images from GitHub Package Registry
// ~/docker/config.json
{
"credsStore": "desktop",
"credHelpers": {
"docker.pkg.github.com": "gh",
"ghcr.io": "gh"
}
}
#!/bin/bash
# This "docker-credential-gh" utility should exist an as executable somewhere in PATH.
#
# Dependencies: gh
#
set -e
cmd="$1"
if [ "erase" = "$cmd" ]; then
cat - >/dev/null
exit 0
fi
if [ "store" = "$cmd" ]; then
cat - >/dev/null
exit 0
fi
if [ "get" != "$cmd" ]; then
exit 1
fi
host="$(cat -)"
host="${host#https://}"
host="${host%/}"
if [ "$host" != "ghcr.io" ] && [ "$host" != "docker.pkg.github.com" ]; then
exit 1
fi
token="$(gh config get -h github.com oauth_token)"
if [ -z "$token" ]; then
exit 1
fi
printf '{"Username":"%s", "Secret":"%s"}\n' "$(gh config get -h github.com user)" "$token"
@MH4GF
Copy link

MH4GF commented Aug 4, 2022

Thanks for making this possible! I was able to login in my environment,

$ docker login ghcr.io
Authenticating with existing credentials...
Login Succeeded

However, it seemed to be ~/.docker/config.json , not ~/docker/config.json , that was overriding the settings.
I hope this is helpful to those who came to see it.

@pkit
Copy link

pkit commented Aug 18, 2023

Should be:

printf '{"Username":"%s", "Secret":"%s"}\n' "$(gh api user -q '.login')" "$token"

on the last line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment