How can I make a Python Flask API stored in GIthub Repository deploy at Google Functions?

Question:

I have a Github organization where I store repositories.

All repositories are programmed in Python. I want to use Github actions in order to push the code that I have in a repository to Google Cloud Functions.

I have a simple flask API with a single route that I want to deploy on Google Cloud Functions.

main.py

import os

from flask import Flask, send_from_directory
from flask_cors import CORS

from src.api.my_route import my_route

app = Flask(__name__)

cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'


# Routes
@app.route('/api/my_route', methods=['GET'])
def __my_route():
    return my_route()


# - Main start ----------------------------------------------------------------
if __name__ == "__main__":
    # Start app
    _port = 5001
    app.run(debug=True, host="0.0.0.0", port=_port)

my_route.py

def my_route():
    return {
               "message": "This is my route",
               "data": None,
               "error": "OK"
           }, 200

In order to do this I have done the following:

1. Create a Goolge Console Project here:

https://console.cloud.google.com/welcome

And notet the "Project ID" from that site.

2. Created workload identity pool:

gcloud iam workload-identity-pools create "engineering-pool" --project="engineering-infra" --location="global" --display-name="engineering pool"

3. Created workload identity pool provider:

gcloud iam workload-identity-pools providers create-oidc "github-provider" --project="engineering-infra" --location="global" --workload-identity-pool="engineering-pool" --display-name="Github provider" --attribute-mapping="google.subject=assertion.sub,attribute.actor=assertion.actor,attribute.aud=assertion.aud" --issuer-uri="https://token.actions.githubusercontent.com"

4. Updated IAM policy for serviceAccount:

gcloud iam service-accounts add-iam-policy-binding "[email protected]" --project="engineering-infra" --role="roles/iam.workloadIdentityUser" --member="principalSet://iam.googleapis.com/projects/XXXXXXXX/locations/global/workloadIdentityPools/engineering-pool/attribute.repository/my-org/my-repo"

Generated a yaml file inside my Github repository:

.github/workflows/google-cloudfunction-docker.yaml

jobs:
  job_id:
    runs-on: ubuntu-latest
    permissions:
      contents: 'read'
      id-token: 'write'

    steps:
    - uses: actions/checkout@v3

    - id: auth
      uses: google-github-actions/auth@v0
      with:
        workload_identity_provider: 'projects/XXX/locations/global/workloadIdentityPools/engineering-pool/providers/my-provider'
        service_account: '[email protected]'

    - id: 'deploy'
      uses: 'google-github-actions/deploy-cloud-functions@v0'
      with:
        name: 'my-flask-api'
        runtime: 'python3.10'

    # Example of using the output
    - id: 'test'
      run: 'curl "${{ steps.deploy.outputs.url }}"'

Github Actions gives the following error:

Error: .github#L1
No event triggers defined in `on`
Asked By: Europa

||

Answers:

From your shared yaml and the error message, looks like you are missing a trigger action e.g:

on: push
Answered By: okwonks
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.