Giter VIP home page Giter VIP logo

gcloud-function-demo-py's Introduction

Google Cloud Function Demo

Example of github-action that automatically deploy flask server to google cloud function

Main Purpose

  • Simple flask server with simple test just for demo
  • Github action to automatically run unit test on each push and pull request
  • Github action to automatically deploy to google cloud function after each tag push

Directory Structure

  • Program
    • main.py: route
    • core.py: core logic
    • test.py: test for core logic
    • requirements.txt: dependencies
  • Environmet Variable
    • .envrc: local shell
    • .gcloudenv.yaml: google cloud function
  • Settings of ignore files
    • .gitignore: ignore files of git
    • .gcloudignore: ignore files of google cloud function
  • Others
    • .github/workflows/workflow.yaml: script of github action
    • Makefile: some handful script, readme.md

Google Cloud Settings

  1. Add a service account with these IAM
    • Cloud Functions Developer
    • Service Account user
  2. Generate key from this service account and
    • Run cat service_account_key.json | base64
  3. Set google cloud secret to github
    • secrets.GCP_PROJECT_ID: Your google cloud project id
    • secrets.GCP_SA_KEY: The base64 code generated from service account key from previous step
  4. After first deployment
    • Remove default service account from this function
    • Add allUser to this cloud function
  • Step 1, 2, 3: To grant google cloud function deployment privilege to github action.
  • Step 4: Give the least permission and let this function be public accessible

Github Action Settings

See .github/workflows/workflow.yaml

  • jobs.test runs unit test on every push and pull request (including tag push)
  • jobs.deploy runs deploy on every tag push

Development

  • Add route at main.py, Add core logic at core.py, Add unit test for core at test.py
  • Run make test for unit test
  • Run python main.py to start local test server
  • Run curl "http://127.0.0.1:5000/entry?str1=hello&str2=world" to test server on local
  • Git add, commit, push to trigger CI to run unit-test
  • Git tag v0.1, push tag to trigger CI to run unit-test and deploy to google cloud function
  • Run curl "https://PRODUCTION_URL/entry?str1=hello&str2=world" to test server on production

gcloud-function-demo-py's People

Contributors

cwza avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.