Skip to content

Instantly share code, notes, and snippets.

@arthurk
Created June 1, 2020 03:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurk/ab9ced56ce78bb8309599ccc62fa2576 to your computer and use it in GitHub Desktop.
Save arthurk/ab9ced56ce78bb8309599ccc62fa2576 to your computer and use it in GitHub Desktop.
fetch list images from GCR via service account
#!/usr/bin/env bash
set -euo pipefail
######################
# Script that fetches a list of all images from GCR
# uses 3 different users in GCR
######################
keyfile="gcr-svc-acc-keyfile.json"
projectName="example-project"
imageName="alpine"
token=$(oauth2l fetch --credentials $keyfile --scope cloud-platform.read-only --cache="")
jwt=$(curl -sSL "https://gcr.io/v2/token?service=gcr.io&scope=repository:$projectName/$imageName:pull" -u _token:$token | jq --raw-output '.token')
curl -sSL -H "Authorization: Bearer $jwt" "https://gcr.io/v2/$projectName/$imageName/tags/list"
# use oauth2 access token instead of JWT from docker registry
token=$(oauth2l fetch --credentials $keyfile --scope cloud-platform.read-only --cache="")
curl -sSL -u "oauth2accesstoken:$token" "https://gcr.io/v2/$projectName/$imageName/tags/list"
# use service account key instead of oauth2 access key
curl -sSL -u "_json_key:$(cat $keyfile)" "https://gcr.io/v2/$projectName/$imageName/tags/list"
@kbkk
Copy link

kbkk commented Mar 11, 2021

Saved me hours. Thanks!!

@slaiyer
Copy link

slaiyer commented Oct 26, 2021

Thank you for this!

@steve-heslouin
Copy link

steve-heslouin commented Sep 20, 2023

thanks a lot, truly live saver, i transformed it in python here https://gist.github.com/steve-heslouin/1dbc0734dc2075853f259c356fb15286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment