Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Last active January 3, 2025 07:02
Show Gist options
  • Save DinoChiesa/d42687fa2ffd450fd995abcbdbe851fb to your computer and use it in GitHub Desktop.
Save DinoChiesa/d42687fa2ffd450fd995abcbdbe851fb to your computer and use it in GitHub Desktop.
Instructions for running peloton-to-garmin (https://github.com/philosowaffle/peloton-to-garmin) in Cloud Run, assuming you're doing this from Windows Powershell v7.4.6:
$env:PROJECT_ID="your-gcp-project-id"
$env:REGION="us-west1"
$env:JOB_NAME="peloton-to-garmin"
gcloud config set core/project $env:PROJECT_ID
# create two GCS buckets - https://cloud.google.com/sdk/gcloud/reference/storage/buckets/create
# one for the output, one for the configuration file
gcloud storage buckets create gs://peloton-to-garmin-output --location "$env:REGION" `
--project "$env:PROJECT_ID"
gcloud storage buckets create gs://peloton-to-garmin-config --location "$env:REGION" `
--project "$env:PROJECT_ID"
# copy the config file to the bucket
gsutil -m cp .\secrets\headless-config.json gs://peloton-to-garmin-config/configuration.local.json
# grant rights to the service account that Cloud Run will use
$PROJECT_NUMBER=(gcloud projects describe $env:PROJECT_ID --format="value(projectNumber)")
$SA_EMAIL="[email protected]"
gcloud storage buckets add-iam-policy-binding gs://peloton-to-garmin-output `
--member="serviceAccount:${SA_EMAIL}" `
--role=roles/storage.objectUser
gcloud storage buckets add-iam-policy-binding gs://peloton-to-garmin-data `
--member="serviceAccount:${SA_EMAIL}" `
--role=roles/storage.objectUser
gcloud storage buckets add-iam-policy-binding gs://peloton-to-garmin-config `
--member="serviceAccount:${SA_EMAIL}" `
--role=roles/storage.objectViewer
# build the new image (see https://github.com/philosowaffle/peloton-to-garmin/discussions/678#discussioncomment-11300480)
docker build . --network=host -f .\docker\Dockerfile.console -t p2g-console-modified
# prepare docker to push to the Google Cloud artifact registry
gcloud auth configure-docker us-west1-docker.pkg.dev
# create repository
gcloud artifacts repositories create docker-images --repository-format=docker
# tag and push (use the image ID from the build above)
docker tag IMAGE_ID "us-west1-docker.pkg.dev/$env:PROJECT_ID/docker-images/p2g-console-modified"
docker push "us-west1-docker.pkg.dev/$env:PROJECT_ID/docker-images/p2g-console-modified"
# create the job - https://cloud.google.com/sdk/gcloud/reference/run/jobs/create
gcloud run jobs create $env:JOB_NAME `
--image "us-west1-docker.pkg.dev/$env:PROJECT_ID/docker-images/p2g-console-modified" `
--tasks 1 `
--max-retries 1 `
--region "$env:REGION" `
--project "$env:PROJECT_ID" `
--set-env-vars "P2G_CONFIG=/app/mounted-config" `
--add-volume "name=volume_output,type=cloud-storage,bucket=peloton-to-garmin-output" `
--add-volume-mount "volume=volume_output,mount-path=/app/output" `
--add-volume "name=volume_data,type=cloud-storage,bucket=peloton-to-garmin-data" `
--add-volume-mount "volume=volume_data,mount-path=/app/data" `
--add-volume "name=volume_config,type=cloud-storage,bucket=peloton-to-garmin-config" `
--add-volume-mount "volume=volume_config,mount-path=/app/mounted-config" `
# manually kick off a run:
gcloud run jobs execute $env:JOB_NAME --region "$env:REGION"
# you can use the google cloud console to examine logs of that
# Schedule the job to run every 15 minutes, but only through hours 6-23
gcloud scheduler jobs create http p2g-scheduled-job `
--schedule="3,18,33,48 6-23 * * * " `
--time-zone="America/Los_Angeles" `
--uri="https://us-west1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${env:PROJECT_ID}/jobs/${env:JOB_NAME}:run" `
--project="$env:PROJECT_ID" `
--location="$env:REGION" `
--http-method=POST `
--headers="User-Agent=Google-Cloud-Scheduler" `
--oauth-service-account-email="$SA_EMAIL" `
--oauth-token-scope="https://www.googleapis.com/auth/cloud-platform" `
--min-backoff="5s" --max-backoff="10m"
# You can then browse the output bucket to see log files, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment