Skip to content

Commit

Permalink
v1.1 work
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Jul 14, 2020
1 parent e5a167b commit f240894
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Build & Test

on:
push:
branches: [ master ]
workflow_dispatch:

jobs:
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ This allows you to chain workflows, the classic use case is have a CI build work

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

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
*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

*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}}"`

## Inputs
### `workflow`
**Required.** The name of the workflow to trigger and run. This is the name decared in the YAML, not the filename
**Required.** The name or ID of the workflow to trigger and run. This is the name declared in the YAML, not the filename

### `token`

Expand Down
2 changes: 1 addition & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ inputs:
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.'
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'
Expand Down
22 changes: 16 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,12 @@ module.exports = require("child_process");

"use strict";

// ----------------------------------------------------------------------------
// Copyright (c) Ben Coleman, 2020
// Licensed under the MIT License.
//
// Workflow Dispatch Action - Main task code
// ----------------------------------------------------------------------------
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]; } });
Expand Down Expand Up @@ -531,7 +537,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const github = __importStar(__webpack_require__(469));
// async wrapper function
//
// Main task function (async wrapper)
//
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
Expand All @@ -541,15 +549,15 @@ function run() {
// 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
// Decode inputs, this 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
// List workflows in repo via API
const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, {
ref: ref,
inputs: inputs
Expand All @@ -562,13 +570,13 @@ function run() {
core.debug('### END: List Workflows response data');
// Locate workflow by name as we need it's id
const foundWorkflow = listResp.data.workflows.find((wf) => {
// Match on name or id
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference);
});
if (!foundWorkflow)
throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`);
console.log(`Workflow id is: ${foundWorkflow.id}`);
// Call workflow_dispatch API
// Call workflow_dispatch API to trigger the workflow
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
ref: ref,
inputs: inputs
Expand All @@ -580,7 +588,9 @@ function run() {
}
});
}
// Call the main task run
//
// Call the main task run function
//
run();


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workflow-dispatch",
"version": "1.0.0",
"version": "1.1.0",
"description": "Trigger running GitHub Actions workflows",
"main": "dist/index.js",
"scripts": {
Expand Down
24 changes: 18 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// ----------------------------------------------------------------------------
// Copyright (c) Ben Coleman, 2020
// Licensed under the MIT License.
//
// Workflow Dispatch Action - Main task code
// ----------------------------------------------------------------------------

import * as core from '@actions/core'
import * as github from '@actions/github'

// async wrapper function
//
// Main task function (async wrapper)
//
async function run(): Promise<void> {
try {
// Required inputs
Expand All @@ -11,7 +20,7 @@ async function run(): Promise<void> {
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
// Decode inputs, this MUST be a valid JSON string
let inputs = {}
const inputsJson = core.getInput('inputs')
if(inputsJson) {
Expand All @@ -21,7 +30,7 @@ async function run(): Promise<void> {
// Get octokit client for making API calls
const octokit = github.getOctokit(token)

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

// Locate workflow by name as we need it's id
const foundWorkflow = listResp.data.workflows.find((wf: Record<string, string>) => {
// Match on name or id
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference)
})

if(!foundWorkflow) throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`)

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

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

// Call the main task run
//
// Call the main task run function
//
run()

0 comments on commit f240894

Please sign in to comment.