Search code examples
githubgoogle-cloud-functionsfile-structure

Google Cloud Functions: Project layout for Github publishing


I'm starting with Google Cloud Functions, and I want to use a real editor + github instead of the inline editor.

For an existing Google App Engine Flex(php) project, I've modified it so that it publish on PubSub some data about user.

I want to write a set of functions :

  • One function that update BigQuery, and post on a topic that a user data has been updated
  • Subscribed to this update : Several functions would run on query on BigQueries and store the result in Firebase (for a mobile app to display the data statistics)

Here are some basic questions I couldn't find any answer :

  • How can I organise my project? What is the expected layout so that GCP can fetch the project from github and run it ?
  • [Optional] Can I have multiple file for one function to organise my code ?

I've found this : https://github.com/PrinceCornNM/generator-firebase-cloud-functions but I also read that the Function for firebase was somehow incompatible on a stackoverflow post.


Solution

  • Here is how I made it work :

    Setup the Github Repository

    • Create a repo on Githup, checkout from my IDE
    • Create a folder with the name of the function 'processNewTroncQueteur'
    • Create a package.json and index.js in this folder, and copy the content of my working function from Google Cloud Function
    • Commit

    Check my repository as an example : https://github.com/dev-mansonthomas/RedCrossQuestCloudFunctions

    Sync my Github Repo with Google Source Control

    • Go to https://source.cloud.google.com/
    • "New Repository", "Connect external repository"
    • Select your GCP Project, then choose github, check the checkbox for consent, and click "Connect to GitHub" (follow instructions)
    • Choose the project on GitHub, and click "Connect Selected Repository"

    (I had to try twice, the first time I got an error without details)

    Create your Cloud Function from Source Control

    • Go to https://console.cloud.google.com/functions/
    • New Function
    • Choose Name, Memory, Trigger, Runtime, and region (advanced)
    • Choose "Cloud Source Repository" for "Source Code"
    • Repository : Copy/Paste the repo name from "Google Cloud Repository", you can find it, in the upper left corner of the screen, when you're in the repo. For me it's: github_dev-mansonthomas_RedCrossQuestCloudFunctions
    • I used, Branch/Master
    • Directory with source code : the name of the folder 'processNewTroncQueteur'
    • function to execute: 'processNewTroncQueteur'
    • Create, and I've tested it and it works.