Skip to content

Commit aecd6e0

Browse files
committed
Preping for v1 release
1 parent b2ad6ce commit aecd6e0

File tree

15 files changed

+1348
-112
lines changed

15 files changed

+1348
-112
lines changed

.eslintrc.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = {
2+
env: {
3+
es6: true
4+
},
5+
extends: [
6+
'eslint:recommended',
7+
'plugin:@typescript-eslint/eslint-recommended',
8+
'plugin:@typescript-eslint/recommended'
9+
],
10+
parser: '@typescript-eslint/parser',
11+
parserOptions: {
12+
'project': 'tsconfig.json',
13+
'sourceType': 'module'
14+
},
15+
plugins: [
16+
'@typescript-eslint'
17+
],
18+
rules: {
19+
'no-trailing-spaces': 'error',
20+
'no-console': 'off',
21+
22+
'@typescript-eslint/semi': ['error', 'never'],
23+
'@typescript-eslint/indent': ['error', 2],
24+
'@typescript-eslint/member-delimiter-style': 'off',
25+
'@typescript-eslint/no-explicit-any': 'warn',
26+
'@typescript-eslint/no-unused-vars': ['error', { 'argsIgnorePattern': 'next|res|req' }],
27+
28+
'func-call-spacing': 'off',
29+
'@typescript-eslint/func-call-spacing': 'error',
30+
31+
'quotes': 'off',
32+
'@typescript-eslint/quotes': ['error', 'single'],
33+
34+
'comma-spacing': 'off',
35+
'@typescript-eslint/comma-spacing': ['error']
36+
}
37+
};

.github/workflows/echo-1.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Message Echo 1
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
message:
7+
required: true
8+
description: "Message to echo"
9+
10+
jobs:
11+
hello:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Echo message
15+
run: echo '${{ github.event.inputs.message }}'

.github/workflows/echo-2.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Message Echo 2
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
message:
7+
required: false
8+
default: "this is echo 2"
9+
description: "Message to echo"
10+
11+
jobs:
12+
hello:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Echo message
16+
run: echo '${{ github.event.inputs.message }}'

.github/workflows/hello.yaml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.github/workflows/test.yaml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Test Action
22

33
on:
4-
workflow_dispatch:
4+
push
55

66
jobs:
77
test:
@@ -10,8 +10,24 @@ jobs:
1010
- name: Check out repository
1111
uses: actions/checkout@v2
1212

13-
- name: Invoke the workflow-dispatch action
13+
- name: Invoke echo 1 workflow using this action
1414
uses: ./
1515
with:
16+
workflow: Message Echo 1
17+
token: ${{ secrets.PERSONAL_TOKEN }}
18+
inputs: '{"message": "blah blah"}'
19+
20+
- name: Invoke echo 2 workflow using this action
21+
uses: ./
22+
with:
23+
workflow: Message Echo 2
24+
token: ${{ secrets.PERSONAL_TOKEN }}
25+
26+
- name: Invoke external workflow using this action
27+
uses: ./
28+
with:
29+
workflow: Deploy To Kubernetes
30+
repo: benc-uk/dapr-store
31+
token: ${{ secrets.PERSONAL_TOKEN }}
1632
ref: master
17-
workflow-id: "534267"
33+

.vscode/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.fixAll.eslint": true
4+
},
5+
"eslint.format.enable": true,
6+
"[typescript]": {
7+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
8+
}
9+
}

README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,62 @@
1-
# Workflow Dispatch Action
1+
# GitHub Action for Dispatching Workflows
22

3-
This action triggers another GitHub Actions workflow, via the `workflow_dispatch` event
3+
This action triggers another GitHub Actions workflow, using the `workflow_dispatch` event.
4+
The workflow must be configured for this event type e.g. `on: [workflow_dispatch]`
5+
6+
This allows you to chain workflows, the classic use case is have a CI build workflow, trigger a CD release/deploy workflow when it completes. Allowing you to maintain separate workflows for CI and CD, and pass data between them as required.
7+
8+
For details of `workflow_dispatch` see [this blog post introducing this type of event trigger](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/)
9+
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
411

512
## Inputs
13+
### `workflow`
14+
**Required.** The name of the workflow to trigger and run.
615

7-
### `workflow-id`
16+
### `token`
817

9-
**Required** The id of thw workflow to trgger and run.
18+
**Required.** A GitHub access token (PAT) with write access to the repo in question. **NOTE.** The automatically provided token e.g. `${{ secrets.GITHUB_TOKEN }}` can not be used, GitHub prevents this token from being able to fire the `workflow_dispatch` and `repository_dispatch` event. [The reasons are explained in the docs](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token).
1019

11-
## Outputs
20+
The solution is to manually create a PAT and store it as a secret e.g. `${{ secrets.PERSONAL_TOKEN }}`
1221

13-
### `time`
22+
### `inputs`
23+
**Optional.** The inputs to pass to the workflow (if any are configured), this must be a JSON encoded string, e.g. `{ "myInput": "foobar" }`
1424

15-
The time we greeted you.
25+
### `ref`
26+
**Optional.** The Git reference used with the triggered workflow run. The reference can be a branch, tag, or a commit SHA. If omitted the context ref of the triggering workflow is used
1627

17-
## Example usage
28+
### `repo`
29+
**Optional.** The default behavior is to trigger workflows in the same repo as the triggering workflow, if you wish to trigger in another GitHub repo "externally", then provide the owner + repo name with slash between them e.g. `microsoft/vscode`
1830

19-
uses: actions/hello-world-javascript-action@v1
20-
with:
21-
who-to-greet: 'Mona the Octocat'
31+
32+
## Outputs
33+
None
34+
35+
36+
## Example usage
37+
```yaml
38+
- name: Invoke workflow without inputs
39+
uses: benc-uk/workflow-dispatch@v1
40+
with:
41+
workflow: My Workflow
42+
token: ${{ secrets.PERSONAL_TOKEN }}
43+
```
44+
45+
```yaml
46+
- name: Invoke workflow with inputs
47+
uses: benc-uk/workflow-dispatch@v1
48+
with:
49+
workflow: Another Workflow
50+
token: ${{ secrets.PERSONAL_TOKEN }}
51+
inputs: '{ "message": "blah blah", "debug": true }'
52+
```
53+
54+
```yaml
55+
- name: Invoke workflow in another repo with inputs
56+
uses: benc-uk/workflow-dispatch@v1
57+
with:
58+
workflow: Some Workflow
59+
repo: benc-uk/example
60+
token: ${{ secrets.PERSONAL_TOKEN }}
61+
inputs: '{ "message": "blah blah", "debug": true }'
62+
```

action.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ name: 'Workflow Dispatch'
22
description: 'Trigger another GitHub Actions workflow'
33

44
inputs:
5-
workflow-id:
6-
description: 'Id of workflow'
5+
workflow:
6+
description: 'Name of workflow to run'
77
required: true
8+
token:
9+
description: 'GitHub token with repo write access, can NOT use secrets.GITHUB_TOKEN, see readme'
10+
required: true
11+
inputs:
12+
description: 'Inputs to pass to the workflow, must be a JSON string.'
13+
required: false
814
ref:
915
description: 'The reference of the workflow run. The reference can be a branch, tag, or a commit SHA'
1016
required: false
17+
repo:
18+
description: 'Repo owner & name, slash separated, only set if invoking a workflow in a different repo'
19+
required: false
1120

1221
runs:
1322
using: 'node12'

dist/index.js

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports =
4040
/******/ // the startup function
4141
/******/ function startup() {
4242
/******/ // Load entry module and return exports
43-
/******/ return __webpack_require__(104);
43+
/******/ return __webpack_require__(131);
4444
/******/ };
4545
/******/
4646
/******/ // run startup
@@ -394,34 +394,6 @@ module.exports = require("os");
394394

395395
/***/ }),
396396

