Skip to content

Commit

Permalink
[DEVOPS-242] add clear redis caches workflow (#46)
Browse files Browse the repository at this point in the history
<details open>
<summary><a href="https://amuniversal.atlassian.net/browse/DEVOPS-242"
title="DEVOPS-242" target="_blank">DEVOPS-242</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
<td>DevOps: Create GitHub Actions workflow for clearing Azure Redis
Caches</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Story"
src="https://amuniversal.atlassian.net/images/icons/issuetypes/story.png"
/>
        Story
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Progress</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
<td><a
href="https://amuniversal.atlassian.net/issues?jql=project%20%3D%20DEVOPS%20AND%20labels%20%3D%20ServiceDesk%20ORDER%20BY%20created%20DESC"
title="ServiceDesk">ServiceDesk</a></td>
    </tr>
  </table>
</details>
<!--
do not remove this marker as it will break action-jira-linter's
functionality.
  added_by_jira_lint
-->
---

<!--
We appreciate the effort for this pull request but before that please
make sure you read the contribution guidelines, then fill out the blanks
below.

Please format the PR title appropriately based on the type of change:
  [<issue>]: <description>
Where <issue> is the related Jira Issue Key.
-->

## Description

- Added workflow to clear Redis caches based off environment

## Related Issues

<!-- List any related Jira issues here -->

- Jira Issue: DEVOPS-242
- Testing Environment: [![Clear Azure Redis
Cache](https://github.com/Andrews-McMeel-Universal/reusable_workflows-test/actions/workflows/clear-azure-redis-cache.yml/badge.svg)](https://github.com/Andrews-McMeel-Universal/reusable_workflows-test/actions/workflows/clear-azure-redis-cache.yml)
  • Loading branch information
ebronson68 authored Aug 8, 2023
1 parent 23f1e5d commit 0705093
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/clear-azure-redis-cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Clear Azure Redis Cache

on:
workflow_call:
inputs:
environment:
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-redis-cache:
name: Clear Azure Redis Cache
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Login to Azure
uses: Andrews-McMeel-Universal/cache-azure-login@v1
with:
azureCredentials: "${{ secrets.azureCredentials }}"

- 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
with:
inlineScript: |
# Check to see if Azure Redis Cache PowerShell Module is installed
Install-Module -Name Az.RedisCache -Confirm:$false -Force
# Get list of Redis instances using tags
$Redis = Get-AzRedisCache -ResourceGroupName "${{ inputs.resourceGroupName }}" | 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 "${{ inputs.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"

0 comments on commit 0705093

Please sign in to comment.