Skip to content

Commit

Permalink
Slots Cleanup - Adding new check (#1211)
Browse files Browse the repository at this point in the history
* PR - Adding new check for checking PR and slots counts

* Adding Token-Id check

* Adding link to actual repo and solving the query param

* Removing --name from the query

* Checking variables

* Adding new step to load .env

* Removing quote from the resource group

* Adding query to filter out the result

* Adding check to compare the numbers

* Getting the PR ids

* adding a step to check out

* Adding count object with the PRs slots

* removing count from the extension

* Wrapping the array inside the bracket

* Updating counting logic

* adding tsv output for the slot name

* Replacing the prefix of PR names

* Getting Slots ids to compare it with

* Adding logic to extract missing slots

* Adding comments for the step

* Replacing the InputObject to SideIndicator to handle error

* Wrapping the object with double quote

* Logic to extract unique values

* Unique id from the object

* Adding logic for the extraction of Slots ids

* wrapping the arrays in double quotes

* Fixing typo with variable name

* Renaming variables

* Adding scheduler for the action

* Adding workflow dispatcher

* Adding workflow dispatch with the pull request

* removing variable with dispatcher

* Modifying the delete flow and adding trigger for in the new flow

* Updating syntax

* wrapping the curl request with `

* Curl - changing the syntax

* Adding third party action to trigger the workflow

* Adding ref for the main branch

* adding the extention to find the yml

* Removing repo name since it's in same repo

* removing the input for the flow

* updating flow to callable flow and calling it from the flow itself

* Converting the input into json

* adding the callable flow in separate action

* Inheriting secrets

* Updating the slot logic

* adding check to echo the array

* adding check for the second job

* changing array to a single id

* Clean up

* Updating the variable name

* displaying the variable

* Adding firstSlotId

* Changing the logic to compare the PR slots

* fixing typo

* Adding logic for single slot id

* Slot id in an array

* Adding second check to avoid to avoid running second flow

* Putting variable to outside of condition to update itself

* Adding schedule to run the flow

* fixing typo

* Adding logic to run it for multiple ids

* removing comment

* fixing the variable name

* adding json in the variable list

* spliting the input

* removing fromJSON to matrix

* adding json

* adding the comma and formatting it in Json

* adding the slotname in output

* testing the object type

* setting up the variable name

* testing param

* removing hardcoded values

* testing with variable name

* changing the syntax for output

* changing into an array

* changing the variable wrapping

* adding variable from json

* putting into json

* Updating the param to JSON

* wrapping the ouput

* wrapping it with the object

* replacing double quotes with backslashes

* fixing typo

* removing the second check

* compressing the string

* removing already formatted json

* changing the variable name

* fixing variable name

* fixing the echo with json

* adding string to convert the object

* removing the output thing

* removing the step to add the string

* adding the matrix back

* adding the env

* without env

* adding the text

* removing slot and parsing it first

* tesingt output

* checking without env

* with quotes

* adding the json string for testing

* adding double quote

* adding new formate

* changing the type of input

* removing some more content

* without single quotes

* in double quotes

* changing the quotes

* adding inside the object

* removing quotes

* adding text

* joining text

* adding json

* without quote

* converting from json

* adding space between comma's

* using actual example

* with env

* adding new YML

* assigning hardcoded values

* adding hardcoded json

* single quote

* adding formatted json

* Json from the other workflow

* adding listofslotsFromFirst job

* adding json format

* wrapping the text

* Separating azure login

* Adding the dependencies for the different jobs

* reverting back last commit

* cleaning up the first flow

* adding hardcoded false

* Clean up with the pr-close flow

* making naming conventions consistent

* removing testing yml

* handling single id scenario

* Clean up and check with single id

* Removing hardcoded values

* Clean with the naming conventions

* Adding scheduling to run this flow

* Update .github/workflows/check-active-prs-and-slots.yml

Co-authored-by: Matt Wicks [SSW] <[email protected]>

* Update .github/workflows/check-active-prs-and-slots.yml

Co-authored-by: Matt Wicks [SSW] <[email protected]>

* Update - camelCase for pr list step

* Removing redundant steps

* removing the quote to output the slots

* echoing slot

* testing echo

* Checking the length of the array

* Removing double quotes

* adding bool value to check the next step

* updating logic for next flow

* using github cli

* adding github token as env

* Adding hardcoded values

* Adding hardcoded value to test

* Clean up the hardcoded values

* Removing singular values

* adding jq query to get the values

* Removing extra step and cleaning up the code for JSON

* test run for multiple ids

* Clean up from hardcoded values

* testing json values

* for testing

* testing it with JSOn

* testing

* quoting it

* adding the env. slot name

* without outputting it

* Adding the values back

* removing from JSON

* adding an array for testing

* testing

* adding from json

* removing hardcoded values

* Content - renaming the filename

* Update .github/workflows/slots-cleanup-weekly.yml

Co-authored-by: Matt Wicks [SSW] <[email protected]>

* File - renaming it to make more sense

---------

Co-authored-by: Matt Wicks [SSW] <[email protected]>
  • Loading branch information
amankumarrr and wicksipedia authored Aug 17, 2023
1 parent 6fa85d6 commit 0beb2be
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 7 deletions.
36 changes: 29 additions & 7 deletions .github/workflows/pr-close-delete-env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,48 @@ on:
types: [closed]
workflow_dispatch:
inputs:
environment:
slotID:
description: "Pull request number"
required: true
default: ""
workflow_call:
inputs:
slotIDs:
type: string
description: "Slot IDs"
required: true

defaults:
run:
shell: pwsh

env:
SLOT_NAME: pr-${{ github.event.number }}

SLOT_NAME: ${{ inputs.slotIDs || inputs.slotID || github.event.number }}
SLOT_PREFIX: pr-
permissions:
id-token: write
contents: read

jobs:
setting-up-slot-ids:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Converting slots into JSON Formatted Array
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
delete-slot:
runs-on: ubuntu-latest
needs: setting-up-slot-ids
strategy:
matrix:
SLOT_NAME: ${{ fromJson(needs.setting-up-slot-ids.outputs.matrix) }}

steps:
- uses: actions/checkout@v3
Expand All @@ -32,7 +55,6 @@ jobs:
uses: xom9ikk/dotenv@v2
with:
path: ./.github
load-mode: strict

- name: Azure CLI - Login
uses: azure/login@v1
Expand All @@ -41,7 +63,7 @@ jobs:
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Cleanup ACRPull & KeyVault permission
- name: Cleanup ACR Pull & Key Vault permission
run: |
$acrId = az acr show `
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} `
Expand All @@ -54,7 +76,7 @@ jobs:
$identityId = az webapp identity show `
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} `
--name ${{ env.APP_SERVICE_NAME }} `
--slot ${{ env.SLOT_NAME }} `
--slot ${{ env.SLOT_PREFIX }}${{ matrix.SLOT_NAME }} `
--query principalId `
--output tsv
Write-Host '✅ slot identity found'
Expand Down Expand Up @@ -92,5 +114,5 @@ jobs:
az webapp deployment slot delete `
--resource-group ${{ env.AZURE_RESOURCE_GROUP }} `
--name ${{ env.APP_SERVICE_NAME }} `
--slot ${{ env.SLOT_NAME }} `
--slot ${{ env.SLOT_PREFIX }}${{ matrix.SLOT_NAME }} `
--output none
89 changes: 89 additions & 0 deletions .github/workflows/weekly-slots-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Weekly app service slot cleanup

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

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

