107 lines
3.5 KiB
YAML
107 lines
3.5 KiB
YAML
# This taskfile handles building and deploying the petname-service
|
|
version: '3'
|
|
|
|
vars:
|
|
DEFAULT_PROJECT_ID: "infinite-deck-479516-g6"
|
|
REGION: "us-east4"
|
|
SERVICE_NAME: "petname"
|
|
DOMAIN: "proto-hype.net"
|
|
|
|
tasks:
|
|
login:
|
|
desc: Login w/ gcloud
|
|
summary: |
|
|
Initializes gcloud and sets the project, region, and zone. You can optionally provide
|
|
a project ID as an argument, otherwise it defaults to the pre-existing project.
|
|
|
|
Example: task init
|
|
cmds:
|
|
- |
|
|
PROJECT_ID="${1:-{{.DEFAULT_PROJECT_ID}}}"
|
|
gcloud auth login --update-adc
|
|
gcloud config set project "$PROJECT_ID"
|
|
gcloud config set compute/region {{.REGION}}
|
|
gcloud config set compute/zone {{.REGION}}-c
|
|
gcloud auth application-default set-quota-project "$PROJECT_ID"
|
|
|
|
setup:
|
|
desc: Enable required Google Cloud APIs
|
|
summary: |
|
|
Enables the Cloud Run, Cloud Build, and Artifact Registry APIs required for deployment.
|
|
cmds:
|
|
- |
|
|
gcloud services enable run.googleapis.com cloudbuild.googleapis.com artifactregistry.googleapis.com
|
|
|
|
deploy:
|
|
desc: Deploy the service to Google Cloud Run
|
|
summary: |
|
|
Deploys the source code directly to Cloud Run using Cloud Build (Option A).
|
|
|
|
Example: task deploy
|
|
cmds:
|
|
- |
|
|
gcloud run deploy {{.SERVICE_NAME}} \
|
|
--source . \
|
|
--region {{.REGION}} \
|
|
--allow-unauthenticated
|
|
|
|
map-domain:
|
|
desc: Map a custom domain to the Cloud Run service
|
|
summary: |
|
|
Map <service>.<domain> to the deployed service. This requires the service
|
|
to be deployed in a region which support domain-mappints (like us-east4).
|
|
|
|
Example: task map-domain
|
|
cmds:
|
|
- |
|
|
gcloud beta run domain-mappings create \
|
|
--service {{.SERVICE_NAME }} \
|
|
--domain {{ .SERVICE_NAME }}.{{ .DOMAIN }} \
|
|
--region {{.REGION}}
|
|
|
|
logs:
|
|
desc: Tail the real-time logs for the service
|
|
summary: |
|
|
Streams the live logs from Cloud Run to your terminal. Useful for debugging
|
|
or watching traffic hit the service. Press Ctrl+C to stop.
|
|
|
|
Example: task logs
|
|
cmds:
|
|
- |
|
|
gcloud beta run services logs tail {{.SERVICE_NAME}} \
|
|
--region {{.REGION}}
|
|
|
|
test:
|
|
desc: Test the deployed service
|
|
summary: |
|
|
Tests the deployed service by sending a request to the mapped domain. Make sure
|
|
to replace <service> and <domain> with your actual service name and domain.
|
|
|
|
Example: task test
|
|
silent: true
|
|
cmds:
|
|
- |
|
|
function test_service() {
|
|
local path="$1"
|
|
|
|
# Use curl to capture both HTTP status code and response body
|
|
local response=$(curl -s -w "%{http_code}" https://{{.SERVICE_NAME}}.{{.DOMAIN}}/$path)
|
|
|
|
# Extract status code (last 3 chars) and body (everything else)
|
|
local status_code="${response:${#response}-3}"
|
|
local body="${response:0:${#response}-3}"
|
|
|
|
if [ "$status_code" -lt 399 ]; then
|
|
printf "GET /%s -> [SUCCESS] Status: %s, Body: %s\n" "$path" "$status_code" "$body"
|
|
else
|
|
printf "GET /%s -> [FAILURE] Status: %s, Body: %s\n" "$path" "$status_code" "$body"
|
|
fi
|
|
|
|
# Explicitly return 0 so the task does not fail if a test fails
|
|
return 0
|
|
}
|
|
|
|
test_service ""
|
|
test_service "?words=4"
|
|
test_service "?separator=."
|