PR - Adding new check for checking PR and slots counts #44
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Active PRs and Slots | |
#on: | |
# schedule: | |
# - cron: "34 11 * * 2" # Schedule to run every Wednesday at 7 UTC | |
# workflow_dispatch: | |
on: | |
pull_request: | |
branches: | |
- main | |
defaults: | |
run: | |
shell: pwsh | |
permissions: | |
id-token: write | |
contents: read | |
jobs: | |
check-pr-slots: | |
runs-on: ubuntu-latest | |
outputs: | |
SlotsWithoutPRArray: ${{ steps.listOfSlots.outputs.SlotsWithoutPRArray }} | |
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 Deployment Slots Count | |
id: slots | |
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 | |
echo "slots=$slots" >> $env:GITHUB_OUTPUT | |
env: | |
AZURE_CORE_OUTPUT: json | |
- name: Get Active PRs Count | |
id: PRs | |
run: | | |
$active_prs=$(curl -s "https://api.github.com/repos/SSWConsulting/SSW.Website/pulls?state=open" | jq -r '.[].number') | |
echo "active_prs=$active_prs" >> $env:GITHUB_OUTPUT | |
- name: Check Active PRs and Slots | |
id: AreEqualSlots | |
run: | | |
# Comparing the number of Slots and PRs | |
$PRsArray = "${{ steps.PRs.outputs.active_prs }}" -split ' ' | |
$SlotsArray = "${{ steps.slots.outputs.slots }}" -split ' ' | |
$AreSlotsEqual = $PRsArray.Length -eq $SlotsArray.Length | |
if ( $AreSlotsEqual ) { | |
echo "✅ - Number of slots are equal to number of active PRs" | |
} | |
else { | |
echo "❌ - Number of slots are not equal to number of active PRs" | |
echo "AreSlotsEqual=$AreSlotsEqual" >> $env:GITHUB_OUTPUT | |
} | |
- name: List of Slots that need to be deleted | |
id: listOfSlots | |
if: ${{steps.AreEqualSlots.outputs.AreSlotsEqual == 'False' }} | |
run: | | |
# Compare the arrays to find differences | |
$Slots = "${{ steps.slots.outputs.slots }}" | |
$PRs = "${{ steps.PRs.outputs.active_prs }}" | |
# Split the strings into arrays of numbers | |
$SlotsArray = $Slots -split ' ' | |
$PRArray = $PRs -split ' ' | |
# Find the values present in $string2 but not in $string1 | |
$SlotsWithoutPR = $SlotsArray | Where-Object { $_ -notin $PRArray } | |
if ( $SlotsWithoutPR.Length -gt 0 ) { | |
$SlotsWithoutPRArray = $SlotsWithoutPR -join ' ' | |
Write-Host "⚡- These slots need to be deleted : $SlotsWithoutPRArray" | |
echo $SlotsWithoutPRArray | |
echo "SlotsWithoutPRArray=$SlotsWithoutPRArray" >> $env:GITHUB_OUTPUT | |
} else { | |
Write-Host "✅ - Some PRs have not their slot deployed!" | |
} | |
deleteSlot: | |
name: Invoking the flow to delete slots | |
needs: check-pr-slots | |
if: ${{ needs.check-pr-slots.outputs.SlotsWithoutPRArray }} != '' | |
uses: ./.github/workflows/pr-close-delete-env.yml | |
with: | |
pullRequestId: ${{ needs.check-pr-slots.outputs.SlotsWithoutPRArray }} | |
permissions: | |
id-token: write | |
contents: read | |
secrets: inherit |