|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +echo "Attempting to tag all repositories in ACR $ACR_NAME with short commit hash: $SHORT_COMMIT_HASH" |
| 4 | +echo "Source tag for import will be: $ENVIRONMENT_TAG" |
| 5 | + |
| 6 | +# Get list of repositories |
| 7 | +repo_list=$(az acr repository list --name "$ACR_NAME" --output tsv) |
| 8 | + |
| 9 | +if [ -z "$repo_list" ]; then |
| 10 | + echo "No repositories found in ACR $ACR_NAME. Nothing to tag." |
| 11 | + exit 0 |
| 12 | +fi |
| 13 | + |
| 14 | +echo "Found repositories: $(echo $repo_list | wc -w)" |
| 15 | +echo "---" |
| 16 | + |
| 17 | +exit_code=0 |
| 18 | + |
| 19 | +for repo_name in $repo_list; do |
| 20 | + source_image="${ACR_NAME}.azurecr.io/${repo_name}:${ENVIRONMENT_TAG}" |
| 21 | + target_image="${repo_name}:${SHORT_COMMIT_HASH}" |
| 22 | + |
| 23 | + echo "Processing repository: $repo_name" |
| 24 | + |
| 25 | + echo " Checking for existing target tag: $SHORT_COMMIT_HASH" |
| 26 | + target_tag_check_output=$(az acr manifest list-metadata --registry "$ACR_NAME" --name "$repo_name" --query "[?tags.contains(@, '${SHORT_COMMIT_HASH}')]" --output tsv) |
| 27 | + target_tag_check_status=$? |
| 28 | + |
| 29 | + if [ $target_tag_check_status -eq 0 ] && [ -n "$target_tag_check_output" ]; then |
| 30 | + echo " Target tag '$SHORT_COMMIT_HASH' already exists. Skipping import for this repository." |
| 31 | + echo "---" |
| 32 | + continue |
| 33 | + fi |
| 34 | + |
| 35 | + echo " Proceeding with import attempt: $source_image -> $target_image" |
| 36 | + |
| 37 | + az acr import \ |
| 38 | + --name "$ACR_NAME" \ |
| 39 | + --source "$source_image" \ |
| 40 | + --image "$target_image" \ |
| 41 | + --force |
| 42 | + |
| 43 | + import_status=$? |
| 44 | + |
| 45 | + if [ $import_status -ne 0 ]; then |
| 46 | + echo " ⚠️ Warning: ACR import command failed for repository '$repo_name' (Exit Code: $import_status)." |
| 47 | + exit_code=1 # Record import failure |
| 48 | + else |
| 49 | + echo " Import successful for '$repo_name'." |
| 50 | + fi |
| 51 | + echo "---" |
| 52 | +done |
| 53 | + |
| 54 | +echo "Finished processing all repositories." |
| 55 | +exit $exit_code |
0 commit comments