Deployment Pipeline – GitHub Actions Google Cloud Functions
A few weeks ago I prepared a workshop for a code assignment where the participants should write an integration towards OpanAI’s new assistants API. The coding was done in TypeScript, and to avoid spending time on getting everything up and running and instead being able to focus on the assignment I prepared a GitHub template project that deployed the Google Cloud function with the help of GitHub Actions.
I had a hard time finding any complete examples so let me share what I did.
Prerequisites
- Gmail account
- GitHub account
Google Cloud Setup
If you haven’t already signed up for Google Cloud, browse to Google Cloud and start a new account for free. You are getting 300$ of credits that are valid for 90 days.
I won’t go into all the details on how to set up the correct permissions through code or their UI, instead, we will use the glcoud CLI that when you do a manual deployment of your cloud function, it will also set up the correct permissions for you. If you are doing this for a serious project I would like to recommend using infrastructure as code instead.
Instead of me explaining how to create and deploy the cloud function, you should follow Google’s example on how to create and deploy a simple HTTP function that you can find here – Create a Cloud Function by using the Google Cloud CLI.
Nice, now you should have a Google Cloud Function up and running.
Deployment pipeline
Log in to GitHub, create a new repository, and follow their instructions to push your newly created code to your repository.
Now it’s time to set up the GitHub Action.
Under your root folder create the following file../github/workflows/deploy-function.yml
And then add the following code to it.
name: "Deploy"
on:
push:
branches:
- "main"
jobs:
job_id:
runs-on: "ubuntu-latest"
permissions:
contents: "read"
id-token: "write"
steps:
- uses: "actions/checkout@v4"
- uses: "actions/setup-node@v4"
with:
node-version: "20" # Specify the Node.js version you are using
- name: Load .env file
uses: xom9ikk/dotenv@v2
with:
load-mode: strict
# Install dependencies and build project
- name: "Install Dependencies and Build"
run: |
npm install
npm run build
- id: "auth"
uses: "google-github-actions/auth@v2"
with:
credentials_json: YOUR_CREDENTIALS
- name: Deploy function
run: |
gcloud functions deploy nodejs-http-function
--gen2 \
--region=REGION_YOU_DEPLOYED_TO \
--runtime=nodejs20 \
--source=. \
--entry-point=helloGET \
--trigger-http
Replace the REGION_YOU_DEPLOYED_TO with the region you selected.
And you also need to replace YOUR_CREDENTIALS. Here you will find instructions for setting up Service Account Key JSON.
When you are done, every time you push your code to the main branch, the Google Cloud Function will be deployed.
Good luck and happy coding!