Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline - Adding acr img clean up #2222

Merged
merged 69 commits into from Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
c65eb94
Pipeline - Adding acr img clean up
amankumarrr Feb 29, 2024
dcc35be
Adding step to login into Azure
amankumarrr Feb 29, 2024
74ce101
Updating job name and order of azure login step
amankumarrr Feb 29, 2024
57394a3
Updating cli command
amankumarrr Feb 29, 2024
93485ef
Updating cli to get the list of acr images
amankumarrr Feb 29, 2024
adfbae4
Adding ACR name and correct image name to get the list
amankumarrr Feb 29, 2024
dd1ba73
removing show-tags from the cmd
amankumarrr Feb 29, 2024
e78c5d9
Adding image tag for the list
amankumarrr Feb 29, 2024
90167ad
removing extra tags for the list
amankumarrr Feb 29, 2024
91e0659
updating cmd to get the tags
amankumarrr Feb 29, 2024
4ef799e
Filtering out the tags
amankumarrr Feb 29, 2024
3d7bd89
Adding order by command for the repo
amankumarrr Feb 29, 2024
f90853b
Updating the variable name
amankumarrr Feb 29, 2024
8202bb3
Removing limit on the query
amankumarrr Feb 29, 2024
1531f23
Adding comparison for the active PR and images
amankumarrr Feb 29, 2024
8c840c5
Adding pipeline to run the matrix
amankumarrr Feb 29, 2024
3d1cc1e
Adding output for the comparison step
amankumarrr Feb 29, 2024
341ad35
Adding prop as JSON param
amankumarrr Feb 29, 2024
5037127
Deleting test image
amankumarrr Feb 29, 2024
ea14759
Adding image name with tag to delete it
amankumarrr Feb 29, 2024
9c4b4e1
Updating schedule and adding login steps for Azure
amankumarrr Mar 1, 2024
da247a3
cleaning weekly check and adding updated checks
amankumarrr Mar 1, 2024
ad2b5cf
removing the tags limit
amankumarrr Mar 1, 2024
ec5b6d8
Adding limiting to 250
amankumarrr Mar 1, 2024
cc59f3d
Adding deletion step to clean up the image
amankumarrr Mar 1, 2024
f88c419
Adding comment and reducing noise
amankumarrr Mar 1, 2024
ae39536
Adding non-pr prefixed tags
amankumarrr Mar 1, 2024
d28cbfb
Tags - Wrapping it with quote
amankumarrr Mar 1, 2024
ac184ff
Updating the variable name
amankumarrr Mar 1, 2024
73713cf
echoing non-pr tags
amankumarrr Mar 1, 2024
82ff7cf
merging non-pr tags
amankumarrr Mar 1, 2024
2b1e64c
Fixing the variable name
amankumarrr Mar 1, 2024
1015741
updating variable name
amankumarrr Mar 1, 2024
5747868
Adding prefix condition for non-pr tags
amankumarrr Mar 1, 2024
fdf9927
wrapping text around input values
amankumarrr Mar 1, 2024
b1a3a76
Adding additional checks to not delete production and staging acr
amankumarrr Mar 1, 2024
916af89
Adding staging and production sha
amankumarrr Mar 1, 2024
d4f2cb1
removing PRs for temporary
amankumarrr Mar 1, 2024
9f94c91
deleting based on digest value
amankumarrr Mar 1, 2024
2b2840a
Getting digest data for specific ACR
amankumarrr Mar 1, 2024
479a776
replacing repository with registery
amankumarrr Mar 1, 2024
6920667
manifest for the specific tag
amankumarrr Mar 1, 2024
9aa1f03
getting digest value in a different way
amankumarrr Mar 1, 2024
e4a7d01
removing test docker image
amankumarrr Mar 1, 2024
133852e
Adding github sha to track it
amankumarrr Mar 1, 2024
e4f4e95
adding period with the output
amankumarrr Mar 1, 2024
3309991
Removing sha tag from the docker img
amankumarrr Mar 3, 2024
b5bbf92
Adding prs tags only to delete
amankumarrr Mar 3, 2024
4239e33
prefixing with dollar sign
amankumarrr Mar 3, 2024
0050e35
Adding non-prs tags
amankumarrr Mar 3, 2024
c4fc366
Changing the limit of tag list
amankumarrr Mar 4, 2024
635eaef
Cleaning up the steps for the delete slots
amankumarrr Mar 4, 2024
05f95d4
Removing redundant steps
amankumarrr Mar 4, 2024
92d5dbf
wrapping the tags within a quote
amankumarrr Mar 4, 2024
e360050
Removing acr login
amankumarrr Mar 4, 2024
046b127
Merge branch 'main' into 2210-spendOps
amankumarrr Mar 4, 2024
83d0263
Adding condition to stop the second flow
amankumarrr Mar 4, 2024
839d83f
Merge branch '2210-spendOps' of https://github.com/SSWConsulting/SSW.…
amankumarrr Mar 4, 2024
add3daa
Removing PR trigger
amankumarrr Mar 4, 2024
3bb174c
Passing JSON to call delete pr flow
amankumarrr Mar 4, 2024
51b33c5
Adding step to remove untagged digests
amankumarrr Mar 4, 2024
24f6d08
Adding PR trigger
amankumarrr Mar 4, 2024
559c609
Updating the query to retrieve the manifest
amankumarrr Mar 4, 2024
b702566
Adding acr tag
amankumarrr Mar 4, 2024
262141e
Updating the props for the list
amankumarrr Mar 4, 2024
81cf5e7
adding delete command to delete untagged shas
amankumarrr Mar 5, 2024
4fc9064
Adding yes prop to confirm delete
amankumarrr Mar 5, 2024
55a273f
removing PR trigger
amankumarrr Mar 5, 2024
518ec5e
Removing testing scripts
amankumarrr Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/.env
@@ -1,4 +1,5 @@
ACR_LOGIN_SERVER=acrsswwebsite.azurecr.io
ACR_NAME=acrsswwebsite
APP_SERVICE_NAME=app-sswwebsite-9eb3
AZURE_RESOURCE_GROUP=ssw.com.au
AZURE_RESOURCE_GROUP_LOCATION=australiaeast
Expand Down
72 changes: 72 additions & 0 deletions .github/workflows/delete-acr-image.yml
amankumarrr marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,72 @@
name: Delete - Docker image from ACR

