Skip to content

Commit

Permalink
Merge pull request #21 from the-actions-org/feat/improve-and-doc
Browse files Browse the repository at this point in the history
Feat/improve and doc
  • Loading branch information
fbiesse authored Jun 21, 2024
2 parents 7ef5140 + 6bf7225 commit 3133c5d
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/timeout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Sleep
run: sleep 1200s
run: sleep 120s
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
.env
.env
.idea
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this

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

*This action is a fork of `benc-uk/workflow-dispatch` to add support for waiting for workflow completion.*
*This action is a fork of `aurelien-baudet/workflow-dispatch` with a better management of run-name option and node20 support.*

## Inputs

Expand Down Expand Up @@ -105,16 +105,16 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
> Based on the value, result will be:
>
> * `output`: Multiline string
>
> ```log
>
> ```log
> <job-name> | <datetime> <message>
> <job-name> | <datetime> <message>
> ...
> ```
>
> * `json-output`: JSON string
>
> ```json
>
> ```json
> {
> "<job-name>": [
> {
Expand All @@ -135,7 +135,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
```yaml
- name: Invoke workflow without inputs. Wait for result
uses: aurelien-baudet/workflow-dispatch@v2
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: My Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
Expand All @@ -145,7 +145,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this

```yaml
- name: Invoke workflow without inputs. Don't wait for result
uses: aurelien-baudet/workflow-dispatch@v2
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: My Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
Expand All @@ -156,7 +156,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
```yaml
- name: Invoke workflow with inputs
uses: aurelien-baudet/workflow-dispatch@v2
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: Another Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
Expand All @@ -167,7 +167,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
```yaml
- name: Invoke workflow in another repo with inputs
uses: aurelien-baudet/workflow-dispatch@v2
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: Some Workflow
repo: benc-uk/example
Expand All @@ -180,7 +180,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
```yaml
- name: Invoke workflow and handle result
id: trigger-step
uses: aurelien-baudet/workflow-dispatch@v2
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: Another Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
Expand All @@ -194,7 +194,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
```yaml
- name: Invoke workflow and scrap output
id: trigger-step
uses: aurelien-baudet/workflow-dispatch@v2
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: Another Workflow
token: ${{ secrets.PERSONAL_TOKEN }}
Expand All @@ -220,7 +220,7 @@ jobs:
```yaml
- name: Invoke workflow and handle result
id: trigger-step
uses: aurelien-baudet/workflow-dispatch@v3
uses: the-actions-org/workflow-dispatch@v4
env:
RUN_NAME: ${{ github.repository }}/actions/runs/${{ github.run_id }}
with:
Expand Down Expand Up @@ -252,6 +252,9 @@ on:

Thanks to:

