Skip to content

Commit 9eec33f

Browse files
Add get-workflow-ref action
Resolves the ref a caller used to invoke a reusable workflow from this repo by parsing the caller's workflow file. No API calls or extra permissions needed — works with both public and private repos. This is needed because reusable workflows cannot access their own invocation ref via the github context (github.workflow_ref and github.workflow_sha both point to the caller's repo). Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
1 parent 2d1d405 commit 9eec33f

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

get-workflow-ref/action.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Get Workflow Ref
2+
description: >
3+
Resolve the ref that a caller used to invoke a reusable workflow from this
4+
repo. Parses the caller's workflow file — no API calls or extra permissions
5+
needed.
6+
7+
inputs:
8+
workflow_name:
9+
description: >
10+
Substring to match in the caller's uses line (e.g.
11+
"cockroachdb/actions/.github/workflows/github-issue-autosolve.yml").
12+
required: true
13+
14+
outputs:
15+
ref:
16+
description: The ref (tag, branch, or SHA) from the caller's uses line.
17+
value: ${{ steps.resolve.outputs.ref }}
18+
19+
runs:
20+
using: "composite"
21+
steps:
22+
- name: Checkout caller workflows
23+
uses: actions/checkout@v5
24+
with:
25+
sparse-checkout: .github/workflows
26+
persist-credentials: false
27+
28+
- name: Resolve ref from caller workflow
29+
id: resolve
30+
shell: bash
31+
run: |
32+
workflow_ref="${WORKFLOW_REF}"
33+
workflow_path="${workflow_ref#"${REPO}/"}"
34+
workflow_path="${workflow_path%"@${REF}"}"
35+
36+
if [ ! -f "$workflow_path" ]; then
37+
echo "::error::Could not find caller workflow at: $workflow_path"
38+
exit 1
39+
fi
40+
41+
match="$(grep "$WORKFLOW_NAME" "$workflow_path" | head -1)"
42+
if [ -z "$match" ]; then
43+
echo "::error::Could not find '$WORKFLOW_NAME' in $workflow_path"
44+
exit 1
45+
fi
46+
47+
# Handle local reference (./): use the caller's own ref.
48+
if [[ "$match" == *"./.github/workflows"* ]]; then
49+
ref="${REF}"
50+
else
51+
ref="$(echo "$match" | sed 's/.*@//' | awk '{print $1}')"
52+
fi
53+
54+
if [ -z "$ref" ]; then
55+
echo "::error::Could not parse ref from: $match"
56+
exit 1
57+
fi
58+
59+
echo "ref=$ref" >> "$GITHUB_OUTPUT"
60+
echo "Resolved workflow ref: $ref"
61+
env:
62+
WORKFLOW_REF: ${{ github.workflow_ref }}
63+
REPO: ${{ github.repository }}
64+
REF: ${{ github.ref }}
65+
WORKFLOW_NAME: ${{ inputs.workflow_name }}

0 commit comments

Comments
 (0)