diff --git a/.github/actions/main-action/action.yml b/.github/actions/main-action/action.yml index 60a5dedd..2a841148 100644 --- a/.github/actions/main-action/action.yml +++ b/.github/actions/main-action/action.yml @@ -127,6 +127,8 @@ runs: S3_BUCKET=${{ inputs.s3-bucket }} AWS_REGION=${{ inputs.aws-region }} TWILIO_SERVERLESS_API_CONCURRENCY=1 + GOOGLE_SHEETS_CREDENTIALS=${{ inputs.GOOGLE_SHEETS_CREDENTIALS }} + GOOGLE_SHEET_ID=${{ inputs.GOOGLE_SHEET_ID }} EOT shell: bash # Can be overridden in custom action @@ -174,13 +176,41 @@ runs: working-directory: ${{ github.action_path }} shell: bash - # Send Slack notifying success - - name: Slack Aselo channel - id: slack-parallel - uses: slackapi/slack-github-action@v1.14.0 - if: ${{ inputs.send-slack-message != 'false' }} + # # Send Slack notifying success + # - name: Slack Aselo channel + # id: slack-parallel + # uses: slackapi/slack-github-action@v1.14.0 + # if: ${{ inputs.send-slack-message != 'false' }} + # with: + # channel-id: ${{ env.ASELO_DEPLOYS_CHANNEL_ID }} + # slack-message: "`[Serverless]` Deployment to `${{ env.helpline-name }}` from ${{ github.ref_type }} `${{ github.ref_name }}` requested by `${{ github.triggering_actor }}` completed using workflow '${{ github.workflow }}' with SHA ${{ github.sha }} :rocket:." + # env: + # SLACK_BOT_TOKEN: ${{ env.GITHUB_ACTIONS_SLACK_BOT_TOKEN }} + # Update Google Sheets + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Python + uses: actions/setup-python@v3 with: - channel-id: ${{ env.ASELO_DEPLOYS_CHANNEL_ID }} - slack-message: "`[Serverless]` Deployment to `${{ env.helpline-name }}` from ${{ github.ref_type }} `${{ github.ref_name }}` requested by `${{ github.triggering_actor }}` completed using workflow '${{ github.workflow }}' with SHA ${{ github.sha }} :rocket:." + python-version: '3.8' + - name: Install dependencies + shell: bash + run: | + python -m pip install --upgrade pip + pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client gspread + - name: Update Google Sheets + shell: bash env: - SLACK_BOT_TOKEN: ${{ env.GITHUB_ACTIONS_SLACK_BOT_TOKEN }} + GOOGLE_SHEETS_CREDENTIALS: ${{ inputs.GOOGLE_SHEETS_CREDENTIALS }} + GOOGLE_SHEET_ID: ${{ inputs.GOOGLE_SHEET_ID }} + helpline: ${{ inputs.helpline-code }} + environments: ${{ inputs.environment-code }} + environment: ${{ inputs.environment }} + github_ref: ${{ github.ref }} + github_sha: ${{ github.sha }} + github_actor: ${{ github.triggering_actor }} + github_branch: ${{ github.ref_name }} + # aws_region: ${{ secrets.AWS_DEFAULT_REGION }} + run: | + echo "${GOOGLE_SHEETS_CREDENTIALS}" > credentials.json + python update_matrix.py \ No newline at end of file diff --git a/.github/workflows/deploy-multiple-accounts.yml b/.github/workflows/deploy-multiple-accounts.yml index 3a3770dc..dab18b65 100644 --- a/.github/workflows/deploy-multiple-accounts.yml +++ b/.github/workflows/deploy-multiple-accounts.yml @@ -66,7 +66,6 @@ jobs: aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ secrets.AWS_DEFAULT_REGION }} # Get AWS parameters to setup environment variables for Serverless function - # Get AWS parameters to setup environment variables for Serverless function - name: Set Twilio Account SID uses: marvinpinto/action-inject-ssm-secrets@latest with: @@ -192,6 +191,8 @@ jobs: # Set 'false' if the target environment is production OR the force_enable_operating_hours override option is checked - otherwise 'true' disable-operating-hours: ${{ (inputs.force_enable_operating_hours == 'true' || matrix.environment_code == 'PROD') && 'false' || 'true' }} send-slack-message: ${{ inputs.send_slack_message_per_deploy }} + GOOGLE_SHEETS_CREDENTIALS: ${{ secrets.GOOGLE_SHEETS_CREDENTIALS }} + GOOGLE_SHEET_ID: ${{ secrets.GOOGLE_SHEET_ID }} # Send Slack notifying success send-slack-message: # The type of runner that the job will run on @@ -219,7 +220,6 @@ jobs: with: ssm_parameter: 'ASELO_DEPLOYS_CHANNEL_ID' env_variable_name: 'ASELO_DEPLOYS_CHANNEL_ID' - - name: Slack Aselo channel id: slack uses: slackapi/slack-github-action@v1.26.0 diff --git a/update_matrix.py b/update_matrix.py new file mode 100644 index 00000000..6bfa821c --- /dev/null +++ b/update_matrix.py @@ -0,0 +1,79 @@ +# Copyright (C) 2021-2023 Technology Matters +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see https://www.gnu.org/licenses/. + +import os +import json +import sys +import gspread +from google.oauth2.service_account import Credentials +from google.auth.exceptions import GoogleAuthError +from datetime import datetime + +def main(): + try: + # Load credentials from the environment variable + credentials_json = os.getenv('GOOGLE_SHEETS_CREDENTIALS') + sheet_id = os.getenv('GOOGLE_SHEET_ID') + + if not credentials_json: + raise ValueError("Credentials JSON not found in environment variable.") + if not sheet_id: + raise ValueError("Sheet ID not found in environment variable.") + + credentials_data = json.loads(credentials_json) + print("Credentials loaded successfully.") + + # Define the scope and credentials + scopes = [ + 'https://www.googleapis.com/auth/spreadsheets', + 'https://www.googleapis.com/auth/drive.file' + ] + credentials = Credentials.from_service_account_info(credentials_data, scopes=scopes) + print("Credentials initialized successfully.") + + client = gspread.authorize(credentials) + print("Google Sheets client authorized successfully.") + + sheet = client.open_by_key(sheet_id).worksheet("Deploys") + print("Google Sheet opened successfully.") + + current_date = datetime.now().strftime("%m/%d/%Y") + current_time = datetime.now().strftime("%H:%M:%S") + + helpline = os.getenv('helpline') + environment = os.getenv('environment') + github_sha = os.getenv('github_sha') + github_actor = os.getenv('github_actor') + github_branch = os.getenv('github_branch') + # aws_region = os.getenv('aws_region') + + print("Environment variables loaded successfully.") + + hl_env = helpline + "_" + environment + + new_row = [current_date, current_time, "serverless", hl_env, github_branch, github_actor, github_sha] + append_row = sheet.append_row(new_row) + print("Row added to Deploys sheet.") + + if not append_row: + raise RuntimeError("Failed to add row to Deploys sheet.") + + print("Google Sheet updated successfully.") + + except (GoogleAuthError, ValueError, RuntimeError, json.JSONDecodeError) as e: + print(f"Error: {e}") + sys.exit(1) + +if __name__ == '__main__': + main()