範例:端到端建立一個 service

此文件屬於使用 CLI指南的一部分。請在此處查看完整指南:安裝並使用 Stackhero CLI,從命令列、scripts、CI 及 AI agents 管理您的 stacks 和 services

以下是一個範例 script,示範如何建立 stack、加入 service、等待它準備就緒,以及擷取其 credentials。此範例完全以非互動方式運作,並使用 STACKHERO_TOKEN environment variable。

#!/bin/bash
set -e

export STACKHERO_TOKEN="usr-xxxxxx:your-token"

serviceStore="directus"   # A service store name. See: `stackhero services-store-list`
instance="1GB"            # An instance (size) name. See: `stackhero instances-store-list --service-store=directus`
region="europe"           # A region name. See: `stackhero regions-list`

# Create a stack (uses your only organization, add `--organization` if you have several)
stackId=$(stackhero --format=script stack-create \
  --name="My stack")
echo "Stack: ${stackId}"

# Add a service (names are resolved automatically, and the stack ID above also works)
serviceId=$(stackhero --format=script service-add \
  --stack="My stack" \
  --service-store="${serviceStore}" \
  --instance="${instance}" \
  --region="${region}")
echo "Service: ${serviceId}"

# Wait for the service to be running
stackhero service-wait-for --service="${serviceId}"

# Retrieve credentials and configuration
stackhero service-configuration-get --service="${serviceId}" --format=json

每個 service 文件頁面都包含一份可直接執行的 Automate with the CLI 指南,並按該 service 度身訂造,讓您具備開始所需的一切。