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.

GeIcThHoUB$_GUISTEHRU=B$_(TcOuKrElN-|sSdLoc-kHer"Aluotghionrigzhactri.oino:-tuok"e$n{G$IGTIHTUHBU_BU_STEORK}E"N"-hptatspssw:r/da-psit.dgiinthub.com/user|jq-r.login)