Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active August 2, 2024 14:35
Show Gist options
  • 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"
@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