117 lines
3.9 KiB
YAML
117 lines
3.9 KiB
YAML
# This taskfile handles building and deploying the petname-service
|
|
version: '3'
|
|
|
|
dotenv: ['.env']
|
|
|
|
vars:
|
|
PROJECT_ID: '{{ .PROJECT_ID }}'
|
|
REGION: '{{ default "us-east4" .REGION }}'
|
|
DOMAIN: '{{ .DOMAIN }}'
|
|
SERVICE_NAME: "petname"
|
|
|
|
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:-{{ .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"
|
|
local url="https://{{.SERVICE_NAME}}.{{.DOMAIN}}/$path"
|
|
|
|
# Use curl to capture response body and append the http_code at the end.
|
|
# Using `|| true` prevents Task from failing if curl encounters a network error.
|
|
local response=$(curl -s -w "%{http_code}" "$url") || true
|
|
|
|
# Extract status code (last 3 chars) and body (everything else)
|
|
local status_code="${response:${#response}-3}"
|
|
local status_text="ERROR"
|
|
local body="${response:0:${#response}-3}"
|
|
|
|
# Check if curl failed completely (e.g. DNS failure), resulting in empty output
|
|
if [ -z "$response" ] || [ "${response:${#response}-3}" = "000" ]; then
|
|
body="[Network failure or unresolvable host]"
|
|
fi
|
|
|
|
# Status code in the 200-399 range is considered a successful response
|
|
if [ "$status_code" -ge 200 ] && [ "$status_code" -lt 400 ]; then
|
|
status_text="SUCCESS"
|
|
fi
|
|
|
|
printf "GET /%s -> [%s] Status: %s, Body: %s\n" "$path" "$status_text" "$status_code" "$body"
|
|
|
|
return 0
|
|
}
|
|
|
|
test_service ""
|
|
test_service "?words=4"
|
|
test_service "?separator=."
|