Skip to content

Instantly share code, notes, and snippets.

Avatar

Grant Timmerman grant

View GitHub Profile
@grant
grant / setup_iam.sh
Created May 16, 2021
A script that sets up Workflows IAM for a GitHub Action
View setup_iam.sh
# Add secret for project
PROJECT=$(gcloud config get-value project)
gh secret set GCP_PROJECT_ID -b $PROJECT
# Create service account
SERVICE_ACCOUNT=my-wf-service-account
gcloud iam service-accounts create $SERVICE_ACCOUNT
gcloud projects add-iam-policy-binding $PROJECT \
--member "serviceAccount:$SERVICE_ACCOUNT@$PROJECT.iam.gserviceaccount.com" \
--role "roles/workflows.editor"
@grant
grant / deploy-cloud-workflow.yaml
Last active May 18, 2021
A GitHub Workflow that deploys a Cloud Workflow
View deploy-cloud-workflow.yaml
name: Deploy Cloud Workflow
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy Cloud Workflow
runs-on: ubuntu-18.04
steps:
@grant
grant / myFirstWorkflow.workflows.yaml
Created May 15, 2021
Cloud Workflow – myFirstWorkflow
View myFirstWorkflow.workflows.yaml
- getCurrentTime:
call: http.get
args:
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
result: currentTime
- readWikipedia:
call: http.get
args:
url: https://en.wikipedia.org/w/api.php
query:
@grant
grant / deploy.sh
Created May 4, 2021
Deploy gRPC to Cloud Run
View deploy.sh
# Get Cloud Run URL
ENDPOINT=$(\
gcloud run services list \
--project=${PROJECT} \
--region=${REGION} \
--platform=managed \
--format="value(status.address.url)" \
--filter="metadata.name=grpc-calculator")
ENDPOINT=${ENDPOINT#https://} && echo ${ENDPOINT}
View server.js
function calculate(call, callback) {
const request = call.request;
let result;
if (request.operation === 'ADD') {
result = request.first_operand + request.second_operand;
} else {
result = request.first_operand - request.second_operand;
}
callback(null, {result});
}
@grant
grant / index.js
Created May 4, 2021
Listen to gRPC requests
View index.js
function main() {
const server = new grpc.Server();
server.addService(calculatorProto.Calculator.service, {calculate});
server.bindAsync(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure(), (error, port) => {
if (error) {
throw error;
}
server.start();
});
}
View calculator.proto
enum Operation {
ADD = 0;
SUBTRACT = 1;
}
message BinaryOperation {
float first_operand = 1;
float second_operand = 2;
Operation operation = 3;
};
message CalculationResult {
@grant
grant / cal-gcloud.sh
Created Apr 18, 2021
gcloud eventarc triggers create CAL
View cal-gcloud.sh
export TRIGGER_NAME=events-cal-trigger
export RUN_SERVICE=helloworld-events
export PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')"
gcloud eventarc triggers create $TRIGGER_NAME \
--destination-run-service=$RUN_SERVICE \
--service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \
--event-filters="type=google.cloud.audit.log.v1.written" \
--event-filters="serviceName=cloudfunctions.googleapis.com" \
--event-filters="methodName=google.cloud.functions.v1.CloudFunctionsService.CreateFunction"
@grant
grant / hello_world_http.cc
Created Feb 13, 2021
Hello World, C++ Functions Framework
View hello_world_http.cc
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <nlohmann/json.hpp>
namespace gcf = ::google::cloud::functions;
gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
@grant
grant / deploy.sh
Created Jan 27, 2021
run-workflow-function deploy.sh
View deploy.sh
# Configuration
export PROJECT_ID=$(gcloud config get-value core/project)
# Deploy Workflow
gcloud workflows deploy myFirstWorkflow \
--source=myFirstWorkflow.yaml
# Deploy (private) Function
gcloud functions deploy runWorkflowFunction \
--runtime nodejs12 \