Skip to content

Commit

Permalink
Preping for v1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Jul 11, 2020
1 parent b2ad6ce commit aecd6e0
Show file tree
Hide file tree
Showing 15 changed files with 1,348 additions and 112 deletions.
37 changes: 37 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
env: {
es6: true
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended'
],
parser: '@typescript-eslint/parser',
parserOptions: {
'project': 'tsconfig.json',
'sourceType': 'module'
},
plugins: [
'@typescript-eslint'
],
rules: {
'no-trailing-spaces': 'error',
'no-console': 'off',

'@typescript-eslint/semi': ['error', 'never'],
'@typescript-eslint/indent': ['error', 2],
'@typescript-eslint/member-delimiter-style': 'off',
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unused-vars': ['error', { 'argsIgnorePattern': 'next|res|req' }],

'func-call-spacing': 'off',
'@typescript-eslint/func-call-spacing': 'error',

'quotes': 'off',
'@typescript-eslint/quotes': ['error', 'single'],

'comma-spacing': 'off',
'@typescript-eslint/comma-spacing': ['error']
}
};
15 changes: 15 additions & 0 deletions .github/workflows/echo-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Message Echo 1

on:
workflow_dispatch:
inputs:
message:
required: true
description: "Message to echo"

jobs:
hello:
runs-on: ubuntu-latest
steps:
- name: Echo message
run: echo '${{ github.event.inputs.message }}'
16 changes: 16 additions & 0 deletions .github/workflows/echo-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Message Echo 2

on:
workflow_dispatch:
inputs:
message:
required: false
default: "this is echo 2"
description: "Message to echo"

jobs:
hello:
runs-on: ubuntu-latest
steps:
- name: Echo message
run: echo '${{ github.event.inputs.message }}'
10 changes: 0 additions & 10 deletions .github/workflows/hello.yaml

This file was deleted.

22 changes: 19 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Test Action

on:
workflow_dispatch:
push

jobs:
test:
Expand All @@ -10,8 +10,24 @@ jobs:
- name: Check out repository
uses: actions/checkout@v2

- name: Invoke the workflow-dispatch action
- name: Invoke echo 1 workflow using this action
uses: ./
with:
workflow: Message Echo 1
token: ${{ secrets.PERSONAL_TOKEN }}
inputs: '{"message": "blah blah"}'

- name: Invoke echo 2 workflow using this action
uses: ./
with:
workflow: Message Echo 2
token: ${{ secrets.PERSONAL_TOKEN }}

- name: Invoke external workflow using this action
uses: ./
with:
workflow: Deploy To Kubernetes
repo: benc-uk/dapr-store
token: ${{ secrets.PERSONAL_TOKEN }}
ref: master
workflow-id: "534267"

9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.format.enable": true,
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
}
63 changes: 52 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
# Workflow Dispatch Action
# GitHub Action for Dispatching Workflows

This action triggers another GitHub Actions workflow, via the `workflow_dispatch` event
This action triggers another GitHub Actions workflow, using the `workflow_dispatch` event.
The workflow must be configured for this event type e.g. `on: [workflow_dispatch]`

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.

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/)

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

## Inputs
### `workflow`
**Required.** The name of the workflow to trigger and run.

### `workflow-id`
### `token`

**Required** The id of thw workflow to trgger and run.
**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).

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

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

The time we greeted you.
### `ref`
**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

## Example usage
### `repo`
**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`

uses: actions/hello-world-javascript-action@v1
with:
who-to-greet: 'Mona the Octocat'

## Outputs
None


## Example usage
```yaml
- name: Invoke workflow without inputs
uses: benc-uk/workflow-dispatch@v1
with:
workflow: My Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
```
```yaml
- name: Invoke workflow with inputs
uses: benc-uk/workflow-dispatch@v1
with:
workflow: Another Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
inputs: '{ "message": "blah blah", "debug": true }'
```
```yaml
- name: Invoke workflow in another repo with inputs
uses: benc-uk/workflow-dispatch@v1
with:
workflow: Some Workflow
repo: benc-uk/example
token: ${{ secrets.PERSONAL_TOKEN }}
inputs: '{ "message": "blah blah", "debug": true }'
```
13 changes: 11 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ name: 'Workflow Dispatch'
description: 'Trigger another GitHub Actions workflow'

inputs:
workflow-id:
description: 'Id of workflow'
workflow:
description: 'Name of workflow to run'
required: true
token:
description: 'GitHub token with repo write access, can NOT use secrets.GITHUB_TOKEN, see readme'
required: true
inputs:
description: 'Inputs to pass to the workflow, must be a JSON string.'
required: false
ref:
description: 'The reference of the workflow run. The reference can be a branch, tag, or a commit SHA'
required: false
repo:
description: 'Repo owner & name, slash separated, only set if invoking a workflow in a different repo'
required: false

runs:
using: 'node12'
Expand Down
116 changes: 87 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports =
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(104);
/******/ return __webpack_require__(131);
/******/ };
/******/
/******/ // run startup
Expand Down Expand Up @@ -394,34 +394,6 @@ module.exports = require("os");

/***/ }),

/***/ 104:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

const core = __webpack_require__(470);
const github = __webpack_require__(469);

try {
const workflowId = core.getInput('workflow-id');
const ref = core.getInput('ref') || github.context.ref;
const token = core.getInput('token');

const octokit = github.getOctokit(token);

const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
octokit.request(`POST repos/${owner}/${repo}/actions/workflows/${workflowId}/dispatches`, {
ref: ref
});

// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2);
console.log(`The event payload: ${payload}`);
} catch (error) {
core.setFailed(error.message);
}

/***/ }),

/***/ 118:
/***/ (function(module, __unusedexports, __webpack_require__) {

Expand Down Expand Up @@ -521,6 +493,92 @@ exports.getApiBaseUrl = getApiBaseUrl;

module.exports = require("child_process");

/***/ }),

/***/ 131:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const github = __importStar(__webpack_require__(469));
// async wrapper function
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
// Required inputs
const token = core.getInput('token');
const workflowName = core.getInput('workflow');
// Optional inputs, with defaults
const ref = core.getInput('ref') || github.context.ref;
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`;
// Decode inputs, these MUST be a valid JSON string
let inputs = {};
const inputsJson = core.getInput('inputs');
if (inputsJson) {
inputs = JSON.parse(inputsJson);
}
// Get octokit client for making API calls
const octokit = github.getOctokit(token);
// List workflows via API
const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, {
ref: ref,
inputs: inputs
});
if (listResp.status != 200)
throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`);
// Locate workflow by name as we need it's id
const workflowFind = listResp.data.workflows.find((w) => {
return w['name'] === workflowName;
});
if (!workflowFind)
throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`);
console.log(`Workflow id is: ${workflowFind.id}`);
// Call workflow_dispatch API
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
ref: ref,
inputs: inputs
});
core.info(`API response status: ${dispatchResp.status} 🚀`);
}
catch (error) {
core.setFailed(error.message);
}
});
}
// Call the main task run
run();


/***/ }),

/***/ 141:
Expand Down
22 changes: 0 additions & 22 deletions index.js

This file was deleted.

Loading

0 comments on commit aecd6e0

Please sign in to comment.