From 190c1cf20113685a08c904742a3dcd91fae5b176 Mon Sep 17 00:00:00 2001 From: ebronson68 <111298136+ebronson68@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:12:22 -0500 Subject: [PATCH 1/5] Add Clear redis cache workflow --- .../workflows/clear-azure-redis-cache.yaml | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 .github/workflows/clear-azure-redis-cache.yaml diff --git a/.github/workflows/clear-azure-redis-cache.yaml b/.github/workflows/clear-azure-redis-cache.yaml new file mode 100644 index 00000000..c10c95ad --- /dev/null +++ b/.github/workflows/clear-azure-redis-cache.yaml @@ -0,0 +1,77 @@ +name: Clear Azure Redis Cache + +on: + workflow_call: + inputs: + environment: + required: true + type: string + description: "Environment to clear caches for." + secrets: + azureCredentials: + required: true + +jobs: + clear_cache: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Hash azureCredentials secret + uses: pplanel/hash-calculator-action@v1.3.1 + id: hash + with: + input: ${{ secrets.azureCredentials }} + method: MD5 + + - name: Cache Azure credentials + id: azure-cache + uses: actions/cache@v3 + with: + path: | + ~/.Azure + ${{ github.workspace }}/.Azure + key: ${{ runner.os }}-azurecreds-${{ steps.hash.outputs.digest }} + + - name: Login via Az module + if: steps.azure-cache.outputs.cache-hit != 'true' + uses: azure/login@v1 + with: + creds: "${{ secrets.azureCredentials }}" + enable-AzPSSession: true + + # Script is adapted from https://github.com/Andrews-McMeel-Universal/azure-content-management_tool/blob/main/Scripts/rundeck/Flush-RedisCache.ps1 + - name: Clear Redis Cache + uses: azure/powershell@v1 + with: + inlineScript: | + # Check to see if Azure Redis Cache PowerShell Module is installed + Install-Module -Name Az.RedisCache -Confirm:$false + + # Get list of Redis instances using tags + $Redis = Get-AzRedisCache -ResourceGroupName "$ResourceGroupName" -Name "$RedisCacheName" | Where-Object { $_.Tag.Values -eq "${{ inputs.environment }}" } + + # Loop through Redis instances + $Redis | ForEach-Object { + $RedisHostname = $_.HostName + $RedisPort = $_.Port + $RedisName = $_.Name + $RedisKey = (Get-AzRedisCacheKey -Name $RedisName -ResourceGroupName "$ResourceGroupName").PrimaryKey + + # Authenticate using Redis key and flush cache database + $RedisCommands = "AUTH $RedisKey + SELECT 0 + FLUSHDB" + $RedisResult = $RedisCommands | redis-cli -h "$RedisHostname" -p "$RedisPort" + + # Check if output from cache clearing command has correct status message + if ($RedisResult -match 'OK') { + Write-Host "Successfully flushed cache for $RedisHostname" -ForegroundColor Green + } + else { + Write-Error "Failed to clear cache for $RedisHostname" + } + } + azPSVersion: "latest" + From 3481bf1ffeb75fd079a56249071297e0d5a4c4a6 Mon Sep 17 00:00:00 2001 From: ebronson68 <111298136+ebronson68@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:22:20 -0500 Subject: [PATCH 2/5] Added resourceGroupName input --- .github/workflows/clear-azure-redis-cache.yaml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/clear-azure-redis-cache.yaml b/.github/workflows/clear-azure-redis-cache.yaml index c10c95ad..3ec75f3a 100644 --- a/.github/workflows/clear-azure-redis-cache.yaml +++ b/.github/workflows/clear-azure-redis-cache.yaml @@ -7,12 +7,17 @@ on: required: true type: string description: "Environment to clear caches for." + resourceGroupName: + default: "AMU_DevOps_RG" + type: string + description: "Name of Resource Group that the Azure Redis Instance is in." secrets: azureCredentials: required: true jobs: - clear_cache: + clear-redis-cache: + name: Clear Azure Redis Cache runs-on: ubuntu-latest steps: - name: Checkout @@ -47,10 +52,10 @@ jobs: with: inlineScript: | # Check to see if Azure Redis Cache PowerShell Module is installed - Install-Module -Name Az.RedisCache -Confirm:$false + Install-Module -Name Az.RedisCache -Confirm:$false -Force # Get list of Redis instances using tags - $Redis = Get-AzRedisCache -ResourceGroupName "$ResourceGroupName" -Name "$RedisCacheName" | Where-Object { $_.Tag.Values -eq "${{ inputs.environment }}" } + $Redis = Get-AzRedisCache -ResourceGroupName "${{ inputs.resourceGroupName }}" | Where-Object { $_.Tag.Values -eq "${{ inputs.environment }}" } # Loop through Redis instances $Redis | ForEach-Object { From afababc904df46c7521e6ab80cc6dce6228f8763 Mon Sep 17 00:00:00 2001 From: ebronson68 <111298136+ebronson68@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:25:21 -0500 Subject: [PATCH 3/5] Fix resourceGroupName input usage --- .github/workflows/clear-azure-redis-cache.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/clear-azure-redis-cache.yaml b/.github/workflows/clear-azure-redis-cache.yaml index 3ec75f3a..c85ff06c 100644 --- a/.github/workflows/clear-azure-redis-cache.yaml +++ b/.github/workflows/clear-azure-redis-cache.yaml @@ -62,7 +62,7 @@ jobs: $RedisHostname = $_.HostName $RedisPort = $_.Port $RedisName = $_.Name - $RedisKey = (Get-AzRedisCacheKey -Name $RedisName -ResourceGroupName "$ResourceGroupName").PrimaryKey + $RedisKey = (Get-AzRedisCacheKey -Name $RedisName -ResourceGroupName "${{ inputs.resourceGroupName }}").PrimaryKey # Authenticate using Redis key and flush cache database $RedisCommands = "AUTH $RedisKey From 3646b33e21346b21cab98003eba1af45c414fa17 Mon Sep 17 00:00:00 2001 From: ebronson68 <111298136+ebronson68@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:29:02 -0500 Subject: [PATCH 4/5] Add step to install redis-tools apt package --- .github/workflows/clear-azure-redis-cache.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/clear-azure-redis-cache.yaml b/.github/workflows/clear-azure-redis-cache.yaml index c85ff06c..e1b94bde 100644 --- a/.github/workflows/clear-azure-redis-cache.yaml +++ b/.github/workflows/clear-azure-redis-cache.yaml @@ -46,6 +46,12 @@ jobs: creds: "${{ secrets.azureCredentials }}" enable-AzPSSession: true + - name: Install Dependencies + uses: awalsh128/cache-apt-pkgs-action@v1 + with: + packages: redis-tools + version: 1.0 + # Script is adapted from https://github.com/Andrews-McMeel-Universal/azure-content-management_tool/blob/main/Scripts/rundeck/Flush-RedisCache.ps1 - name: Clear Redis Cache uses: azure/powershell@v1 From fafb9990bbb1ed50273436987f02977f68c14741 Mon Sep 17 00:00:00 2001 From: ebronson68 <111298136+ebronson68@users.noreply.github.com> Date: Thu, 27 Jul 2023 12:06:34 -0500 Subject: [PATCH 5/5] Use new cache-azure-login action --- .../workflows/clear-azure-redis-cache.yaml | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/.github/workflows/clear-azure-redis-cache.yaml b/.github/workflows/clear-azure-redis-cache.yaml index e1b94bde..aad84adf 100644 --- a/.github/workflows/clear-azure-redis-cache.yaml +++ b/.github/workflows/clear-azure-redis-cache.yaml @@ -23,28 +23,10 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Hash azureCredentials secret - uses: pplanel/hash-calculator-action@v1.3.1 - id: hash + - name: Login to Azure + uses: Andrews-McMeel-Universal/cache-azure-login@v1 with: - input: ${{ secrets.azureCredentials }} - method: MD5 - - - name: Cache Azure credentials - id: azure-cache - uses: actions/cache@v3 - with: - path: | - ~/.Azure - ${{ github.workspace }}/.Azure - key: ${{ runner.os }}-azurecreds-${{ steps.hash.outputs.digest }} - - - name: Login via Az module - if: steps.azure-cache.outputs.cache-hit != 'true' - uses: azure/login@v1 - with: - creds: "${{ secrets.azureCredentials }}" - enable-AzPSSession: true + azureCredentials: "${{ secrets.azureCredentials }}" - name: Install Dependencies uses: awalsh128/cache-apt-pkgs-action@v1