397-
/***/ 104:
398-
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
399-
400-
const core = __webpack_require__(470);
401-
const github = __webpack_require__(469);
402-
403-
try {
404-
const workflowId = core.getInput('workflow-id');
405-
const ref = core.getInput('ref') || github.context.ref;
406-
const token = core.getInput('token');
407-
408-
const octokit = github.getOctokit(token);
409-
410-
const owner = github.context.repo.owner;
411-
const repo = github.context.repo.repo;
412-
octokit.request(`POST repos/${owner}/${repo}/actions/workflows/${workflowId}/dispatches`, {
413-
ref: ref
414-
});
415-
416-
// Get the JSON webhook payload for the event that triggered the workflow
417-
const payload = JSON.stringify(github.context.payload, undefined, 2);
418-
console.log(`The event payload: ${payload}`);
419-
} catch (error) {
420-
core.setFailed(error.message);
421-
}
422-
423-
/***/ }),
424-
425397
/***/ 118:
426398
/***/ (function(module, __unusedexports, __webpack_require__) {
427399

@@ -521,6 +493,92 @@ exports.getApiBaseUrl = getApiBaseUrl;
521493

522494
module.exports = require("child_process");
523495

496+
/***/ }),
497+
498+
/***/ 131:
499+
/***/ (function(__unusedmodule, exports, __webpack_require__) {
500+
501+
"use strict";
502+
503+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
504+
if (k2 === undefined) k2 = k;
505+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
506+
}) : (function(o, m, k, k2) {
507+
if (k2 === undefined) k2 = k;
508+
o[k2] = m[k];
509+
}));
510+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
511+
Object.defineProperty(o, "default", { enumerable: true, value: v });
512+
}) : function(o, v) {
513+
o["default"] = v;
514+
});
515+
var __importStar = (this && this.__importStar) || function (mod) {
516+
if (mod && mod.__esModule) return mod;
517+
var result = {};
518+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
519+
__setModuleDefault(result, mod);
520+
return result;
521+
};
522+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
523+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
524+
return new (P || (P = Promise))(function (resolve, reject) {
525+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
526+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
527+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
528+
step((generator = generator.apply(thisArg, _arguments || [])).next());
529+
});
530+
};
531+
Object.defineProperty(exports, "__esModule", { value: true });
532+
const core = __importStar(__webpack_require__(470));
533+
const github = __importStar(__webpack_require__(469));
534+
// async wrapper function
535+
function run() {
536+
return __awaiter(this, void 0, void 0, function* () {
537+
try {
538+
// Required inputs
539+
const token = core.getInput('token');
540+
const workflowName = core.getInput('workflow');
541+
// Optional inputs, with defaults
542+
const ref = core.getInput('ref') || github.context.ref;
543+
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`;
544+
// Decode inputs, these MUST be a valid JSON string
545+
let inputs = {};
546+
const inputsJson = core.getInput('inputs');
547+
if (inputsJson) {
548+
inputs = JSON.parse(inputsJson);
549+
}
550+
// Get octokit client for making API calls
551+
const octokit = github.getOctokit(token);
552+
// List workflows via API
553+
const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, {
554+
ref: ref,
555+
inputs: inputs
556+
});
557+
if (listResp.status != 200)
558+
throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`);
559+
// Locate workflow by name as we need it's id
560+
const workflowFind = listResp.data.workflows.find((w) => {
561+
return w['name'] === workflowName;
562+
});
563+
if (!workflowFind)
564+
throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`);
565+
console.log(`Workflow id is: ${workflowFind.id}`);
566+
// Call workflow_dispatch API
567+
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
568+
ref: ref,
569+
inputs: inputs
570+
});
571+
core.info(`API response status: ${dispatchResp.status} 🚀`);
572+
}
573+
catch (error) {
574+
core.setFailed(error.message);
575+
}
576+
});
577+
}
578+
// Call the main task run
579+
run();
580+
581+
524582
/***/ }),
525583

526584
/***/ 141:

index.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)