-
I’m using GHCR as my container registry. The GitOps automation we use breaks if an engineer specifies an image that doesn’t exist. For example, if the last built image is Is there a way use the GHCR v2 http API or the GitHub API to check if a specific image/tag combination exists? And what PAT scopes are needed for the check (if it is available)? |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 11 replies
-
GHCR supports the Docker Registry API. You could either try to download the specific tag and see if you get a valid manifest, or retrieve the list of tags and check if the tag shows up. You’ll need to use a token with read:packages scope for the requests. That said, maybe it’s better to catch and handle the error that currently occurs instead of adding a separate check? After all, the error occurring already tells you that the tag doesn’t exist. 😉 |
Beta Was this translation helpful? Give feedback.
-
I’m asking specifically because other topics reference portions of the Docker Registry API that haven’t been implemented yet (e.g. Additionally, we are using a third-party GitOps package which breaks when it tries to install an image that doesn’t exist. So we’re opting to check the GitOps definition during the lint/test phrase before even executing the GitOps process. |
Beta Was this translation helpful? Give feedback.
-
You can use the You’ll need a token for this like a PAT. (soon this will work with the Here’s an example:
|
Beta Was this translation helpful? Give feedback.
-
Now that ghcr.io supports GITHUB_TOKEN, you can do the following
Cheers |
Beta Was this translation helpful? Give feedback.
-
Based on @13013SwagR’s answer, here is my shell script version for Bash/command line. Creating Personal Access Token (PAT) to access Github container registry
Listing available tags for a container on GHCRFirst set your PAT in
Should give your PAT token that looks lke:
The PAT needs to be converted to base64 encoding for GHCR REST API.
To list tags for organisation/user
You should get a JSON reply like:
Here is also our Github Actions build recipe how to build and push images to GCHR. |
Beta Was this translation helpful? Give feedback.
-
REST services are okay, but what would really help users out is a basic search frontend, like Docker Hub enjoys. |
Beta Was this translation helpful? Give feedback.
-
How can we list all the docker images that we have on the GitHub container registry? |
Beta Was this translation helpful? Give feedback.
-
It's difficult to query without a webui |
Beta Was this translation helpful? Give feedback.
-
where is the documentation to use the API ? |
Beta Was this translation helpful? Give feedback.
-
Hi fellas. I hope my use case will be helpful to many people. Here is the workflow to check if a container image exists and push it to SolutionsExample WorkflowThis workflow checks if a container image exists. If not, it builds and pushes a new version to GitHub Container Registry. It uses only the Automatic Token declared in the workflow, not a Classic PAT issued from the user account. name: Release backup-utils image
run-name: 📦 [younsl/backup-utils] Release backup-utils image (amd64)
on:
workflow_dispatch:
inputs:
BACKUP_UTILS_VERSION:
description: 'Version of github-backup-utils to use'
required: true
default: 3.14.0
env:
IMAGE_NAME: younsl/backup-utils
BACKUP_UTILS_VERSION: ${{ github.event.inputs.BACKUP_UTILS_VERSION }}
permissions:
contents: read
packages: write
jobs:
check:
runs-on: ubuntu-latest
outputs:
image_exists: ${{ steps.image_check.outputs.image_exists }}
steps:
- name: Check if image exists on GitHub Container Registry
id: image_check
run: |
ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64)
TAGS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \
https://ghcr.io/v2/${{ env.IMAGE_NAME }}/tags/list)
echo "TAGS: $TAGS"
## Check if TAGS is empty or null
if [[ -z "$TAGS" || "$TAGS" == "null" ]]; then
echo "No tags found, treating as image not existing."
echo "image_exists=false" >> $GITHUB_OUTPUT
else
## Check if the specific tag already exists
if echo "$TAGS" | jq -e --arg TAG "${{ env.BACKUP_UTILS_VERSION }}" '.tags | index($TAG)'; then
echo "Image with tag ${{ env.BACKUP_UTILS_VERSION }} already exists."
echo "image_exists=true" >> $GITHUB_OUTPUT
else
echo "Image with tag ${{ env.BACKUP_UTILS_VERSION }} not found."
echo "image_exists=false" >> $GITHUB_OUTPUT
fi
fi
release:
runs-on: ubuntu-latest
needs: check
if: ${{ needs.check.outputs.image_exists == 'false' }}
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4
- name: Download and extract github-backup-utils
id: prepare
run: |
echo "Downloading github-backup-utils version ${{ env.BACKUP_UTILS_VERSION }} ..."
curl -L -o github-backup-utils-${{ env.BACKUP_UTILS_VERSION }}.tar.gz \
https://github.com/github/backup-utils/releases/download/v${{ env.BACKUP_UTILS_VERSION }}/github-backup-utils-v${{ env.BACKUP_UTILS_VERSION }}.tar.gz
echo "Extracting github-backup-utils tarball ..."
tar -xzf github-backup-utils-${{ env.BACKUP_UTILS_VERSION }}.tar.gz
- name: Login to GitHub Container Registry (ghcr.io)
id: login
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build
id: build
run: |
docker build \
--platform linux/amd64 \
-t ghcr.io/${{ env.IMAGE_NAME }}:${{ env.BACKUP_UTILS_VERSION }} github-backup-utils-v${{ env.BACKUP_UTILS_VERSION }}
- name: Push
id: push
run: |
docker push ghcr.io/${{ env.IMAGE_NAME }}:${{ env.BACKUP_UTILS_VERSION }} Key Points1. List image tags using
|
Beta Was this translation helpful? Give feedback.
-
Any WebUI recommend to go through GHCR images from Github Container registry (GHCR)? |
Beta Was this translation helpful? Give feedback.
-
I am confused. It is really great that GitHub provides a Docker Registry. Even better that Docker images can be used with GitHub Actions. But, I don't understand how the ability to do basic maintenance is missing? Even if not by API, there should be a basic way to view exiting Docker images, under your account, and delete old cruft, especially if there is a security concern found, right? I don't see any mention of how to do either of these steps mentioned on either of the Docker Registry pages? Is this documented somewhere else? Working with the Docker registry - GitHub Docs Working with the Container registry - GitHub Docs |
Beta Was this translation helpful? Give feedback.
-
As a user of a project that publishes container images on ghcr, I would really appreciate the ability to view what images are available without having to find my GitHub token, open my terminal, and run a curl command. |
Beta Was this translation helpful? Give feedback.
-
Our orchestration framework pings the registry for images for the "latest" hashes so we can see if we're behind. One of our dependencies switched to ghcr, and we're getting authentication errors when we try to list tags for this image. As a consumer of public docker images, it's really weird that I can't anonymously list tags for a public image without a token.
|
Beta Was this translation helpful? Give feedback.
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: