Skip to content

Commit

Permalink
Add support for filtering workflow dispatch runs by run-name.
Browse files Browse the repository at this point in the history
  • Loading branch information
ichengchang committed Aug 28, 2023
1 parent c5f467f commit 1a4ac51
Show file tree
Hide file tree
Showing 8 changed files with 929 additions and 878 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,33 @@ jobs:
with:
expected: failure
actual: ${{ steps.timeout-workflow.outcome }}

concurrent-echo-2-test:
needs: [build]
strategy:
matrix:
msg : [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
runs-on: ubuntu-latest
name: "concurrent-echo-2-test [trigger+wait|by workflow filename|confusing workflow run id should no longer happen]"
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- name: Download dist
uses: actions/download-artifact@v3
with:
name: build
path: dist
- name: Invoke concurrent echo-2 workflows using this action
id: concurrent-echo-2
uses: ./
with:
ref: ${{ github.event.pull_request.head.ref }}
run-name: "echo-02-${{ matrix.msg }}"
workflow: echo-02.yaml
token: ${{ secrets.BROADBOT_TOKEN }}
inputs: '{
"message": "${{ matrix.msg }}",
"run-name": "echo-02-${{ matrix.msg }}"
}'
6 changes: 5 additions & 1 deletion .github/workflows/echo-02.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
name: Message Echo 2

run-name: ${{ inputs.run-name }}
on:
workflow_dispatch:
inputs:
message:
description: "Message to echo"
required: false
default: "this is echo 2"
run-name:
description: 'Run Name of workflow dispatch'
required: false
default: ''

jobs:
echo:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ All values must be strings (even if they are used as booleans or numbers in the
### `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`

### `run-name` (since 4.0.0)
**Optional.** The default behavior is to get the remote run ID based on the latest workflow name and date, if you have multiple of the same workflow running at the same time it can point to an incorrect run id. You can specify the run name to fetch the run ID based on the actual run name.

### `wait-for-completion`
**Optional.** If `true`, this action will actively poll the workflow run to get the result of the triggered workflow. It is enabled by default. If the triggered workflow fails due to either `failure`, `timed_out` or `cancelled` then the step that has triggered the other workflow will be marked as failed too.

Expand Down
17 changes: 12 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9472,7 +9472,7 @@ function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const args = utils_1.getArgs();
const workflowHandler = new workflow_handler_1.WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);
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 🚀`);
Expand Down Expand Up @@ -9568,6 +9568,7 @@ function getArgs() {
const waitForCompletion = waitForCompletionStr && waitForCompletionStr === 'true';
const waitForCompletionTimeout = toMilliseconds(core.getInput('wait-for-completion-timeout'));
const checkStatusInterval = toMilliseconds(core.getInput('wait-for-completion-interval'));
const runName = core.getInput('run-name');
return {
token,
workflowRef,
Expand All @@ -9580,7 +9581,8 @@ function getArgs() {
displayWorkflowUrlInterval,
checkStatusInterval,
waitForCompletion,
waitForCompletionTimeout
waitForCompletionTimeout,
runName
};
}
exports.getArgs = getArgs;
Expand Down Expand Up @@ -9685,11 +9687,12 @@ const ofConclusion = (conclusion) => {
return WorkflowRunConclusion[key];
};
class WorkflowHandler {
constructor(token, workflowRef, owner, repo, ref) {
constructor(token, workflowRef, owner, repo, ref, runName) {
this.workflowRef = workflowRef;
this.owner = owner;
this.repo = repo;
this.ref = ref;
this.runName = runName;
this.triggerDate = 0;
// Get octokit client for making API calls
this.octokit = github.getOctokit(token);
Expand Down Expand Up @@ -9773,12 +9776,16 @@ class WorkflowHandler {
event: 'workflow_dispatch'
});
debug_1.debug('List Workflow Runs', response);
const runs = response.data.workflow_runs
let runs = response.data.workflow_runs
.filter((r) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
if (this.runName) {
core.info(`Filter by run-name: ${this.runName}`);
runs = runs.filter((r) => r.name.trim().toLowerCase() === this.runName.trim().toLowerCase());
}
debug_1.debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r) => ({
id: r.id,
name: r.name,
created_at: r.creatd_at,
created_at: r.created_at,
triggerDate: new Date(this.triggerDate).toISOString(),
created_at_ts: new Date(r.created_at).valueOf(),
triggerDateTs: this.triggerDate
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function computeConclusion(start: number, waitForCompletionTimeout: number, resu
async function run(): Promise<void> {
try {
const args = getArgs();
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName);

// Trigger workflow run
await workflowHandler.triggerWorkflow(args.inputs);
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function getArgs() {
const waitForCompletion = waitForCompletionStr && waitForCompletionStr === 'true';
const waitForCompletionTimeout = toMilliseconds(core.getInput('wait-for-completion-timeout'));
const checkStatusInterval = toMilliseconds(core.getInput('wait-for-completion-interval'));
const runName = core.getInput('run-name');

return {
token,
Expand All @@ -56,7 +57,8 @@ export function getArgs() {
displayWorkflowUrlInterval,
checkStatusInterval,
waitForCompletion,
waitForCompletionTimeout
waitForCompletionTimeout,
runName
};
}

Expand Down
11 changes: 8 additions & 3 deletions src/workflow-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class WorkflowHandler {
private workflowRef: string,
private owner: string,
private repo: string,
private ref: string) {
private ref: string,
private runName: string) {
// Get octokit client for making API calls
this.octokit = github.getOctokit(token)
}
Expand Down Expand Up @@ -134,12 +135,16 @@ export class WorkflowHandler {
});
debug('List Workflow Runs', response);

const runs = response.data.workflow_runs
let runs = response.data.workflow_runs
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
if (this.runName) {
core.info(`Filter by run-name: ${this.runName}`);
runs = runs.filter((r: any) => r.name.trim().toLowerCase() === this.runName.trim().toLowerCase());
}
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
id: r.id,
name: r.name,
created_at: r.creatd_at,
created_at: r.created_at,
triggerDate: new Date(this.triggerDate).toISOString(),
created_at_ts: new Date(r.created_at).valueOf(),
triggerDateTs: this.triggerDate
Expand Down
Loading

0 comments on commit 1a4ac51

Please sign in to comment.