Google Cloud Functions

Deploying

Deployment questions and answers

How to access private NPM registries on Artifact Registry? If they are in separate projects you need to give the default cloud build service account Artifact Reader to that repo. Note that this is NOT the service account you're calling gcloud functions deploy with, as gcloud does some fancy use another service account stuff.

Q: so where is the default cloud build service account? A: gcloud functions deploy spits out serviceConfig YAML which lists serviceAccountEmail. This seems to be the default cloud build service account you need

Alternatively, just specify the authorization key for Cloud Build to use ??

Deployment questions and answers Attachments (2)

Best simple examples of deployment pipelines

Q: Any example on doing this via CircleCI and what the account's privs need? A: yes (see a link here)

Best simple examples of deployment pipelines Attachments (1)

What NPM steps does gcloud function deploy run?

From the logs

    npm install --package-lock-only --quiet
    npm ci --quiet
    npm run build   <-- or can run gcp-build, if you have it
    npm prune --production
What NPM steps does gcloud function deploy run? Attachments (2)

Customizing

Customizing build process Node If you have a "gcp-build" build script Google will run that instead of npm run build.

Local testing

Getting it to work via ES modules

command I used: npx @google-cloud/functions-framework --source=dist/main/typescript/index.js --target=composeEndpoint

aka: point the source directory to the compiled javascript, not typescript

make sure that target function is exported all the way in your index.js file at the top level (don't stop mid way!!!)

Getting it to work via ES modules Attachments (1)

Getting a target to work when you really want that to be a function

So you specify a --target to the function-framework as normal

npx @google-cloud/functions-framework --target=MyCloudFunction.app

Somewhere you export this:

// Google's cloud function target parameter really expects a property, not calling a function
// BUT we want this to a bit dynamic, as we don't know, at file read time, if we want an Express
// app up (we might be using this as a CLI)
//
// So make a class, with a static getter function, and all that fancy means we have dynamic
// property - when ES6+ requests the "app" property it automatically executes the function found
// there and returns the result as the value of the property. Which is our Express middleware...
export class MyCloudFunction {
    static get app() {
        const app = express()
        return getRoutes(app)

        return app
    }
}