Alert Resource Costs #109
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: Alert Resource Costs | |
on: | |
schedule: | |
- cron: '0 19 * * *' # Run daily at 7:00 PM UTC/3:00 PM EST | |
env: | |
ALERT_THRESHOLD: 60 | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
jobs: | |
alert_costs: | |
strategy: | |
fail-fast: false | |
matrix: | |
rg: [prime-data-hub-demo1, prime-data-hub-demo2, prime-data-hub-demo3] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out changes | |
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 | |
- name: Login into Azure | |
uses: ./.github/actions/vpn-azure | |
with: | |
sp-creds: ${{ secrets.SERVICE_PRINCIPAL_CREDS }} | |
- name: Run Az Cost CLI | |
id: az-cost | |
uses: ./.github/actions/az-cost | |
with: | |
rg: ${{ matrix.rg }} | |
days_ago: 1 | |
total_format: summary | |
- name: Get Last Pusher and Last Change Date | |
id: last-pusher | |
run: | | |
branch_name=$(echo "${{ matrix.rg }}" | sed 's/prime-data-hub-//') | |
last_pusher=$(gh api repos/CDCgov/prime-reportstream/commits/$branch_name --jq '.author.login') | |
last_change_date=$(gh api repos/CDCgov/prime-reportstream/commits/$branch_name --jq '.commit.author.date') | |
echo "username=$last_pusher" >> $GITHUB_OUTPUT | |
echo "last_change_date=$last_change_date" >> $GITHUB_OUTPUT | |
- name: Check Environment Age | |
id: env-age | |
run: | | |
current_date=$(date +%s) | |
last_change_date=$(date -d "${{ steps.last-pusher.outputs.last_change_date }}" +%s) | |
age_in_days=$(( (current_date - last_change_date) / 86400 )) | |
echo "age_in_days=$age_in_days" >> $GITHUB_OUTPUT | |
- name: Slack Notification | |
if: ${{ fromJSON(steps.az-cost.outputs.result) > fromJSON(env.ALERT_THRESHOLD) && fromJSON(steps.env-age.outputs.age_in_days) > 2 }} | |
uses: ./.github/actions/notifications | |
with: | |
method: slack | |
title: Azure Costs | |
message: | | |
Resource Group \`${{ matrix.rg }}\` is exceeding the cost threshold and is ${{ steps.env-age.outputs.age_in_days }} days old. | |
If still running and no longer needed, please [destroy](https://github.com/CDCgov/prime-reportstream/actions/workflows/destroy_demo_environment.yml). | |
* **Cost per day: 💲${{ steps.az-cost.outputs.result }}** | |
* **Provisioned by: \`${{ steps.last-pusher.outputs.username }}\`** | |
* **Last Change Date: \`${{ steps.last-pusher.outputs.last_change_date }}\`** | |
icon-emoji: ':money_with_wings:' | |
channel: prime-reportstream-engineering | |
webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }} | |
color: failure | |
slackify-markdown: true |