Skip to content

Deploy cloud functions #68

Deploy cloud functions

Deploy cloud functions #68

name: Deploy cloud functions
on:
workflow_call:
inputs:
function_name:
type: string
description: Name of the function directory (e.g. aggregate_hourly_downloads)
required: true
commit_sha:
description: Commit SHA to deploy (leave blank for latest)
required: false
type: string
workflow_dispatch:
inputs:
function_name:
type: string
description: Name of the function directory (e.g. aggregate_hourly_downloads)
required: true
commit_sha:
description: Commit SHA to deploy (leave blank for latest)
required: false
type: string
environment:
type: environment
description: Select the environment
env:
COMMIT_SHA_TO_DEPLOY: ${{ inputs.commit_sha || github.sha }}
permissions:
contents: read
id-token: write
jobs:
deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment || 'development' }}
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Zip function source
# zips all files inside the 'src/' directory into src.zip
run: |
cd "stats-functions/${{ inputs.function_name }}/src"
zip -r src.zip .
mv src.zip "../../../terraform/${{ inputs.function_name }}/src.zip"
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v3
with:
workload_identity_provider: ${{ vars.PROVIDER_NAME }}
service_account: ${{ vars.SERVICE_ACCOUNT }}
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v3
- name: Set up terraform
uses: hashicorp/setup-terraform@v3
- name: Check version
run: terraform --version
- name: Initialize remote backend
run: terraform init -backend-config="bucket=${{ vars.ENV_VAR }}-arxiv-terraform-state"
working-directory: "terraform/${{ inputs.function_name }}"
- name: Format check
run: terraform fmt -check
working-directory: "terraform/${{ inputs.function_name }}"
- name: Apply
run: terraform apply -var-file=envs/${{ vars.ENV_VAR }}.tfvars -var="commit_sha=${{ env.COMMIT_SHA_TO_DEPLOY }}" -auto-approve
working-directory: "terraform/${{ inputs.function_name }}"
- name: Configure Cloud SQL instances on the created Cloud Run instance
# run only if the apply succeeded
if: success()
run: |
# get the kebab-case Cloud Run service name
SERVICE_NAME=$(echo "stats-${{ inputs.function_name }}" | tr '_' '-')
# pull the correct variable of instances from the github environment based on function name
FUNCTION_NAME_UPPER=$(echo "${{ inputs.function_name }}" | tr '[:lower:]' '[:upper:]')
VAR_NAME="INSTANCES_$FUNCTION_NAME_UPPER"
INSTANCES="${!VAR_NAME}"
if [ -n "$INSTANCES" ]; then
echo "Configuring Cloud SQL instances $INSTANCES on service $SERVICE_NAME"
gcloud run services update "$SERVICE_NAME" \
--set-cloudsql-instances "$INSTANCES" \
--region "us-central1"
else
echo "No instances found. Skipping Cloud SQL configuration"
fi
env:
# map all github environment variables into the shell environment
${{ vars }}