Skip to content

Bump the github-actions group with 5 updates #27

Bump the github-actions group with 5 updates

Bump the github-actions group with 5 updates #27

Workflow file for this run

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
name: 'PR Cleanup'
on:
pull_request_target:
types: [closed]
permissions:
id-token: write
contents: read
pull-requests: write
issues: write
jobs:
cleanup:
name: Clean up PR resources
runs-on: ubuntu-latest
environment: ${{ github.event.pull_request.head.repo.full_name != github.repository && 'ftk-fork' || 'ftk-pr' }}
steps:
- name: Install Az modules
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module -Name Az.Accounts -RequiredVersion 2.19.0 -Force
Install-Module -Name Az.Resources -RequiredVersion 6.16.2 -Force
- name: Azure login
uses: azure/login@a457da9ea143d694b1b9c7c869ebb04ebe844ef5 # v2.3.0
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
enable-AzPSSession: true
- name: Delete resource groups
id: delete
shell: pwsh
run: |
$prNumber = "${{ github.event.pull_request.number }}"
$pattern = "pr-$prNumber-*"
$groups = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -like $pattern }
if (-not $groups)
{
Write-Host "No resource groups found matching '$pattern'."
"deleted=0" >> $env:GITHUB_OUTPUT
"failed=" >> $env:GITHUB_OUTPUT
return
}
$deleted = 0
$failed = @()
foreach ($rg in $groups)
{
try
{
Write-Host "Deleting $($rg.ResourceGroupName)..."
Remove-AzResourceGroup -Name $rg.ResourceGroupName -Force -ErrorAction Stop
$deleted++
}
catch
{
Write-Warning "Failed to delete $($rg.ResourceGroupName): $_"
$failed += $rg.ResourceGroupName
}
}
"deleted=$deleted" >> $env:GITHUB_OUTPUT
"failed=$($failed -join ',')" >> $env:GITHUB_OUTPUT
- name: Post cleanup comment
if: steps.delete.outputs.deleted != '0'
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr comment "${{ github.event.pull_request.number }}" \
--repo "${{ github.repository }}" \
--body "Test environments cleaned up."
- name: Create issue for failed cleanups
if: steps.delete.outputs.failed != ''
env:
GH_TOKEN: ${{ github.token }}
run: |
failed="${{ steps.delete.outputs.failed }}"
pr=${{ github.event.pull_request.number }}
author=${{ github.event.pull_request.user.login }}
issue_url=$(gh issue create \
--repo "${{ github.repository }}" \
--title "Cleanup failed for PR #$pr" \
--body "The following resource groups could not be deleted: $failed. Please delete them manually. See #$pr.")
# Try to assign the PR author; ignore failure for external contributors
issue_number=$(echo "$issue_url" | grep -oE '[0-9]+$')
gh issue edit "$issue_number" --repo "${{ github.repository }}" --add-assignee "$author" || true
- name: Create issue if cleanup job failed
# Catches hard failures (login, Get-AzResourceGroup) that abort before
# the delete step can report per-group results — otherwise pr-*-* groups
# would linger with no notification.
if: failure()
env:
GH_TOKEN: ${{ github.token }}
run: |
pr=${{ github.event.pull_request.number }}
author=${{ github.event.pull_request.user.login }}
issue_url=$(gh issue create \
--repo "${{ github.repository }}" \
--title "Cleanup failed for PR #$pr" \
--body "Automated cleanup for PR #$pr failed before it could delete resource groups. Check for lingering \`pr-$pr-*\` resource groups and delete them manually. [View logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}).")
issue_number=$(echo "$issue_url" | grep -oE '[0-9]+$')
gh issue edit "$issue_number" --repo "${{ github.repository }}" --add-assignee "$author" || true