Skip to content

Commit f240894

Browse files
committed
v1.1 work
1 parent e5a167b commit f240894

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

.github/workflows/build-test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: Build & Test
22

33
on:
44
push:
5+
branches: [ master ]
56
workflow_dispatch:
67

78
jobs:

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ This allows you to chain workflows, the classic use case is have a CI build work
77

88
For details of the `workflow_dispatch` even see [this blog post introducing this type of trigger](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/)
99

10-
Note. The GitHub UI will report flows triggered by this action as "manually triggered" even though they have been run programmatically via another workflow and the API
10+
*Note 1.* The GitHub UI will report flows triggered by this action as "manually triggered" even though they have been run programmatically via another workflow and the API
11+
12+
*Note 2.* If you want to reference the target workflow by ID, you will need to list them with the following REST API call `curl https://api.github.com/repos/{{owner}}/{{repo}}/actions/workflows -H "Authorization: token {{pat-token}}"`
1113

1214
## Inputs
1315
### `workflow`
14-
**Required.** The name of the workflow to trigger and run. This is the name decared in the YAML, not the filename
16+
**Required.** The name or ID of the workflow to trigger and run. This is the name declared in the YAML, not the filename
1517

1618
### `token`
1719

action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ inputs:
99
description: 'GitHub token with repo write access, can NOT use secrets.GITHUB_TOKEN, see readme'
1010
required: true
1111
inputs:
12-
description: 'Inputs to pass to the workflow, must be a JSON string.'
12+
description: 'Inputs to pass to the workflow, must be a JSON string'
1313
required: false
1414
ref:
1515
description: 'The reference of the workflow run. The reference can be a branch, tag, or a commit SHA'

dist/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ module.exports = require("child_process");
500500

501501
"use strict";
502502

503+
// ----------------------------------------------------------------------------
504+
// Copyright (c) Ben Coleman, 2020
505+
// Licensed under the MIT License.
506+
//
507+
// Workflow Dispatch Action - Main task code
508+
// ----------------------------------------------------------------------------
503509
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
504510
if (k2 === undefined) k2 = k;
505511
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
@@ -531,7 +537,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
531537
Object.defineProperty(exports, "__esModule", { value: true });
532538
const core = __importStar(__webpack_require__(470));
533539
const github = __importStar(__webpack_require__(469));
534-
// async wrapper function
540+
//
541+
// Main task function (async wrapper)
542+
//
535543
function run() {
536544
return __awaiter(this, void 0, void 0, function* () {
537545
try {
@@ -541,15 +549,15 @@ function run() {
541549
// Optional inputs, with defaults
542550
const ref = core.getInput('ref') || github.context.ref;
543551
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`;
544-
// Decode inputs, these MUST be a valid JSON string
552+
// Decode inputs, this MUST be a valid JSON string
545553
let inputs = {};
546554
const inputsJson = core.getInput('inputs');
547555
if (inputsJson) {
548556
inputs = JSON.parse(inputsJson);
549557
}
550558
// Get octokit client for making API calls
551559
const octokit = github.getOctokit(token);
552-
// List workflows via API
560+
// List workflows in repo via API
553561
const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, {
554562
ref: ref,
555563
inputs: inputs
@@ -562,13 +570,13 @@ function run() {
562570
core.debug('### END: List Workflows response data');
563571
// Locate workflow by name as we need it's id
564572
const foundWorkflow = listResp.data.workflows.find((wf) => {
565-
// Match on name or id
573+
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
566574
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference);
567575
});
568576
if (!foundWorkflow)
569577
throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`);
570578
console.log(`Workflow id is: ${foundWorkflow.id}`);
571-
// Call workflow_dispatch API
579+
// Call workflow_dispatch API to trigger the workflow
572580
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
573581
ref: ref,
574582
inputs: inputs
@@ -580,7 +588,9 @@ function run() {
580588
}
581589
});
582590
}
583-
// Call the main task run
591+
//
592+
// Call the main task run function
593+
//
584594
run();
585595

586596

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "workflow-dispatch",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Trigger running GitHub Actions workflows",
55
"main": "dist/index.js",
66
"scripts": {

src/main.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
// ----------------------------------------------------------------------------
2+
// Copyright (c) Ben Coleman, 2020
3+
// Licensed under the MIT License.
4+
//
5+
// Workflow Dispatch Action - Main task code
6+
// ----------------------------------------------------------------------------
7+
18
import * as core from '@actions/core'
29
import * as github from '@actions/github'
310

4-
// async wrapper function
11+
//
12+
// Main task function (async wrapper)
13+
//
514
async function run(): Promise<void> {
615
try {
716
// Required inputs
@@ -11,7 +20,7 @@ async function run(): Promise<void> {
1120
const ref = core.getInput('ref') || github.context.ref
1221
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`
1322

14-
// Decode inputs, these MUST be a valid JSON string
23+
// Decode inputs, this MUST be a valid JSON string
1524
let inputs = {}
1625
const inputsJson = core.getInput('inputs')
1726
if(inputsJson) {
@@ -21,7 +30,7 @@ async function run(): Promise<void> {
2130
// Get octokit client for making API calls
2231
const octokit = github.getOctokit(token)
2332

24-
// List workflows via API
33+
// List workflows in repo via API
2534
const listResp = await octokit.request(`GET /repos/${repo}/actions/workflows`, {
2635
ref: ref,
2736
inputs: inputs
@@ -35,14 +44,15 @@ async function run(): Promise<void> {
3544

3645
// Locate workflow by name as we need it's id
3746
const foundWorkflow = listResp.data.workflows.find((wf: Record<string, string>) => {
38-
// Match on name or id
47+
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
3948
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference)
4049
})
50+
4151
if(!foundWorkflow) throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`)
4252

4353
console.log(`Workflow id is: ${foundWorkflow.id}`)
4454

45-
// Call workflow_dispatch API
55+
// Call workflow_dispatch API to trigger the workflow
4656
const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
4757
ref: ref,
4858
inputs: inputs
@@ -53,5 +63,7 @@ async function run(): Promise<void> {
5363
}
5464
}
5565

56-
// Call the main task run
66+
//
67+
// Call the main task run function
68+
//
5769
run()

0 commit comments

Comments
 (0)