on:
workflow_call:
inputs:
imageTags:
type: string
description: "Image Tags or PR numbers"
required: true
workflow_dispatch:
inputs:
imageTag:
description: "Image Tag or PR number"
required: true

defaults:
run:
shell: pwsh

env:
IMAGE_TAGS: ${{ inputs.imageTags || inputs.imageTag }}
PREFIX: pr-
permissions:
id-token: write
contents: read

jobs:
setting-up-img-tags:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Get image tags from input
id: set-matrix
run: |
$imageTags = '${{ env.IMAGE_TAGS }}'
echo "matrix=$imageTags" >> $env:GITHUB_OUTPUT

delete-acr-image:
runs-on: ubuntu-latest
needs: setting-up-img-tags
strategy:
matrix:
IMAGE_TAG: ${{ fromJson(needs.setting-up-img-tags.outputs.matrix) }}

steps:
- uses: actions/checkout@v4

- name: Load .env file
uses: xom9ikk/dotenv@v2
with:
path: ./.github

- name: Azure CLI - Login
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: ACR - Delete image
run: |
if('${{ matrix.IMAGE_TAG }}'.Length -le 4) # PR tags consist of 4 digits (i.e pr-xxxx)
{
$imageTagWithPrefix = '${{ env.PREFIX }}${{ matrix.IMAGE_TAG}}'
}else{
$imageTagWithPrefix = '${{ matrix.IMAGE_TAG }}'
}

