Skip to content

Instantly share code, notes, and snippets.

@lcenchew
Last active October 25, 2023 07:11
Show Gist options
  • Save lcenchew/9ce38f34b00f35e090730e8ba8e92dd7 to your computer and use it in GitHub Desktop.
Save lcenchew/9ce38f34b00f35e090730e8ba8e92dd7 to your computer and use it in GitHub Desktop.
Notes - GCP #notes

Google Cloud Platform

CLI

On Cloud Shell

gcloud auth list 

gcloud config list

gcloud config set project <PROJECT_ID>
gcloud config unset project

gcloud projects list

On Local

brew cask install google-cloud-sdk
    source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc"
    source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc"

gcloud init  

gcloud auth login

Cloud Shell Editor

VS Code like online editor

API

Generate Oauth token

Cloud Build

Collection of images for cloud build

Create a cloud-builder image and upload to the Google container registry

gcloud builds submit --config cloudbuild.yaml

Structure of the cloudbuild.yaml

steps:
- id: 'Step name'                       # give this step a name, use in waitFor
  name: 'gcr.io/cloud-builders/bazel'   # container image to use, similar to `docker pull `
        'docker'                        # official image in docker hub
        'google/cloud-sdk'              # in docker hub
        'microsoft/dotnet:sdk'                
        'gcr.io/cloud-marketplace-containers/google/bazel'
        
  args: [string, string, ...]           # args to pass into the container entry
  env: [`KEY=VALUE`, `KEY=VALUE`, ...]  # env to set
  dir: 'examples/hello_world'           # 
  waitFor: ['id', 'id', ...]            # use the id here
  entrypoint: 'node'                    # use a different entrypoint from image default
  secretEnv: string                     # see [Using secrets and credentials](https://cloud.google.com/cloud-build/docs/securing-builds/use-encrypted-secrets-credentials#using_the_encrypted_variable_in_build_requests)
  volumes:                              # volume that persist across steps
  - name: 'vol1'
    path: '/persistent_volume'
  timeout: 500s                         # step timeout
  
- id: 
  name: 'gcr.io/cloud-builders/mvn'
  args: ['install']
  env:
  - `KEY=VALUE`                         # can also write in this way
  - `KEY=VALUE`
  volumes:                              # volume persisted across steps
  - name: 'vol1'
    path: '/persistent_volume'  
  ...
- id: 
  name: string

logsBucket: 'gs://mybucket'             # Cloud Storage bucket to write logs
timeout: 3000s                          # build timeout
options:
  env: 'KEY=VALUE'
  secretEnv: 
  volumes:                              # volume persisted entire build
  - name: 'vol1'
    path: '/persistent_volume'  
  sourceProvenanceHash: ['SHA256']
  machineType: enum(MachineType)        # 'N1_HIGHCPU_8'
  diskSizeGb: '200'                     # max 1000
  substitutionOption: 'ALLOW_LOOSE'
  logStreamingOption: 'STREAM_ON'       # default OFF
  logging: 'GCS_ONLY'                   # default GCS and Logging
                
substitutions:                          # substitute specific variables at build time
  _ANDROID_VERSION: '28'
  _VERSION: '1.0'
tags: ['tag1', 'tag2']
secrets: object(Secret)
images:                                 # Docker images to push to Container Registry
- 'gcr.io/$PROJECT_ID/gcloud'
- 'gcr.io/$PROJECT_ID/gcloud-slim'

artifacts:                              # one or more non-container artifacts to be stored in Cloud Storage
  objects:
    location: 'gs://mybucket/'
    paths: ['my-package']
    

local

Install Cloud Build Local Builder (see also Container Registry setup)

gcloud components install cloud-build-local

cloud-build-local --dryrun=false .

Cloud Function

Cloud Source Repository

Create a repo

gcloud source repos create <reponame>

Cloud Storage

gsutil mb -l ASIA-SOUTHEAST1  gs://

Container Registry

local

Install the Google Container Registry's Docker credential helper

gcloud components install docker-credential-gcr

Make sure Docker is running

docker-credential-gcr configure-docker

Firebase

npm init --yes
npm install -g firebase-tools
firebase login --no-localhost
firebase init

Sample project

git clone https://github.com/rosera/pet-theory.git

Pub/Sub

gcloud pubsub topics create <topic>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment