How to check if a container image exists on ghcr.io?
You can use the tags/list endpoint to grab all available tags.
You’ll need a token for this like a PAT. (soon this will work with the GITHUB_TOKEN)
Here’s an example:
step 1.Go to Personal tokens in Github Developer settings.
# for a public image you can get a fake NOOP token to use
curl https://ghcr.io/token\?scope\="repository:USER/IMAGE:pull"
{"token":"{TOKEN}"}
curl -H "Authorization: Bearer {TOKEN}" https://ghcr.io/v2/USER/IMAGE/tags/list
{"name":"USER/IMAGE","tags":["2.7.5-arm64","2.7.5-armhf","2.7.5-amd64"]}
step 2.To list tags for organisation/user myorg for project myproject
# To list tags
curl -H "Authorization: Bearer ${GHCR_TOKEN}" https://ghcr.io/v2/USER/IMAGE/tags/list
curl -H "Authorization: Bearer $GHCR_TOKEN" https://ghcr.io/v2/myorg/myproject/tags/list
step 3.You should get a JSON reply like:
{"name":"myorg/myproject","tags":["pr-58"]}
Now that ghcr.io supports GITHUB_TOKEN, you can do the following
GHCR_TOKEN=$(echo ${{ secrets.GITHUB_TOKEN }} | base64)
# To list tags
curl -H "Authorization: Bearer ${GHCR_TOKEN}" https://ghcr.io/v2/USER/IMAGE/tags/list
# To pull manifest
OLD_TAG=test
MANIFEST=$(curl -H "Authorization: Bearer ${GHCR_TOKEN}" https://ghcr.io/v2/USER/IMAGE/manifests/$OLD_TAG)
# To push a new tag
CONTENT_TYPE="application/vnd.docker.distribution.manifest.v2+json"
NEW_TAG=latest
curl -f -X PUT -H "Content-Type: ${CONTENT_TYPE}" -H "Authorization: Bearer ${GHCR_TOKEN}" -d "${MANIFEST}" "https://ghcr.io/v2/USER/IMAGE/manifests/$NEW_TAG"
https://github.com/orgs/community/discussions/26279