Skip to content

Commit

Permalink
Test github action.
Browse files Browse the repository at this point in the history
  • Loading branch information
everaldorodrigo committed Sep 25, 2024
1 parent 7f7dc34 commit 346d3e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/check_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import boto3
import os
import requests

from datetime import datetime


# Create the expected file name
today_date = datetime.today().strftime("%Y%m%d")
expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip"

# Create the S3 client
s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION"))

# Try to fetch the file metadata
try:
s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file)
print(f"Backup file {expected_file} exists, no action needed")
except s3_client.exceptions.ClientError:
print(f"Backup file {expected_file} does NOT exist.")

# Create the payload for Slack
slack_data = {
"channel": os.getenv("SLACK_CHANNEL"),
"username": os.getenv("APPLICATION_NAME"),
"icon_emoji": ":thumbsdown:",
"text": f":alert: Backup file {expected_file} does NOT exist.",
}

try:
response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10)
if response.status_code == 200:
print(" └─ Slack notification sent successfully.")
else:
print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}")
except requests.exceptions.Timeout:
print(" └─ Request timed out to Slack WebHook URL.")
except requests.exceptions.RequestException as e:
print(f" └─ Failed to send Slack notification. Error: {str(e)}")
40 changes: 1 addition & 39 deletions .github/workflows/check_backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
pip install boto3
- name: Check if backup exists in S3
id: check_s3_backup
run: python check_backup.py
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
Expand All @@ -32,41 +32,3 @@ jobs:
S3_FOLDER: "db_backup/"
SLACK_CHANNEL: "#observability-test"
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
import boto3
import os
import requests
from datetime import datetime
# Create the expected file name
today_date = datetime.utcnow().strftime("%Y%m%d")
expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip"
# Create the S3 client
s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION"))
# Try to fetch the file metadata
try:
s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file)
print(f"Backup file {expected_file} exists, no action needed")
except s3_client.exceptions.ClientError:
print(f"Backup file {expected_file} does NOT exist.")
# Create the payload for Slack
slack_data = {
"channel": os.getenv("SLACK_CHANNEL"),
"username": os.getenv("APPLICATION_NAME"),
"icon_emoji": ":thumbsdown:",
"text": ":alert: Backup file {expected_file} does NOT exist.",
}
try:
response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10)
if response.status_code == 200:
print(" └─ Slack notification sent successfully.")
else:
print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}")
except requests.exceptions.Timeout:
print(" └─ Request timed out to Slack WebHook URL.")
except requests.exceptions.RequestException as e:
print(f" └─ Failed to send Slack notification. Error: {str(e)}")

0 comments on commit 346d3e2

Please sign in to comment.