|
1 |
| -import os |
2 |
| -import sys |
3 |
| -import json |
4 |
| -from datetime import datetime |
5 |
| - |
6 |
| -try: |
7 |
| - import pandas as pd |
8 |
| - from google.oauth2 import service_account |
9 |
| - from googleapiclient.discovery import build |
10 |
| - from googleapiclient.errors import HttpError |
11 |
| -except ImportError as e: |
12 |
| - print(f"Error: {e}. Please make sure all required packages are installed.") |
13 |
| - sys.exit(1) |
14 |
| - |
15 |
| -# Set up credentials |
16 |
| -try: |
17 |
| - gcp_sa_key = os.environ['GCP_SA_KEY'] |
18 |
| - service_account_info = json.loads(gcp_sa_key) |
19 |
| - creds = service_account.Credentials.from_service_account_info( |
20 |
| - service_account_info, |
21 |
| - scopes=['https://www.googleapis.com/auth/spreadsheets'] |
22 |
| - ) |
23 |
| -except KeyError: |
24 |
| - print("Error: GCP_SA_KEY environment variable not set.") |
25 |
| - sys.exit(1) |
26 |
| -except json.JSONDecodeError: |
27 |
| - print("Error: Invalid JSON in GCP_SA_KEY environment variable.") |
28 |
| - sys.exit(1) |
29 |
| - |
30 |
| -# Build the Sheets API service |
31 |
| -service = build('sheets', 'v4', credentials=creds) |
32 |
| - |
33 |
| -# Get the Sheet ID from environment variable |
34 |
| -SHEET_ID = os.environ.get('GOOGLE_SHEET_ID') |
35 |
| -if not SHEET_ID: |
36 |
| - print("Error: GOOGLE_SHEET_ID environment variable not set or empty.") |
37 |
| - sys.exit(1) |
38 |
| - |
39 |
| -print(f"Using Sheet ID: {SHEET_ID}") |
40 |
| - |
41 |
| -# Read the existing merged CSV file |
42 |
| -csv_file = 'public/merged.csv' |
43 |
| -try: |
44 |
| - df = pd.read_csv(csv_file) |
45 |
| - if df.empty: |
46 |
| - raise ValueError("CSV file is empty") |
47 |
| -except FileNotFoundError: |
48 |
| - print(f"Error: CSV file '{csv_file}' not found.") |
49 |
| - sys.exit(1) |
50 |
| -except (pd.errors.EmptyDataError, ValueError): |
51 |
| - print(f"Error: CSV file '{csv_file}' is empty.") |
52 |
| - sys.exit(1) |
53 |
| -except pd.errors.ParserError: |
54 |
| - print(f"Error: Unable to parse CSV file '{csv_file}'.") |
55 |
| - sys.exit(1) |
56 |
| - |
57 |
| -# Convert DataFrame to list of lists |
58 |
| -values = [df.columns.tolist()] + df.values.tolist() |
59 |
| - |
60 |
| -# Sheet name with current date |
61 |
| -current_date = datetime.now().strftime("%Y-%m-%d") |
62 |
| -sheet_name = f'Jobs {current_date}' |
63 |
| - |
64 |
| -# Update or create sheet |
65 |
| -try: |
66 |
| - sheet_metadata = service.spreadsheets().get(spreadsheetId=SHEET_ID).execute() |
67 |
| - sheets = sheet_metadata.get('sheets', '') |
68 |
| - sheet_exists = any(sheet['properties']['title'] == sheet_name for sheet in sheets) |
69 |
| - |
70 |
| - if sheet_exists: |
71 |
| - service.spreadsheets().values().clear( |
72 |
| - spreadsheetId=SHEET_ID, |
73 |
| - range=f'{sheet_name}!A1:Z' |
74 |
| - ).execute() |
75 |
| - else: |
76 |
| - service.spreadsheets().batchUpdate( |
77 |
| - spreadsheetId=SHEET_ID, |
78 |
| - body={'requests': [{'addSheet': {'properties': {'title': sheet_name}}}]} |
79 |
| - ).execute() |
80 |
| - |
81 |
| - # Update sheet with new data |
82 |
| - service.spreadsheets().values().update( |
83 |
| - spreadsheetId=SHEET_ID, |
84 |
| - range=f'{sheet_name}!A1', |
85 |
| - valueInputOption='RAW', |
86 |
| - body={'values': values} |
87 |
| - ).execute() |
88 |
| - print(f"Google Sheets updated successfully with merged data in sheet: {sheet_name}") |
89 |
| - |
90 |
| -except HttpError as e: |
91 |
| - print(f"HTTP Error occurred: {e}") |
92 |
| - print(f"Response content: {e.content}") |
93 |
| - sys.exit(1) |
94 |
| -except Exception as e: |
95 |
| - print(f"Error updating Google Sheets: {e}") |
96 |
| - sys.exit(1) |
| 1 | +name: Update Google Sheets |
| 2 | + |
| 3 | +on: |
| 4 | + repository_dispatch: |
| 5 | + types: [update-sheets] |
| 6 | + |
| 7 | +jobs: |
| 8 | + update-sheets: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Checkout code |
| 12 | + uses: actions/checkout@v4 |
| 13 | + |
| 14 | + - name: Set up Python |
| 15 | + uses: actions/setup-python@v4 |
| 16 | + with: |
| 17 | + python-version: "3.x" |
| 18 | + |
| 19 | + - name: Cache Python dependencies |
| 20 | + uses: actions/cache@v3 |
| 21 | + with: |
| 22 | + path: ~/.cache/pip |
| 23 | + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} |
| 24 | + restore-keys: | |
| 25 | + ${{ runner.os }}-pip- |
| 26 | +
|
| 27 | + - name: Install dependencies |
| 28 | + run: | |
| 29 | + python -m pip install --upgrade pip |
| 30 | + pip install -r requirements.txt |
| 31 | +
|
| 32 | + - name: Update Google Sheets |
| 33 | + run: python update_sheets.py |
| 34 | + env: |
| 35 | + GCP_SA_KEY: ${{ secrets.GCP_SA_KEY }} |
| 36 | + GOOGLE_SHEET_ID: ${{ secrets.GOOGLE_SHEET_ID }} |
| 37 | + |
| 38 | + - name: Debug Information |
| 39 | + if: failure() |
| 40 | + run: | |
| 41 | + echo "GCP_SA_KEY is set: ${{ secrets.GCP_SA_KEY != '' }}" |
| 42 | + echo "GOOGLE_SHEET_ID is set: ${{ secrets.GOOGLE_SHEET_ID != '' }}" |
0 commit comments