Skip to content

Commit aca4245

Browse files
authored
feat: DTOSS-8139 append commit hash tag to all images (#146)
* feat: append SHORT_COMMIT_HASH to all images * feat: append SHORT_COMMIT_HASH to all images * feat: append SHORT_COMMIT_HASH to all images * feat: run step from a script file
1 parent 43a1c11 commit aca4245

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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

Lines changed: 28 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,33 @@ 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+
run: bash ./templates/scripts/deployments/append-commit-hash.sh
267+
240268
aggregate-json:
241269
runs-on: ubuntu-latest
242270
needs: build-and-push
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)