Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
126 lines (119 loc) · 5.3 KB
/
Copy pathaction.yml
File metadata and controls
126 lines (119 loc) · 5.3 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
name: "Blacksmith Set Secrets"
author: Blacksmith
description: "Sets Blacksmith secrets from a GitHub Actions job running on a Blacksmith runner"
branding:
icon: key
color: black
inputs:
secrets:
description: "One NAME=value per line, up to 10. Values are single-line, up to 8KB each, and masked in the job log."
required: true
outputs:
created:
description: "JSON array of the secret names this run created"
value: ${{ steps.set.outputs.created }}
updated:
description: "JSON array of the secret names whose value this run replaced"
value: ${{ steps.set.outputs.updated }}
runs:
using: composite
steps:
- id: set
shell: bash
env:
SECRETS: ${{ inputs.secrets }}
run: |
set -euo pipefail
# Errors name the line number, never the line: it holds a secret.
names=()
values=()
line_number=0
while IFS= read -r line || [ -n "$line" ]; do
line_number=$((line_number + 1))
line=${line%$'\r'}
[[ "$line" =~ ^[[:space:]]*$ ]] && continue
# The runner splits workflow commands on CR, so ::add-mask:: would hide only the text before one.
if [[ "$line" == *$'\r'* ]]; then
echo "::error::secrets line $line_number contains a carriage return; values must be single-line"
exit 1
fi
if [[ "$line" != *=* ]]; then
echo "::error::secrets line $line_number must be NAME=value"
exit 1
fi
name=${line%%=*}
name=${name#"${name%%[![:space:]]*}"}
name=${name%"${name##*[![:space:]]}"}
value=${line#*=}
value=${value#"${value%%[![:space:]]*}"}
value=${value%"${value##*[![:space:]]}"}
if [ -n "$value" ]; then
# Escape workflow-command data so the runner registers the literal value.
echo "::add-mask::${value//%/%25}"
fi
if ! [[ "$name" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
echo "::error::secrets line $line_number: the name is not a valid env var name (letters, digits and underscores, not starting with a digit)"
exit 1
fi
if [ -z "$value" ]; then
echo "::error::secrets line $line_number: $name has an empty value"
exit 1
fi
for seen in "${names[@]}"; do
if [ "$seen" = "$name" ]; then
echo "::error::secrets line $line_number: $name is listed twice"
exit 1
fi
done
names+=("$name")
values+=("$value")
done <<< "$SECRETS"
if [ "${#names[@]}" -eq 0 ] || [ "${#names[@]}" -gt 10 ]; then
echo "::error::secrets must list 1 to 10 NAME=value lines, got ${#names[@]}"
exit 1
fi
# "unset" is the agent's placeholder when the control plane sent no token.
token=${BLACKSMITH_MONITORING_TOKEN:-}
if [ -z "$token" ] || [ "$token" = unset ]; then
echo "::error::useblacksmith/set-secrets only works on Blacksmith runners (runs-on: blacksmith-*)"
exit 1
fi
if ! command -v jq >/dev/null; then
echo "::error::jq is required on the runner"
exit 1
fi
# The monitoring token is only valid on the control plane that provisioned this runner.
backend=${BLACKSMITH_BACKEND_URL:-https://api.blacksmith.sh}
backend=${backend%/}
# The token and values reach curl and jq through files and pipes, not argv, so other processes cannot read them.
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
chmod 700 "$tmp"
printf 'Authorization: Bearer %s\n' "$token" > "$tmp/headers"
for i in "${!names[@]}"; do
printf '%s\0%s\0' "${names[$i]}" "${values[$i]}"
done | jq -Rsc --arg workflow_ref "${GITHUB_WORKFLOW_REF:-}" \
'split("\u0000")[:-1] as $f | {vars: [range(0; $f | length; 2) | {key: $f[.], value: $f[. + 1]}] | from_entries}
+ (if $workflow_ref == "" then {} else {workflow_ref: $workflow_ref} end)' > "$tmp/body"
status=$(curl -sS --retry 3 --retry-connrefused --max-time 30 \
-o "$tmp/response" -w '%{http_code}' \
-X PUT \
-H @"$tmp/headers" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data-binary @"$tmp/body" \
"$backend/api/codesmith/runner/env-vars")
if [ "$status" != 200 ]; then
message=$(jq -r '.message // empty' "$tmp/response" 2>/dev/null || true)
echo "::error::Could not set Blacksmith secrets at $backend (HTTP $status): ${message:-unexpected response}"
exit 1
fi
if ! created=$(jq -ec '.created | select(type == "array" and all(type == "string"))' "$tmp/response" 2>/dev/null) \
|| ! updated=$(jq -ec '.updated | select(type == "array" and all(type == "string"))' "$tmp/response" 2>/dev/null); then
echo "::error::Unexpected response from the Blacksmith secrets API (HTTP $status)"
exit 1
fi
echo "created=$created" >> "$GITHUB_OUTPUT"
echo "updated=$updated" >> "$GITHUB_OUTPUT"
summary='if length == 0 then "none" else join(", ") end'
echo "Set Blacksmith secrets: created $(jq -r "$summary" <<< "$created"), updated $(jq -r "$summary" <<< "$updated")"