Skip to content

Commit af7ca85

Browse files
authored
feat(acr): acr purge workflow (#1278)
## Introduction ✏️ ACR periodic artefacts purge ## Resolution ✔️ * Introduced GitHub workflow and two new composite modules for: * ACR Purge * MS Teams notifications ## Miscellaneous ➕ * Dependencies updates * PR template update --------- Co-authored-by: Abhi Markan <amarkan>
1 parent c6cbac1 commit af7ca85

File tree

7 files changed

+711
-755
lines changed

7 files changed

+711
-755
lines changed

.cspell.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
"venv",
7272
"VNET",
7373
"WOGAN",
74-
"Zabd"
74+
"Zabd",
75+
"crapimdev",
76+
"crapimstaging"
7577
],
7678
"dictionaries": ["en-gb", "companies", "softwareTerms", "misc", "lorem-ipsum", "typescript", "node", "bash", "npm"],
7779
"languageSettings": [

.github/PULL_REQUEST_TEMPLATE.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
# Pull Request
2+
13
## Introduction :pencil2:
4+
25
Include a summary of the changes and related feature(s) or issue(s).
36

47
## Resolution :heavy_check_mark:
8+
59
List all changes made to the codebase.
610

711
## Miscellaneous :heavy_plus_sign:
12+
813
List any additional fixes or improvements.
914

15+
## Screenshot :camera_flash:

.github/actions/acr/action.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This GitHub Action is designed to execute Azure CLI for artefacts purge
2+
3+
name: 'Azure container registry artefacts purge'
4+
description: 'This custom GitHub Actions module will execute AZ CLI for artefacts purge'
5+
6+
# Define the inputs required for this action.
7+
inputs:
8+
credentials:
9+
description: 'Azure subscription and resource group service principal'
10+
required: true
11+
subscription:
12+
description: 'Azure subscription ID'
13+
required: true
14+
group:
15+
description: 'Azure resource group'
16+
required: true
17+
acr:
18+
description: 'Azure container registry name, without domain prefix.'
19+
required: true
20+
days:
21+
description: 'A Go-style duration string to indicate a duration beyond which images are deleted.'
22+
required: false
23+
default: '30'
24+
keep:
25+
description: 'Specifies that the latest x number of to-be-deleted tags are retained. The latest tags are determined by the last modified time of the tag.'
26+
required: false
27+
default: '3'
28+
29+
# Define the steps to run this action.
30+
runs:
31+
using: 'composite'
32+
steps:
33+
# Step 1: Check out the repository.
34+
- name: Repository 🗃️
35+
uses: actions/checkout@v4
36+
37+
# Step 2: Log in to Azure.
38+
- name: Azure 🔐
39+
uses: azure/login@v2
40+
with:
41+
creds: ${{ inputs.credentials }}
42+
43+
# Step 3: Purge artefacts
44+
# https://learn.microsoft.com/en-us/azure/container-registry/container-registry-auto-purge
45+
- name: Artefacts 🗃️
46+
working-directory: .
47+
run: |
48+
az acr run --registry ${{ inputs.acr }} --resource-group ${{ inputs.group }} --subscription ${{ inputs.subscription }} --cmd "acr purge --filter 'mdm:.*' --ago ${{ inputs.days }}d --keep ${{ inputs.keep }}" /dev/null
49+
shell: bash

.github/actions/notify/action.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This GitHub Action is designed to dispatch a custom content MS Teams notification
2+
3+
name: 'Notify'
4+
description: 'This custom GitHub Actions module dispatches MS Teams notification card'
5+
6+
# Define the inputs required for this action.
7+
inputs:
8+
webhook:
9+
description: 'MS Teams channel webhook'
10+
required: true
11+
content:
12+
description: 'Notification card content'
13+
required: true
14+
15+
# Define the steps to run this action.
16+
runs:
17+
using: 'composite'
18+
steps:
19+
# Step 1: Check out the repository.
20+
- name: Repository 🗃️
21+
uses: actions/checkout@v4
22+
23+
# Step 2: Dispatch notification
24+
- name: Notification 🔔
25+
shell: bash
26+
run: |
27+
curl --location '${{ inputs.webhook }}' \
28+
--header 'Content-Type: application/json' \
29+
--data-raw '${{ inputs.content }}'

.github/workflows/purge.yml

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: Purge
2+
run-name: 🧹Azure purge
3+
4+
on:
5+
schedule:
6+
- cron: '0 23 * * *'
7+
8+
env:
9+
APPLICATION: ${{ vars.APPLICATION }}
10+
TIMEZONE: ${{ vars.TIMEZONE }}
11+
12+
jobs:
13+
# 1. Base actions configrations
14+
setup:
15+
name: Setup 🔧
16+
runs-on: [self-hosted, linux, deployment]
17+
outputs:
18+
application: ${{ env.APPLICATION }}
19+
environment: ${{ steps.environment.outputs.environment }}
20+
timezone: ${{ env.TIMEZONE }}
21+
22+
steps:
23+
- name: Environment 🧪
24+
id: environment
25+
run: |
26+
echo "environment=infrastructure" >> "$GITHUB_OUTPUT"
27+
28+
- name: Timezone 🌐
29+
run: echo "Timezone set to ${{ env.TIMEZONE }}"
30+
31+
# 2. Azure purge
32+
purge:
33+
name: Purge 🗑️
34+
needs: [setup]
35+
environment: ${{ needs.setup.outputs.environment }}
36+
runs-on: [self-hosted, APIM, deployment]
37+
env:
38+
ENVIRONMENT: ${{ needs.setup.outputs.environment }}
39+
40+
strategy:
41+
# Do not cancel in-progress jobs upon failure
42+
fail-fast: false
43+
# Single dimension matrix
44+
matrix:
45+
acr: ['crapimdev001', 'crapimstaging001']
46+
47+
concurrency:
48+
group: acr-purge-${{ github.workflow }}-${{ github.workflow_ref }}-${{ matrix.acr }}
49+
cancel-in-progress: true
50+
51+
steps:
52+
- name: Repository 🗂️
53+
uses: actions/checkout@v4
54+
55+
- name: Subscription 🛠️
56+
run: |
57+
if [[ "${{ matrix.acr }}" == *"dev"* ]]; then
58+
echo "SUBSCRIPTION=${{ secrets.DEV_SUBSCRIPTION }}" >> $GITHUB_ENV
59+
elif [[ "${{ matrix.acr }}" == *"staging"* ]]; then
60+
echo "SUBSCRIPTION=${{ secrets.STAGING_SUBSCRIPTION }}" >> $GITHUB_ENV
61+
fi
62+
63+
- name: Resource group 🛠️
64+
run: |
65+
if [[ "${{ matrix.acr }}" == *"dev"* ]]; then
66+
echo "RESOURCE_GROUP=${{ secrets.DEV_RESOURCE_GROUP }}" >> $GITHUB_ENV
67+
elif [[ "${{ matrix.acr }}" == *"staging"* ]]; then
68+
echo "RESOURCE_GROUP=${{ secrets.STAGING_RESOURCE_GROUP }}" >> $GITHUB_ENV
69+
fi
70+
71+
- name: Execute ⚡
72+
uses: ./.github/actions/acr
73+
with:
74+
credentials: ${{ secrets.AZURE_CREDENTIALS_ACR_PURGE }}
75+
subscription: ${{ env.SUBSCRIPTION }}
76+
group: ${{ env.RESOURCE_GROUP }}
77+
acr: ${{ matrix.acr }}
78+
days: ${{ vars.ACR_PURGE_NONPROD_DAYS }}
79+
80+
- name: Notification 🔔
81+
uses: ./.github/actions/notify
82+
with:
83+
webhook: ${{ secrets.MSTEAMS_WEBHOOK }}
84+
content: '{
85+
"@type": "MessageCard",
86+
"@context": "http://schema.org/extensions",
87+
"themeColor": "00703c",
88+
"title": "🗑️ ${{ matrix.acr }} purge",
89+
"summary": "Purged artefacts from ''${{ matrix.acr }}'' Azure container registry",
90+
"sections": [
91+
{
92+
"activityTitle": "ACR Purge",
93+
"activitySubtitle": "Purged artefacts from **${{ matrix.acr }}** Azure container registry",
94+
"facts": [
95+
{ "name": "ACR", "value": "${{ matrix.acr }}" },
96+
{ "name": "Resource group", "value": "${{ env.RESOURCE_GROUP }}" },
97+
{ "name": "Commit", "value": "${{ github.sha }}" }
98+
],
99+
"markdown": true
100+
}
101+
],
102+
"potentialAction": [
103+
{
104+
"@type": "OpenUri",
105+
"name": "Workflow",
106+
"targets": [
107+
{
108+
"os": "default",
109+
"uri": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
110+
}
111+
]
112+
}
113+
]
114+
}'

0 commit comments

Comments
 (0)