mcpField
T
tfc-workspace-runs

tfc-workspace-runs

github.com/laurigates云与 DevOps5962 次安装1 天前更新
代码仓库

关于 tfc-workspace-runs

tfc-workspace-runs 是 Forum Virium Helsinki Terraform Cloud 工作区的快捷查询技能,可按简称或跨工作区查看运行记录,便于快速找到运行 ID 和进行中任务。

主要功能

  1. 01按简称查询已知工作区运行记录
  2. 02汇总多个工作区的最新运行
  3. 03检查进行中的运行
  4. 04使用 TFE_TOKEN 调用 Terraform Cloud API
  5. 05支持添加工作区映射

适用场景

  1. 01查看 GCP 工作区的近期运行
  2. 02扫描所有已知工作区的最新运行
  3. 03确认哪些工作区有进行中任务

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/laurigates/claude-plugins --skill tfc-workspace-runs

安装后在 Claude Code 中输入/tfc-workspace-runs确认已加载。

详细介绍

Terraform Cloud 工作区运行

用于快速访问 Forum Virium Helsinki Terraform Cloud 工作区中运行的便捷封装。

何时使用此技能

在以下情况下使用此技能……在以下情况下改用同系列技能……
通过简称(github、sentry、gcp、onelogin、twingate)列出 FVH 工作区的运行列出非 FVH 工作区或任意 ID 的运行(tfc-list-runs)
一次性检查每个 FVH 工作区的最新运行使用完整选项按状态、操作或来源筛选(tfc-list-runs)
查看哪些 FVH 工作区有进行中的运行查看单个已知运行的详细状态(tfc-run-status)
在深入查看日志/计划 JSON 前找到正确的运行 ID查看特定运行的计划/应用日志内容(tfc-run-logs)

前提条件

export TFE_TOKEN="your-api-token"        # User or team token
export TFE_ADDRESS="app.terraform.io"    # Optional

已知工作区

简称完整工作区名称
githubinfrastructure-github
sentryinfrastructure-sentry
gcpinfrastructure-gcp
onelogininfrastructure-onelogin
twingateinfrastructure-twingate

核心命令

快速列出运行
#!/bin/bash
set -euo pipefail

TOKEN="${TFE_TOKEN:?TFE_TOKEN not set}"
BASE_URL="https://${TFE_ADDRESS:-app.terraform.io}/api/v2"
ORG="ForumViriumHelsinki"

# Map shorthand to full workspace name
case "${1:-}" in
  github)   WORKSPACE="infrastructure-github" ;;
  sentry)   WORKSPACE="infrastructure-sentry" ;;
  gcp)      WORKSPACE="infrastructure-gcp" ;;
  onelogin) WORKSPACE="infrastructure-onelogin" ;;
  twingate) WORKSPACE="infrastructure-twingate" ;;
  *)
    echo "Usage: $0 <workspace> [limit]"
    echo "Workspaces: github, sentry, gcp, onelogin, twingate"
    exit 1
    ;;
esac

LIMIT="${2:-10}"

# Get workspace ID
WS_ID=$(curl -sf --header "Authorization: Bearer $TOKEN" \
  "$BASE_URL/organizations/$ORG/workspaces/$WORKSPACE" | \
  jq -r '.data.id')

# List runs
echo "Recent runs for $WORKSPACE:"
echo ""

curl -sf --header "Authorization: Bearer $TOKEN" \
  "$BASE_URL/workspaces/$WS_ID/runs?page[size]=$LIMIT&include=plan" | \
  jq -r '
    ["RUN_ID", "STATUS", "+", "~", "-", "CREATED", "MESSAGE"],
    (.data[] |
      .relationships.plan.data.id as $pid |
      [
        .id,
        .attributes.status,
        ((.included[] | select(.id == $pid) | .attributes."resource-additions") // 0 | tostring),
        ((.included[] | select(.id == $pid) | .attributes."resource-changes") // 0 | tostring),
        ((.included[] | select(.id == $pid) | .attributes."resource-destructions") // 0 | tostring),
        .attributes."created-at"[0:16],
        (.attributes.message // "No message")[0:30]
      ]
    ) | @tsv' | column -t
单行示例
# GCP workspace - recent runs
curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
  "https://app.terraform.io/api/v2/organizations/ForumViriumHelsinki/workspaces/infrastructure-gcp" | \
  jq -r '.data.id' | \
  xargs -I{} curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
    "https://app.terraform.io/api/v2/workspaces/{}/runs?page[size]=5" | \
  jq -r '.data[] | "\(.id) | \(.attributes.status) | \(.attributes."created-at"[0:16])"'

# GitHub workspace - failed runs only
curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
  "https://app.terraform.io/api/v2/organizations/ForumViriumHelsinki/workspaces/infrastructure-github" | \
  jq -r '.data.id' | \
  xargs -I{} curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
    "https://app.terraform.io/api/v2/workspaces/{}/runs?filter[status]=errored" | \
  jq -r '.data[] | "\(.id) | \(.attributes.status) | \(.attributes.message)"'
获取每个工作区的最新运行
#!/bin/bash
set -euo pipefail

TOKEN="${TFE_TOKEN:?TFE_TOKEN not set}"
BASE_URL="https://${TFE_ADDRESS:-app.terraform.io}/api/v2"
ORG="ForumViriumHelsinki"

WORKSPACES="infrastructure-github infrastructure-sentry infrastructure-gcp infrastructure-onelogin infrastructure-twingate"

echo "Latest run for each workspace:"
echo ""

for WS in $WORKSPACES; do
  WS_ID=$(curl -sf --header "Authorization: Bearer $TOKEN" \
    "$BASE_URL/organizations/$ORG/workspaces/$WS" | jq -r '.data.id')

  LATEST=$(curl -sf --header "Authorization: Bearer $TOKEN" \
    "$BASE_URL/workspaces/$WS_ID/runs?page[size]=1" | \
    jq -r '.data[0] | "\(.id) | \(.attributes.status) | \(.attributes."created-at"[0:16])"')

  echo "$WS: $LATEST"
done
检查进行中的运行
#!/bin/bash
set -euo pipefail

TOKEN="${TFE_TOKEN:?TFE_TOKEN not set}"
BASE_URL="https://${TFE_ADDRESS:-app.terraform.io}/api/v2"
ORG="ForumViriumHelsinki"

WORKSPACES="infrastructure-github infrastructure-sentry infrastructure-gcp infrastructure-onelogin infrastructure-twingate"

echo "Checking for in-progress runs..."
echo ""

for WS in $WORKSPACES; do
  WS_ID=$(curl -sf --header "Authorization: Bearer $TOKEN" \
    "$BASE_URL/organizations/$ORG/workspaces/$WS" | jq -r '.data.id')

  IN_PROGRESS=$(curl -sf --header "Authorization: Bearer $TOKEN" \
    "$BASE_URL/workspaces/$WS_ID/runs?filter[status_group]=non_final&page[size]=5" | \
    jq -r '.data | length')

  if [ "$IN_PROGRESS" -gt 0 ]; then
    echo "$WS: $IN_PROGRESS run(s) in progress"
    curl -sf --header "Authorization: Bearer $TOKEN" \
      "$BASE_URL/workspaces/$WS_ID/runs?filter[status_group]=non_final" | \
      jq -r '.data[] | "  - \(.id): \(.attributes.status)"'
  fi
done

快速参考

获取工作区 ID
# Generic pattern
curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
  "https://app.terraform.io/api/v2/organizations/ForumViriumHelsinki/workspaces/infrastructure-gcp" | \
  jq -r '.data.id'
工作区 URL(供参考)

与其他技能集成

获取最新 GCP 运行的日志
# Get latest run ID
RUN_ID=$(curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
  "https://app.terraform.io/api/v2/organizations/ForumViriumHelsinki/workspaces/infrastructure-gcp" | \
  jq -r '.data.id' | \
  xargs -I{} curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
    "https://app.terraform.io/api/v2/workspaces/{}/runs?page[size]=1" | \
  jq -r '.data[0].id')

# Then use tfc-run-logs skill to get logs
echo "Use tfc-run-logs with run ID: $RUN_ID"
快速检查所有工作区的状态
# Combine with tfc-run-status for detailed info on specific runs

自定义

如需添加更多工作区,请更新脚本中的 case 语句:

case "${1:-}" in
  github)   WORKSPACE="infrastructure-github" ;;
  sentry)   WORKSPACE="infrastructure-sentry" ;;
  gcp)      WORKSPACE="infrastructure-gcp" ;;
  onelogin) WORKSPACE="infrastructure-onelogin" ;;
  twingate) WORKSPACE="infrastructure-twingate" ;;
  # Add more here:
  newworkspace) WORKSPACE="infrastructure-newworkspace" ;;
  *)
    echo "Usage: $0 <workspace>"
    exit 1
    ;;
esac

另请参阅

  • tfc-list-runs:提供所有筛选条件的全功能运行列表
  • tfc-run-logs:获取特定运行的计划/应用日志
  • tfc-run-status:快速检查运行状态
  • tfc-plan-json:获取结构化计划 JSON 输出
  • terraform-workspace-manager:工作区编排和运行管理

评论

支持 Markdown