amankumarrr marked this conversation as resolved.
Show resolved Hide resolved
az acr repository delete --name ${{ env.ACR_NAME }} `
--image ${{ env.IMAGE_NAME }}:$imageTagWithPrefix --yes
Write-Output "✅ ACR - ${{ env.IMAGE_NAME }}:$imageTagWithPrefix image deleted successfully."
22 changes: 14 additions & 8 deletions .github/workflows/pr-close-delete-env.yml
Expand Up @@ -33,15 +33,13 @@ jobs:
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Converting slots into JSON Formatted Array
- name: Get slot ids from input
id: set-matrix
run: |
# TODO: Tech Debt - need to use it as JSON instead of space delimited - Github issue - https://github.com/SSWConsulting/SSW.Website/issues/1228
$slotIDs = '${{ env.SLOT_NAME }}' -split ' '
$slotIDsInJSonArray = ConvertTo-Json -Compress @($slotIDs)
echo "matrix=$slotIDsInJSonArray" >> $env:GITHUB_OUTPUT
$slotIDs = '${{ env.SLOT_NAME }}'
echo "matrix=$slotIDs" >> $env:GITHUB_OUTPUT

delete-slot:
delete-slot-and-acr-cleanup:
runs-on: ubuntu-latest
needs: setting-up-slot-ids
strategy:
Expand All @@ -63,7 +61,7 @@ jobs:
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Cleanup ACR Pull & Key Vault permission
- name: Cleanup ACR and Key Vault Permissions
run: |
$acrId = az acr show `
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} `
Expand Down Expand Up @@ -109,7 +107,15 @@ jobs:
Write-Host '❌ Key Vault not found'
}

- name: Delete slot on staging site
- name: ACR - Delete image
run: |
$imageTagWithPrefix = '${{ env.SLOT_PREFIX }}${{ matrix.SLOT_NAME}}'

az acr repository delete --name ${{ env.ACR_NAME }} `
--image ${{ env.IMAGE_NAME }}:$imageTagWithPrefix --yes
Write-Output "✅ ACR - ${{ env.IMAGE_NAME }}:$imageTagWithPrefix image deleted successfully."

