0.1.4 • Published 5 months ago

gcp-functions v0.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

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

OptionDescriptionDefault
functionsDirThe directory where the functions are located./functions
buildDirThe directory where the functions will be built./functions/build
runtimeThe runtime to use for the functionsnodejs20
regionThe region to deploy the functions toeurope-west6
envAn array of environment variables to pass to the functions[]
prefixA prefix to add to the name of the functions in gcp''
generationThe generation of the functions(gcloud default)
memoryThe memory to allocate to the functions(gcloud default)
timeoutThe timeout of the functions(gcloud default)
maxInstancesThe maximum number of instances of the functions(gcloud default)
0.1.0

5 months ago

0.1.2

5 months ago

0.0.3

5 months ago

0.1.1

5 months ago

0.0.2

5 months ago

0.0.8

5 months ago

0.1.4

5 months ago

0.0.5

5 months ago

0.1.3

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.1

6 months ago