From 10e98b77ff4c8926716ead3356567dbdb7886072 Mon Sep 17 00:00:00 2001 From: ytimocin Date: Fri, 10 May 2024 14:50:40 +0300 Subject: [PATCH] Update purget test resources to fix the workflow failures Signed-off-by: ytimocin --- .github/workflows/purge-test-resources.yaml | 17 ++++++++++---- .github/workflows/test/other_output.txt | 3 +++ .../test/test-purge-test-resources.sh | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test/other_output.txt create mode 100755 .github/workflows/test/test-purge-test-resources.sh diff --git a/.github/workflows/purge-test-resources.yaml b/.github/workflows/purge-test-resources.yaml index 58620ec2..1c6a281f 100644 --- a/.github/workflows/purge-test-resources.yaml +++ b/.github/workflows/purge-test-resources.yaml @@ -2,9 +2,15 @@ name: Purge test resources on: schedule: - cron: "30 0,12 * * *" + pull_request: + types: [opened, synchronize, reopened] + branches: + - v*.* + - edge env: AZURE_RG_DELETE_LIST_FILE: "az_rg_list.txt" VALID_RESOURCE_WINDOW: 6*60*60 + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} jobs: purge_azure_resources: name: Azure resources clean-ups @@ -26,9 +32,10 @@ jobs: current_time=$(date +%s) hours_ago=$((current_time - ${{ env.VALID_RESOURCE_WINDOW }})) + echo "Hours ago: $hours_ago" - jq -r '.[] | select(.creationTime == null || .creationTime < '$hours_ago') | .Name' resource_groups.json > ${{ env.AZURE_RG_DELETE_LIST_FILE}} - jq -r '.[] | {name: .Name, creationTime: .creationTime // "None"}' resource_groups.json > $GITHUB_STEP_SUMMARY + jq -r '.[] | select(.creationTime == null or .creationTime < '$hours_ago') | .Name' resource_groups.json > ${{ env.AZURE_RG_DELETE_LIST_FILE}} + jq -r '.[] | "\(.Name) \(.creationTime // "None")"' resource_groups.json > $GITHUB_STEP_SUMMARY - name: Delete Azure Resource Groups run: | @@ -40,8 +47,8 @@ jobs: done - name: Create GitHub issue on failure - if: ${{ failure() }} + if: failure() && github.event_name == 'schedule' run: | - gh issue create --title "Samples purge test resources failed \ + gh issue create --title "Samples purge test resources failed" \ --body "Test failed on ${{ github.repository }}. See [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more details." \ - s--repo ${{ github.repository }} + --repo ${{ github.repository }} diff --git a/.github/workflows/test/other_output.txt b/.github/workflows/test/other_output.txt new file mode 100644 index 00000000..93b65fc7 --- /dev/null +++ b/.github/workflows/test/other_output.txt @@ -0,0 +1,3 @@ +group1 1000 +group2 2000 +group3 3000 diff --git a/.github/workflows/test/test-purge-test-resources.sh b/.github/workflows/test/test-purge-test-resources.sh new file mode 100755 index 00000000..2ca1413c --- /dev/null +++ b/.github/workflows/test/test-purge-test-resources.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Create a mock resource_groups.json file +echo '[{"Name": "group1", "creationTime": 1000}, {"Name": "group2", "creationTime": 2000}, {"Name": "group3", "creationTime": 3000}]' > resource_groups.json + +# Set the hours_ago variable to a known value +hours_ago=1500 + +echo "hours_ago: $hours_ago" + +# Run the command +jq -r '.[] | select(.creationTime == null or .creationTime < '$hours_ago') | .Name' resource_groups.json > output.txt +jq -r '.[] | "\(.Name) \(.creationTime // "None")"' resource_groups.json > other_output.txt + +# Check the output +if [[ $(cat output.txt) == "group1" ]]; then + echo "Test passed" +else + echo "Test failed" +fi + +# Clean up +rm resource_groups.json output.txt \ No newline at end of file