- name: Delete slot
run: |
az webapp deployment slot delete `
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} `
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/template-build.yml
Expand Up @@ -123,5 +123,4 @@ jobs:
NEXT_PUBLIC_CHATBASE_BOT_ID=${{ env.NEXT_PUBLIC_CHATBASE_BOT_ID }}
SITE_URL=https://www.ssw.com.au
tags: |
${{ env.ACR_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
${{ env.ACR_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}:${{ inputs.tag }}
107 changes: 107 additions & 0 deletions .github/workflows/weekly-acr-images-cleanup.yml
@@ -0,0 +1,107 @@
name: Weekly ACR images cleanup

on:
schedule:
# Monday at 2 PM UTC - https://cron.help/#0_14_*_*_MON
- cron: "0 14 * * MON"
workflow_dispatch:

env:
GH_TOKEN: ${{ github.token }}

defaults:
run:
shell: pwsh

permissions:
id-token: write
contents: read

jobs:
check-acr-images:
runs-on: ubuntu-latest
outputs:
imageTagList: ${{ steps.comparison.outputs.imageTagList }}
steps:
- name: Checking out
uses: actions/checkout@v4

- name: Load .env file
uses: xom9ikk/dotenv@v2
with:
path: ./.github

- name: Azure CLI - Login
uses: azure/login@v1
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Delete all untagged images
run: |
$untaggedDigests = az acr manifest list-metadata -r ${{ env.ACR_NAME }} -n `
${{ env.IMAGE_NAME }} --query "[?tags==null].digest" -o tsv | `
%{ az acr repository delete -n ${{ env.ACR_NAME }} -t ${{ env.IMAGE_NAME }}@$_ --yes}
Write-Host "✅ All untagged images have been deleted"

- name: Get list of active PRs
id: prList
run: |
$active_prs=$(gh pr list --state open --json number | jq -r '.[].number')
echo "active_prs=$active_prs" >> $env:GITHUB_OUTPUT

- name: Get list of ACR image tags
id: imageTags
run: |
$images = az acr repository show-tags `
--name ${{ env.ACR_NAME }} --repository ${{ env.IMAGE_NAME }} `
--top 50 --orderby time_asc --output tsv # Limiting to 250 tags because of the GitHub action matrix limit

# Filter tags that start with "pr-"
$filteredTags = $images | Where-Object { $_ -like "pr-*" }

# Filter tags that do not start with start with "pr-"
$tagsWithoutPRPrefix = $images | Where-Object { $_ -notlike "pr-*" -and $_ -notmatch "staging|production|a21f1263dfaa4e4977595912a8cc80c95f737f98|89a9bccde9c2a407b85105e3261d17ed27006160" }
amankumarrr marked this conversation as resolved.
Show resolved Hide resolved

# Remove the "pr-" prefix from filtered tags
$filteredTagsWithoutPrefix = $filteredTags -replace "^pr-", ""

echo "filteredTags=$filteredTagsWithoutPrefix" >> $env:GITHUB_OUTPUT
echo "tagsWithoutPRPrefix=$tagsWithoutPRPrefix" >> $env:GITHUB_OUTPUT

- name: Compare active PRs with existing ACR images
id: comparison
run: |
# Comparing the number of images and PRs
$prList = "${{ steps.PRList.outputs.active_prs }}" -split ' '
$imageTags = "${{ steps.imageTags.outputs.filteredTags }}" -split ' '

$imagesExistThatRequireDeletion = $imageTags | Where-Object { $_ -notin $prList }
$imagesNeedDeletion = $imagesExistThatRequireDeletion.Length -gt 0

if ( ! $imagesNeedDeletion ) {
echo "✅ - Number of docker images are equal to number of active PRs - 🏃 Skipping next step"
}
else {
echo "❌ - Number of docker images are not equal to number of active PRs"
Write-Host "⚡- These images need to be deleted : $imagesExistThatRequireDeletion"
}
# Convert string into Array
$tags = $imagesExistThatRequireDeletion -split ' '
$imageTagList = ConvertTo-Json -Compress @($tags)

echo "imageTagList=$imageTagList" >> $env:GITHUB_OUTPUT

invokeDeleteImage:
name: Invoking delete-acr-image
needs:
- check-acr-images #Adding second check to avoid running this flow
if: needs.check-acr-images.outputs.imageTagList != '[]'
uses: ./.github/workflows/delete-acr-image.yml
with:
imageTags: ${{ needs.check-acr-images.outputs.imageTagList }}
permissions:
id-token: write
contents: read
secrets: inherit
12 changes: 8 additions & 4 deletions .github/workflows/weekly-slots-cleanup.yml
Expand Up @@ -21,7 +21,7 @@ jobs:
check-pr-slots:
runs-on: ubuntu-latest
outputs:
slotsExistThatRequireDeletion: ${{ steps.comparision.outputs.slotsExistThatRequireDeletion }}
slotList: ${{ steps.comparision.outputs.slotList }}

steps:
- name: Checking out
Expand Down Expand Up @@ -74,16 +74,20 @@ jobs:
echo "❌ - Number of slots are not equal to number of active PRs"
Write-Host "⚡- These slots need to be deleted : $slotsExistThatRequireDeletion"
}
echo "slotsExistThatRequireDeletion=$slotsExistThatRequireDeletion" >> $env:GITHUB_OUTPUT
# Convert string into Array
$slots = $slotsExistThatRequireDeletion -split ' '
$slotList = ConvertTo-Json -Compress @($slots)

echo "slotList=$slotList" >> $env:GITHUB_OUTPUT

invokeDeleteSlot:
name: Invoking PR Close/Delete
needs:
- check-pr-slots #Adding second check to avoid running this flow
if: needs.check-pr-slots.outputs.slotsExistThatRequireDeletion != ''
if: needs.check-pr-slots.outputs.slotList != '[]'
uses: ./.github/workflows/pr-close-delete-env.yml
with:
slotIDs: ${{ needs.check-pr-slots.outputs.slotsExistThatRequireDeletion }}
slotIDs: ${{ needs.check-pr-slots.outputs.slotList }}
permissions:
id-token: write
contents: read
Expand Down