I recently worked on a project to push our private docker images to GitHub’s package registry. Our CI only had a GITHUB_TOKEN
environment variable set, but docker login
requires a username too.
Rather than add a new environment variable to the build and an assumption that the username and token had to match, I used this snippet to derive the username from the token and log in to the registry.
Note: This snippet requires jq
and curl
.
GITHUB_USER=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user | jq -r .login)
echo $GITHUB_TOKEN | docker login ghcr.io -u "${GITHUB_USER}" --password-stdin