defaults:
run:
shell: pwsh

permissions:
id-token: write
contents: read

jobs:
check-pr-slots:
runs-on: ubuntu-latest
outputs:
slotsExistThatRequireDeletion: ${{ steps.comparision.outputs.slotsExistThatRequireDeletion }}

steps:
- name: Checking out
uses: actions/checkout@v2

- 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: Get list of deployed Slots
id: slotList
run: |
$slots = $(az webapp deployment slot list `
--name ${{ env.APP_SERVICE_NAME }} `
--resource-group ${{env.AZURE_RESOURCE_GROUP }} `
--query '[].name' `
--output tsv | `
sed 's/pr-//g')
echo "slots=$slots" >> $env:GITHUB_OUTPUT
- 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: Compare PRs with Slots
id: comparision
run: |
# Comparing the number of Slots and PRs
$prList = "${{ steps.PRList.outputs.active_prs }}" -split ' '
$slotList = "${{ steps.slotList.outputs.slots }}" -split ' '
$slotsExistThatRequireDeletion = $slotList | Where-Object { $_ -notin $prList }
$slotsNeedDeletion = $slotsExistThatRequireDeletion.Length -gt 0
if ( ! $slotsNeedDeletion ) {
echo "✅ - Number of slots are equal to number of active PRs - 🏃 Skipping next step"
}
else {
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
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 != ''
uses: ./.github/workflows/pr-close-delete-env.yml
with:
slotIDs: ${{ needs.check-pr-slots.outputs.slotsExistThatRequireDeletion }}
permissions:
id-token: write
contents: read
secrets: inherit

0 comments on commit 0beb2be

Please sign in to comment.