Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Created January 26, 2024 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slmcmahon/e6c5ea2b102ffbe03d37a477fc053969 to your computer and use it in GitHub Desktop.
Save slmcmahon/e6c5ea2b102ffbe03d37a477fc053969 to your computer and use it in GitHub Desktop.
Check if current deployments are tagged as production in Azure Container Registry
#!/bin/bash
ACR_NAME='the name of your ACR'
# Get all the deployment names in the current namespace
deployments=$(kubectl get deployments -o jsonpath='{.items[*].metadata.name}')
# For each deployment
for deployment in $deployments; do
# Extract the image that is currently used in this deployment
image=$(
kubectl get deployment "$deployment" \
-o jsonpath="{.spec.template.spec.containers[*].image}"
)
# split the image name away from the repository path
repowithtag=$(echo "$image" | cut -d '/' -f2)
# split on : and take the left hand side (repo)
reponame=$(echo "$repowithtag" | cut -d ':' -f1)
# split on : and gake the right hand side (tag)
versiontag=$(echo "$repowithtag" | cut -d ':' -f2)
# check the current repository in ACR to check if the image used
# in the repo is also tagged as production.
# This will return two tags if it is properly taged (wc -l = 2)
# or
# It will return 0 (wc -l = 0)
result_count=$(
az acr manifest list-metadata \
--only-show-errors \
"$ACR_NAME.azurecr.io/$reponame" \
--query "[?tags && contains(tags, 'production') && contains(tags, '$versiontag')].tags[]" \
--output tsv | wc -l
)
# if we didn't get 2 tags, then
if [ "$result_count" -ne 2 ]; then
echo "$image is not properly tagged as production"
else
echo "$image is OK"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment