Skip to content

Commit 1be8b3c

Browse files
committed
adding id support
1 parent eadaf11 commit 1be8b3c

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

.github/workflows/build-test.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ jobs:
1818
- name: Invoke echo 1 workflow using this action
1919
uses: ./
2020
with:
21-
workflow: Message Echo 1
21+
workflow: Message Echo 1
2222
token: ${{ secrets.PERSONAL_TOKEN }}
2323
inputs: '{"message": "blah blah"}'
2424

2525
- name: Invoke echo 2 workflow using this action
2626
uses: ./
2727
with:
28-
workflow: Message Echo 2
28+
workflow: Message Echo 2
2929
token: ${{ secrets.PERSONAL_TOKEN }}
3030

31+
- name: Invoke echo 1 workflow by id
32+
uses: ./
33+
with:
34+
workflow: '1854247'
35+
use-workflow-id: 'true'
36+
token: ${{ secrets.PERSONAL_TOKEN }}
37+
inputs: '{"message": "Mango jam"}'
38+
3139
- name: Update repo with build
3240
uses: mikeal/publish-to-github-action@master
3341
env:

action.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ inputs:
77
required: true
88
token:
99
description: 'GitHub token with repo write access, can NOT use secrets.GITHUB_TOKEN, see readme'
10-
required: true
10+
required: true
11+
use-workflow-id:
12+
description: 'When true the workflow input is treated as an id rather than name'
13+
required: false
14+
default: 'false'
1115
inputs:
1216
description: 'Inputs to pass to the workflow, must be a JSON string.'
13-
required: false
17+
required: false
1418
ref:
1519
description: 'The reference of the workflow run. The reference can be a branch, tag, or a commit SHA'
1620
required: false
1721
repo:
1822
description: 'Repo owner & name, slash separated, only set if invoking a workflow in a different repo'
19-
required: false
23+
required: false
2024

2125
runs:
2226
using: 'node12'

src/main.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ async function run(): Promise<void> {
66
try {
77
// Required inputs
88
const token = core.getInput('token')
9-
const workflowName = core.getInput('workflow')
9+
const workflowReference = core.getInput('workflow')
10+
const workflowIdFlag = core.getInput('use-workflow-id') || 'false'
1011
// Optional inputs, with defaults
1112
const ref = core.getInput('ref') || github.context.ref
1213
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`
@@ -21,25 +22,33 @@ async function run(): Promise<void> {
2122
// Get octokit client for making API calls
2223
const octokit = github.getOctokit(token)
2324

24-
// List workflows via API
25-
const listResp = await octokit.request(`GET /repos/${repo}/actions/workflows`, {
26-
ref: ref,
27-
inputs: inputs
28-
})
29-
if(listResp.status != 200) throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`)
30-
31-
// Debug response if ACTIONS_STEP_DEBUG is enabled
32-
core.debug(listResp.data)
25+
let workflowId = ''
26+
if(workflowIdFlag !== 'true') {
27+
// List workflows via API
28+
const listResp = await octokit.request(`GET /repos/${repo}/actions/workflows`, {
29+
ref: ref,
30+
inputs: inputs
31+
})
32+
if(listResp.status != 200) throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`)
33+
34+
// Debug response if ACTIONS_STEP_DEBUG is enabled
35+
core.debug(listResp.data)
36+
37+
// Locate workflow by name as we need it's id
38+
const foundWorkflow = listResp.data.workflows.find((wf: Record<string, string>) => {
39+
return wf['name'] === workflowReference
40+
})
41+
if(!foundWorkflow) throw new Error(`Unable to find workflow named '${workflowReference}' in ${repo} 😥`)
42+
43+
workflowId = foundWorkflow.id
44+
} else {
45+
workflowId = workflowReference
46+
}
3347

34-
// Locate workflow by name as we need it's id
35-
const workflowFind = listResp.data.workflows.find((wf: Record<string, string>) => {
36-
return wf['name'] === workflowName
37-
})
38-
if(!workflowFind) throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`)
39-
console.log(`Workflow id is: ${workflowFind.id}`)
48+
console.log(`Workflow id is: ${workflowId}`)
4049

4150
// Call workflow_dispatch API
42-
const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
51+
const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${workflowId}/dispatches`, {
4352
ref: ref,
4453
inputs: inputs
4554
})

0 commit comments

Comments
 (0)