gcp-functions
v0.1.4
Published
#### A package to help with deploying and managing Google Cloud Functions.
Downloads
16
Readme
gcp-functions
A package to help with deploying and managing Google Cloud Functions.
Getting started
Setup Google Cloud
Add .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
MY_ENV_VALUE: ${{ secrets.MY_ENV_VALUE }}
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Login
uses: 'google-github-actions/auth@v1'
with:
token_format: 'access_token'
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
- name: Install dependencies
run: npm i
- name: Build GCP functions
run: npm run gcp-functions build
- name: Deploy GCP functions
run: npm run gcp-functions deploy
Create service account
Go to google cloud console and create a service account named "Deploy Service Account" (IAM => Service Account) with the following roles:
- Cloud Functions Admin
- Service Account Token Creator
- Service Account User
- Cloud Functions Admin
Then go to "Keys" and create a new one. Add it to github secrets as "GCP_CREDENTIALS".
Enable these APIs in Google Cloud Console:
- Cloud Functions API
- Cloud Build API
- Cloud Run API (if using gen2 functions)
Install the package:
npm i gcp-functions
Add the following scripts to your package.json
:
{
"scripts": {
"functions:build": "npx gcp-functions build",
"functions:serve": "npx gcp-functions serve",
"functions:deploy": "npx gcp-functions deploy"
}
}
Directory structure
- functions/
- function1.ts
- function2.ts
Content of the functions
import { Request, Response } from '@google-cloud/functions-framework'
export const contact = async (req: Request, res: Response) => {
// ...
}
Config file
Create a .gcp-functions
file at the root of your project:
{
"functionsDir": "./functions",
"buildDir": "./functions/build",
"runtime": "nodejs20",
"region": "europe-west6",
"env": [],
}
Config options
| Option | Description | Default |
| ------------ | ---------------------------------------------------------- | ------------------- |
| functionsDir | The directory where the functions are located | ./functions
|
| buildDir | The directory where the functions will be built | ./functions/build
|
| runtime | The runtime to use for the functions | nodejs20
|
| region | The region to deploy the functions to | europe-west6
|
| env | An array of environment variables to pass to the functions | []
|
| prefix | A prefix to add to the name of the functions in gcp | ''
|
| generation | The generation of the functions | (gcloud default) |
| memory | The memory to allocate to the functions | (gcloud default) |
| timeout | The timeout of the functions | (gcloud default) |
| maxInstances | The maximum number of instances of the functions | (gcloud default) |