Skip to content

Commit 36d3dc4

Browse files
committed
feat: append SHORT_COMMIT_HASH to all images
1 parent a4b94bb commit 36d3dc4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

.github/workflows/stage-3-build-images.yaml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
if: needs.get-functions.outputs.FUNC_NAMES != '[]'
7474
outputs:
7575
pr_num_tag: ${{ env.PR_NUM_TAG }}
76+
short_commit_hash: ${{ env.COMMIT_HASH_TAG }}
7677
steps:
7778
- uses: actions/checkout@v4
7879
with:
@@ -237,6 +238,90 @@ jobs:
237238
path: ./${{ env.VULNERABILITIES_SUMMARY_LOGFILE }}
238239
retention-days: 21
239240

241+
tag-all-repositories:
242+
name: "Append short commit hash to images"
243+
runs-on: ubuntu-latest
244+
needs: build-and-push
245+
if: github.ref == 'refs/heads/main'
246+
permissions:
247+
id-token: write
248+
steps:
249+
- name: Az CLI login
250+
if: github.ref == 'refs/heads/main'
251+
uses: azure/login@v2
252+
with:
253+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
254+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
255+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
256+
257+
- name: Azure Container Registry login
258+
if: github.ref == 'refs/heads/main'
259+
run: az acr login --name ${{ secrets.ACR_NAME }}
260+
261+
- name: Tag All Repositories with new short commit hash
262+
env:
263+
ACR_NAME: ${{ secrets.ACR_NAME }}
264+
SHORT_COMMIT_HASH: ${{ needs.build-and-push.outputs.short_commit_hash }}
265+
ENVIRONMENT_TAG: ${{ inputs.environment_tag }}
266+
267+
run: |
268+
echo "Attempting to tag all repositories in ACR $ACR_NAME with short commit hash: $SHORT_COMMIT_HASH"
269+
270+
# Get list of repositories
271+
repo_list=$(az acr repository list --name "$ACR_NAME" --output tsv)
272+
273+
if [ -z "$repo_list" ]; then
274+
echo "No repositories found in ACR $ACR_NAME. Nothing to tag."
275+
exit 0
276+
fi
277+
278+
echo "Found repositories: $(echo $repo_list | wc -w)"
279+
echo "---"
280+
281+
exit_code=0
282+
283+
for repo_name in $repo_list; do
284+
source_image="${ACR_NAME}.azurecr.io/${repo_name}:${ENVIRONMENT_TAG}"
285+
target_image="${ACR_NAME}.azurecr.io/${repo_name}:${SHORT_COMMIT_HASH}"
286+
287+
echo "Processing repository: $repo_name"
288+
289+
echo " Checking for existing target tag: $SHORT_COMMIT_HASH"
290+
target_tag_check_output=$(az acr repository show-manifests --name "$ACR_NAME" --repository "$repo_name" --query "[?tags.contains(@, '${SHORT_COMMIT_HASH}')]" --output tsv 2>&1)
291+
target_tag_check_status=$? # Get exit status of the check command
292+
293+
if [ $target_tag_check_status -ne 0 ]; then
294+
echo " ⚠️ Warning: Failed to check for existing target tag '$SHORT_COMMIT_HASH' for repo '$repo_name'. Error: $target_tag_check_output"
295+
echo " Proceeding to import attempt regardless..."
296+
elif [ -n "$target_tag_check_output" ]; then
297+
# Check command succeeded (exit status 0) AND output is non-empty, meaning tag exists
298+
echo " Target tag '$SHORT_COMMIT_HASH' already exists. Skipping import for this repository."
299+
echo "---"
300+
continue
301+
fi
302+
303+
echo "Target tag '$SHORT_COMMIT_HASH' not found or check failed. Attempting import: $source_image -> $target_image"
304+
305+
az acr import \
306+
--name "$ACR_NAME" \
307+
--source "$source_image" \
308+
--image "$target_image" \
309+
--force
310+
311+
import_status=$?
312+
313+
if [ $import_status -ne 0 ]; then
314+
echo " ⚠️ Warning: ACR import command failed for repository '$repo_name' (Exit Code: $import_status)."
315+
exit_code=1 # Record import failure
316+
else
317+
echo " Import successful for '$repo_name'."
318+
fi
319+
echo "---"
320+
done
321+
322+
echo "Finished processing all repositories."
323+
exit $exit_code
324+
240325
aggregate-json:
241326
runs-on: ubuntu-latest
242327
needs: build-and-push

0 commit comments

Comments
 (0)