* [LudovicTOURMAN](https://github.com/LudovicTOURMAN )
* [Djontleman](https://github.com/Djontleman)
* [aurelien-baudet](https://github.com/aurelien-baudet)
* [samirergaibi](https://github.com/samirergaibi)
* [rui-ferreira](https://github.com/rui-ferreira)
* [robbertvdg](https://github.com/robbertvdg)
Expand Down
20 changes: 10 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29323,7 +29323,7 @@ function waitForCompletionOrTimeout(workflowHandler, checkStatusInterval, waitFo
}
function computeConclusion(start, waitForCompletionTimeout, result) {
if ((0, utils_1.isTimedOut)(start, waitForCompletionTimeout)) {
core.info(`Workflow wait timed out`);
core.info('Workflow wait timed out');
core.setOutput('workflow-conclusion', workflow_handler_1.WorkflowRunConclusion.TIMED_OUT);
throw new Error('Workflow run has failed due to timeout');
}
Expand Down Expand Up @@ -29358,7 +29358,7 @@ function run() {
const workflowHandler = new workflow_handler_1.WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName);
// Trigger workflow run
yield workflowHandler.triggerWorkflow(args.inputs);
core.info(`Workflow triggered 🚀`);
core.info('Workflow triggered 🚀');
if (args.displayWorkflowUrl) {
const url = yield getFollowUrl(workflowHandler, args.displayWorkflowUrlInterval, args.displayWorkflowUrlTimeout);
core.info(`You can follow the running workflow here: ${url}`);
Expand All @@ -29367,7 +29367,7 @@ function run() {
if (!args.waitForCompletion) {
return;
}
core.info(`Waiting for workflow completion`);
core.info('Waiting for workflow completion');
const { result, start } = yield waitForCompletionOrTimeout(workflowHandler, args.checkStatusInterval, args.waitForCompletionTimeout);
yield handleLogs(args, workflowHandler);
core.setOutput('workflow-id', result === null || result === void 0 ? void 0 : result.id);
Expand Down Expand Up @@ -29455,7 +29455,7 @@ function getArgs() {
? core.getInput('repo').split('/')
: [github.context.repo.owner, github.context.repo.repo];
// Decode inputs, this MUST be a valid JSON string
let inputs = parse(core.getInput('inputs'));
const inputs = parse(core.getInput('inputs'));
const displayWorkflowUrlStr = core.getInput('display-workflow-run-url');
const displayWorkflowUrl = displayWorkflowUrlStr && displayWorkflowUrlStr === 'true';
const displayWorkflowUrlTimeout = toMilliseconds(core.getInput('display-workflow-run-url-timeout'));
Expand Down Expand Up @@ -29501,13 +29501,13 @@ function formatDuration(duration) {
let minutesStr = minutes + '';
let secondsStr = seconds + '';
if (hours < 10) {
hoursStr = "0" + hoursStr;
hoursStr = '0' + hoursStr;
}
if (minutes < 10) {
minutesStr = "0" + minutesStr;
minutesStr = '0' + minutesStr;
}
if (seconds < 10) {
secondsStr = "0" + secondsStr;
secondsStr = '0' + secondsStr;
}
return hoursStr + 'h ' + minutesStr + 'm ' + secondsStr + 's';
}
Expand Down Expand Up @@ -29727,7 +29727,7 @@ class WorkflowHandler {
repo: this.repo
});
const workflows = workflowsResp.data.workflows;
(0, debug_1.debug)(`List Workflows`, workflows);
(0, debug_1.debug)('List Workflows', workflows);
// Locate workflow either by name or id
const workflowFind = workflows.find((workflow) => workflow.name === this.workflowRef || workflow.id.toString() === this.workflowRef);
if (!workflowFind)
Expand Down Expand Up @@ -29926,8 +29926,8 @@ function logHandlerFactory(mode) {
}
}
function escapeImportedLogs(str) {
return str.replace(/^/mg, "| ")
.replace(/##\[([^\]]+)\]/gm, "##<$1>");
return str.replace(/^/mg, '| ')
.replace(/##\[([^\]]+)\]/gm, '##<$1>');
}


Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/main.ts -o dist",
"lint": "eslint src/"
"lint": "eslint src/",
"lint-fix": "eslint src/ --fix"
},
"keywords": [
"github",
Expand Down
4 changes: 2 additions & 2 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export function debug(title: string, content: any) {
if (core.isDebug()) {
core.info(`::group::${title}`)
try {
core.debug(JSON.stringify(content, null, 3));
core.debug(JSON.stringify(content, null, 3))
} catch(e) {
core.debug(`Failed to serialize object, trying toString. Cause: ${e}`)
core.debug(content?.toString());
core.debug(content?.toString())
}
core.info('::endgroup::')
}
Expand Down
89 changes: 44 additions & 45 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,66 @@
// ----------------------------------------------------------------------------

import * as core from '@actions/core'
import * as github from '@actions/github'
import { formatDuration, getArgs, isTimedOut, sleep } from './utils';
import { WorkflowHandler, WorkflowRunConclusion, WorkflowRunResult, WorkflowRunStatus } from './workflow-handler';
import { handleWorkflowLogsPerJob } from './workflow-logs-handler';
import { formatDuration, getArgs, isTimedOut, sleep } from './utils'
import { WorkflowHandler, WorkflowRunConclusion, WorkflowRunResult, WorkflowRunStatus } from './workflow-handler'
import { handleWorkflowLogsPerJob } from './workflow-logs-handler'



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

async function waitForCompletionOrTimeout(workflowHandler: WorkflowHandler, checkStatusInterval: number, waitForCompletionTimeout: number) {
const start = Date.now();
let status;
let result;
const start = Date.now()
let status
let result
do {
await sleep(checkStatusInterval);
await sleep(checkStatusInterval)
try {
result = await workflowHandler.getWorkflowRunStatus();
status = result.status;
result = await workflowHandler.getWorkflowRunStatus()
status = result.status
core.debug(`Worflow is running for ${formatDuration(Date.now() - start)}. Current status=${status}`)
} catch(e: any) {
core.warning(`Failed to get workflow status: ${e.message}`);
core.warning(`Failed to get workflow status: ${e.message}`)
}
} while (status !== WorkflowRunStatus.COMPLETED && !isTimedOut(start, waitForCompletionTimeout));
} while (status !== WorkflowRunStatus.COMPLETED && !isTimedOut(start, waitForCompletionTimeout))
return { result, start }
}

function computeConclusion(start: number, waitForCompletionTimeout: number, result?: WorkflowRunResult) {
if (isTimedOut(start, waitForCompletionTimeout)) {
core.info(`Workflow wait timed out`);
core.setOutput('workflow-conclusion', WorkflowRunConclusion.TIMED_OUT);
throw new Error('Workflow run has failed due to timeout');
core.info('Workflow wait timed out')
core.setOutput('workflow-conclusion', WorkflowRunConclusion.TIMED_OUT)
throw new Error('Workflow run has failed due to timeout')
}

core.info(`Workflow completed with conclusion=${result?.conclusion}`);
const conclusion = result?.conclusion;
core.setOutput('workflow-conclusion', conclusion);
core.info(`Workflow completed with conclusion=${result?.conclusion}`)
const conclusion = result?.conclusion
core.setOutput('workflow-conclusion', conclusion)

if (conclusion === WorkflowRunConclusion.FAILURE) throw new Error('Workflow run has failed');
if (conclusion === WorkflowRunConclusion.CANCELLED) throw new Error('Workflow run was cancelled');
if (conclusion === WorkflowRunConclusion.TIMED_OUT) throw new Error('Workflow run has failed due to timeout');
if (conclusion === WorkflowRunConclusion.FAILURE) throw new Error('Workflow run has failed')
if (conclusion === WorkflowRunConclusion.CANCELLED) throw new Error('Workflow run was cancelled')
if (conclusion === WorkflowRunConclusion.TIMED_OUT) throw new Error('Workflow run has failed due to timeout')
}

async function handleLogs(args: any, workflowHandler: WorkflowHandler) {
try {
const workflowRunId = await workflowHandler.getWorkflowRunId()
await handleWorkflowLogsPerJob(args, workflowRunId);
await handleWorkflowLogsPerJob(args, workflowRunId)
} catch(e: any) {
core.error(`Failed to handle logs of triggered workflow. Cause: ${e}`);
core.error(`Failed to handle logs of triggered workflow. Cause: ${e}`)
}
}

Expand All @@ -75,34 +74,34 @@ async function handleLogs(args: any, workflowHandler: WorkflowHandler) {
//
async function run(): Promise<void> {
try {
const args = getArgs();
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName);
const args = getArgs()
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName)

// Trigger workflow run
await workflowHandler.triggerWorkflow(args.inputs);
core.info(`Workflow triggered 🚀`);
await 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);
core.info(`You can follow the running workflow here: ${url}`)
core.setOutput('workflow-url', url)
}

if (!args.waitForCompletion) {
return;
return
}

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

await handleLogs(args, workflowHandler);
await handleLogs(args, workflowHandler)

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

} catch (error: any) {
core.setFailed(error.message);
core.setFailed(error.message)
}
}

Expand Down
Loading

0 comments on commit 3133c5d

Please sign in to comment.