Skip to content

Commit

Permalink
misc(info) Log the url of the triggered workflow even if we don't wai…
Browse files Browse the repository at this point in the history
…t for completion
  • Loading branch information
aurelien-baudet committed Jan 2, 2021
1 parent 26d8ae6 commit ced6293
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.20.0
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,23 @@ The solution is to manually create a PAT and store it as a secret e.g. `${{ secr
**Optional.** The time to wait to mark triggered workflow has timed out. The time must be suffixed by the time unit e.g. `10m`. Time unit can be `s` for seconds, `m` for minutes and `h` for hours. It has no effect if `wait-for-completion` is `false`. Default is `1h`

### `wait-for-completion-interval`
**Optional.** The time to wait between two polls for getting run status. The time must be suffixed by the time unit e.g. `10m`. Time unit can be `s` for seconds, `m` for minutes and `h` for hours. It has no effect if `wait-for-completion` is `false`. Default is `30s`.
**Optional.** The time to wait between two polls for getting run status. The time must be suffixed by the time unit e.g. `10m`. Time unit can be `s` for seconds, `m` for minutes and `h` for hours. It has no effect if `wait-for-completion` is `false`. Default is `1m`.
**/!\ Do not use a value that is too small to avoid `API Rate limit exceeded`**

### `display-worflow-run-url`
**Optional.** If `true`, it displays in logs the URL of the triggered workflow. It is useful to follow the progress of the triggered workflow. It is enabled by default.

### `display-worflow-run-url-timeout`
**Optional.** The time to wait for getting the workflow run URL. If the timeout is reached, it doesn't fail and continues. Displaying the workflow URL is just for information purpose. The time must be suffixed by the time unit e.g. `10m`. Time unit can be `s` for seconds, `m` for minutes and `h` for hours. It has no effect if `display-worflow-run-url` is `false`. Default is `10m`

### `display-worflow-run-url-interval`
**Optional.** The time to wait between two polls for getting workflow run URL. The time must be suffixed by the time unit e.g. `10m`. Time unit can be `s` for seconds, `m` for minutes and `h` for hours. It has no effect if `display-worflow-run-url` is `false`. Default is `1m`.
**/!\ Do not use a value that is too small to avoid `API Rate limit exceeded`**

## Outputs
### `workflow-url`
The URL of the workflow run that has been triggered. It may be undefined if the URL couldn't be retrieved (timeout reached) or if `wait-for-completion` and `display-worflow-run-url` are both `false`

### `workflow-conclusion`
The result of the triggered workflow. May be one of `success`, `failure`, `cancelled`, `timed_out`, `skipped`, `neutral`, `action_required`. The step in your workflow will fail if the triggered workflow completes with `failure`, `cancelled` or `timed_out`. Other workflow conlusion are considered success.
Only available if `wait-for-completion` is `true`
Expand Down
14 changes: 13 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ inputs:
repo:
description: 'Repo owner & name, slash separated, only set if invoking a workflow in a different repo'
required: false
display-workflow-run-url:
description: 'Get the URL of the triggered workflow and display it in logs (useful to follow the progress of the triggered workflow)'
required: false
default: true
display-workflow-run-url-interval:
description: 'The time to wait (+unit) between two polls to get the URL of the workflow run'
required: false
default: 1m
display-workflow-run-url-timeout:
description: 'Maximum amount of time (+unit) to wait for the URL of the workflow run. If the timeout is reached, it is just ignored'
required: false
default: 10m
wait-for-completion:
description: 'Block until the triggered workflow has finished'
required: false
Expand All @@ -28,7 +40,7 @@ inputs:
wait-for-completion-interval:
description: 'Time to wait (+unit) between two polls to get run status'
required: false
default: 30s
default: 1m

runs:
using: 'node12'
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-and-wait",
"version": "2.0.0",
"version": "2.1.0",
"description": "Trigger running GitHub Actions workflows and wait for result",
"main": "dist/index.js",
"scripts": {
Expand Down
29 changes: 24 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@ import * as github from '@actions/github'
import { formatDuration, getArgs, isTimedOut, sleep } from './utils';
import { WorkflowHandler, WorkflowRunConclusion, WorkflowRunResult, WorkflowRunStatus } from './workflow-handler';



async function getFollowUrl(workflowHandler: WorkflowHandler, interval: number, timeout: number) {
const start = Date.now();
let url;
do {
await sleep(interval);
try {
const result = await workflowHandler.getWorkflowRunStatus();
url = result.url;
} catch(e) {
core.debug(`Failed to get workflow url: ${e.message}`);
}
} while (!url && !isTimedOut(start, timeout));
return url;
}

async function waitForCompletionOrTimeout(workflowHandler: WorkflowHandler, checkStatusInterval: number, waitForCompletionTimeout: number) {
const start = Date.now();
let first = true;
let status;
let result;
do {
await sleep(checkStatusInterval);
try {
result = await workflowHandler.getWorkflowRunStatus();
status = result.status;
if (first) {
core.info(`You can follow the running workflow here: ${result.url}`);
first = false;
}
core.debug(`Worflow is running for ${formatDuration(Date.now() - start)}. Current status=${status}`)
} catch(e) {
core.warning(`Failed to get workflow status: ${e.message}`);
Expand Down Expand Up @@ -60,13 +72,20 @@ async function run(): Promise<void> {
workflowHandler.triggerWorkflow(args.inputs);
core.info(`Workflow triggered 🚀`);

if (args.displayWorkflowUrl) {
const url = await getFollowUrl(workflowHandler, args.displayWorkflowUrlInterval, args.displayWorkflowUrlTimeout)
core.info(`You can follow the running workflow here: ${url}`);
core.setOutput('workflow-url', url);
}

if (!args.waitForCompletion) {
return;
}

core.info(`Waiting for workflow completion`);
const { result, start } = await waitForCompletionOrTimeout(workflowHandler, args.checkStatusInterval, args.waitForCompletionTimeout);

core.setOutput('workflow-url', result?.url);
computeConclusion(start, args.waitForCompletionTimeout, result);

} catch (error) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export function getArgs() {
inputs = JSON.parse(inputsJson);
}

const displayWorkflowUrlStr = core.getInput('display-workflow-run-url');
const displayWorkflowUrl = displayWorkflowUrlStr && displayWorkflowUrlStr === 'true';
const displayWorkflowUrlTimeout = toMilliseconds(core.getInput('display-workflow-run-url-timeout'));
const displayWorkflowUrlInterval = toMilliseconds(core.getInput('display-workflow-run-url-interval'));

const waitForCompletionStr = core.getInput('wait-for-completion');
const waitForCompletion = waitForCompletionStr && waitForCompletionStr === 'true';
const waitForCompletionTimeout = toMilliseconds(core.getInput('wait-for-completion-timeout'));
Expand All @@ -46,6 +51,9 @@ export function getArgs() {
owner,
repo,
inputs,
displayWorkflowUrl,
displayWorkflowUrlTimeout,
displayWorkflowUrlInterval,
checkStatusInterval,
waitForCompletion,
waitForCompletionTimeout
Expand Down

0 comments on commit ced6293

Please sign in to comment.