-
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 Workflowname: Build and push container image (amd64)
run-name: 🚚 [backup-utils] Build and push container image
on:
workflow_dispatch:
inputs:
BACKUP_UTILS_VERSION:
description: 'Version of github-backup-utils to use'
required: true
default: '3.14.0'
env:
BACKUP_UTILS_VERSION: ${{ github.event.inputs.BACKUP_UTILS_VERSION }}
IMAGE_NAME: younsl/backup-utils
permissions:
contents: read
packages: write
jobs:
# Job 1: Check if the image with the tag already exists on GitHub Container Registry
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: |
echo "Checking if image with tag ${{ env.BACKUP_UTILS_VERSION }} exists on ghcr.io ..."
# Fetch the list of tags from GitHub Container Registry
ENCODED_TOKEN=$(echo -n "${{ secrets.GITHUB_TOKEN }}" | base64)
TAG_EXISTS=$(curl -s -H "Authorization: Bearer ${ENCODED_TOKEN}" \
https://ghcr.io/v2/${{ env.IMAGE_NAME }}/tags/list \
| jq -r --arg TAG "${{ env.BACKUP_UTILS_VERSION }}" '.tags[] | select(. == $TAG)')
# Check if the tag exists
if [[ -n "$TAG_EXISTS" ]]; 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
# Job 2: Build and push the container image if it does not exist
release:
runs-on: ubuntu-latest
needs: check
if: ${{ needs.check.outputs.image_exists == 'false' }} # Only run if the image doesn't exist
steps:
# Step 1: Checkout the repository
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Download and extract github-backup-utils
- name: Download and extract github-backup-utils
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
# Step 3: Build the Docker image for amd64
- name: Build container image (amd64)
run: |
echo "Building Docker image for amd64 architecture: ghcr.io/${{ env.IMAGE_NAME }}:${{ env.BACKUP_UTILS_VERSION }} ..."
docker build \
--platform linux/amd64 \
-t ghcr.io/${{ env.IMAGE_NAME }}:${{ env.BACKUP_UTILS_VERSION }} github-backup-utils-v${{ env.BACKUP_UTILS_VERSION }}
# Step 4: Log in to GitHub Container Registry
- name: Log in to GitHub Container Registry
run: |
echo "Logging in to GitHub Container Registry ..."
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Step 5: Push the Docker image to GitHub Container Registry for amd64
- name: Push container image (amd64)
run: |
echo "Pushing Docker image ghcr.io/${{ env.IMAGE_NAME }}:${{ env.BACKUP_UTILS_VERSION }} to ghcr.io ..."
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: