Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
167 lines (149 loc) · 7 KB
/
Copy pathaction.yml
File metadata and controls
167 lines (149 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: "TestDriver.ai"
description: >-
Authenticate to TestDriver.ai from GitHub Actions using GitHub's OIDC token —
no stored TD_API_KEY secret required. Exports TD_API_KEY for later steps.
author: "TestDriver.ai"
branding:
icon: "play-circle"
color: "purple"
# This action ONLY authenticates. It mints a GitHub OIDC token, exchanges it for
# your team's TestDriver API key, and exports TD_API_KEY to $GITHUB_ENV (and as
# step outputs) so a LATER step in your job can run the tests. It does not run
# tests itself.
#
# Requirements in the calling job:
# permissions:
# id-token: write # REQUIRED — lets the runner mint an OIDC token
# contents: read
#
# One-time setup: authorize the TestDriver GitHub App for your org at the console
# (see consoleUrl for your channel) so the org -> team binding exists. If your org
# authorized the App before OIDC support shipped, re-authorize once.
inputs:
channel:
description: "TestDriver release channel: dev | test | canary | stable. Selects the API + console URLs."
required: false
default: "stable"
api-root:
description: "Explicit API root override (e.g. https://api-test.testdriver.ai). When set, wins over `channel`."
required: false
default: ""
api-key:
description: "Optional TD_API_KEY (e.g. the secrets.TD_API_KEY value). Used as a fallback if OIDC auth is unavailable for this org."
required: false
default: ""
outputs:
api-key:
description: "The resolved TestDriver API key (masked in logs)."
value: ${{ steps.auth.outputs.api-key }}
identity:
description: "JSON identity of the resolved team (echoed from the auth exchange). Empty when the api-key fallback was used."
value: ${{ steps.auth.outputs.identity }}
runs:
using: "composite"
steps:
- name: Authenticate to TestDriver
id: auth
shell: bash
env:
TD_CHANNEL: ${{ inputs.channel }}
TD_API_ROOT_INPUT: ${{ inputs.api-root }}
TD_API_KEY_INPUT: ${{ inputs.api-key }}
CHANNELS_JSON: ${{ github.action_path }}/channels.json
run: |
set -uo pipefail
# --- Resolve endpoints from channel (or api-root override) -------------
CHANNEL="${TD_CHANNEL:-stable}"
if ! jq -e --arg c "$CHANNEL" 'has($c)' "$CHANNELS_JSON" >/dev/null 2>&1; then
echo "::error::Unknown TestDriver channel \"$CHANNEL\". Expected one of: dev, test, canary, stable."
exit 1
fi
CONSOLE_URL=$(jq -r --arg c "$CHANNEL" '.[$c].consoleUrl' "$CHANNELS_JSON")
if [ -n "${TD_API_ROOT_INPUT}" ]; then
API_ROOT="${TD_API_ROOT_INPUT%/}"
else
API_ROOT=$(jq -r --arg c "$CHANNEL" '.[$c].apiRoot' "$CHANNELS_JSON")
fi
echo "TestDriver channel: $CHANNEL"
echo "API root: $API_ROOT"
# Helper: adopt a key as the resolved TD_API_KEY (mask + export + output).
adopt_key() {
local key="$1"
echo "::add-mask::$key"
echo "TD_API_KEY=$key" >> "$GITHUB_ENV"
echo "api-key=$key" >> "$GITHUB_OUTPUT"
}
# Helper: use the api-key fallback if one was provided. Returns 0 on success.
use_fallback() {
if [ -n "${TD_API_KEY_INPUT}" ]; then
echo "::warning::Using the provided TD_API_KEY secret as a fallback (OIDC auth was unavailable)."
adopt_key "${TD_API_KEY_INPUT}"
echo "identity=" >> "$GITHUB_OUTPUT"
return 0
fi
return 1
}
# --- 1. Mint the OIDC token (audience must match the server) -----------
if [ -z "${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" ] || [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ]; then
echo "::error::No OIDC token request context. Add \`permissions: { id-token: write }\` to the job so the runner can mint an OIDC token."
if use_fallback; then exit 0; fi
exit 1
fi
OIDC_JSON=$(curl -sS \
-H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=testdriver")
OIDC_TOKEN=$(echo "$OIDC_JSON" | jq -r '.value // empty')
if [ -z "$OIDC_TOKEN" ]; then
echo "::error::Failed to mint a GitHub OIDC token. Ensure \`permissions: { id-token: write }\` is set."
if use_fallback; then exit 0; fi
exit 1
fi
echo "OIDC token minted (length=${#OIDC_TOKEN})."
# --- 2. Exchange the OIDC token for the team's API key -----------------
RESP=$(curl -sS -w '\n%{http_code}' -X POST "$API_ROOT/github/actions/auth" \
-H "Content-Type: application/json" \
-d "{\"token\":\"$OIDC_TOKEN\"}")
BODY=$(echo "$RESP" | head -n -1)
CODE=$(echo "$RESP" | tail -n 1)
echo "Auth exchange HTTP status: $CODE"
ERR=$(echo "$BODY" | jq -r '.error // empty' 2>/dev/null)
# --- 3. Branch on the outcome ------------------------------------------
if [ "$CODE" = "200" ]; then
API_KEY=$(echo "$BODY" | jq -r '.apiKey // empty')
if [ -z "$API_KEY" ] || [ "$API_KEY" = "null" ]; then
echo "::error::Auth succeeded (200) but no apiKey was returned. Body: $BODY"
if use_fallback; then exit 0; fi
exit 1
fi
adopt_key "$API_KEY"
IDENTITY=$(echo "$BODY" | jq -c '.identity // {}')
echo "identity=$IDENTITY" >> "$GITHUB_OUTPUT"
# Export the OIDC-VERIFIED actor id for the test step that runs later.
# The runner already has GITHUB_ACTOR, but that's a renameable login
# the workflow can overwrite; actor_id came off GitHub's signature and
# is immutable. The SDK forwards it on the test run so per-GitHub-user
# billing can tell an attested identity from a self-asserted one.
# Only set on this path — the api-key fallback never sees a token.
ACTOR_ID=$(echo "$BODY" | jq -r '.identity.github.actorId // empty')
if [ -n "$ACTOR_ID" ]; then
echo "TD_GITHUB_ACTOR_ID=$ACTOR_ID" >> "$GITHUB_ENV"
fi
echo "Resolved team identity:"
echo "$BODY" | jq '.identity // "no identity in response"'
echo "Authenticated to TestDriver via OIDC."
exit 0
fi
if [ "$CODE" = "403" ] && [ "$ERR" = "no_team" ]; then
if use_fallback; then exit 0; fi
echo "::error::The TestDriver GitHub App is not authorized for this organization. Authorize it at $CONSOLE_URL (re-authorize once if your org authorized before OIDC support shipped), or set a TD_API_KEY secret and pass it via the \`api-key\` input."
exit 1
fi
if [ "$CODE" = "401" ]; then
if use_fallback; then exit 0; fi
echo "::error::TestDriver rejected the OIDC token (401). This usually means a wrong audience or a missing \`permissions: { id-token: write }\`. Body: $BODY"
exit 1
fi
# Any other status.
if use_fallback; then exit 0; fi
echo "::error::TestDriver auth exchange failed (HTTP $CODE). Body: $BODY"
exit 1