Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Added optional message when cancelling workflow. (#6)
  • Loading branch information
potiuk committed Oct 7, 2020
1 parent e9e87cb commit 0ba85cabd56ddb3e40adf6dc76ae4bf7ee88461e
Showing 4 changed files with 30 additions and 16 deletions.
@@ -105,14 +105,15 @@ and `schedule` events are no longer needed.

## Inputs

| Input | Required | Default | Comment |
|------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `token` | yes | | The github token passed from `${{ secrets.GITHUB_TOKEN }}` |
| `cancelMode` | no | `duplicates` | The mode to run cancel on. The available options are `duplicates`, `self`, `failedJobs`, `namedJobs` |
| `sourceRunId` | no | | Useful only in `workflow_run` triggered events. It should be set to the id of the workflow triggering the run `${{ github.event.workflow_run.id }}` in case cancel operation should cancel the source workflow. |
| `notifyPRCancel` | no | | Boolean. If set to true, it notifies the cancelled PRs with a comment containing reason why they are being cancelled. |
| `notifyPRMessageStart` | no | | Only for workflow_run events triggered by the PRs. If not empty, it notifies those PRs with the message specified at the start of the workflow - adding the link to the triggered workflow_run. |
| `jobNameRegexps` | no | | An array of job name regexps. Only runs containing any job name matching any of of the regexp in this array are considered for cancelling in `failedJobs` and `namedJobs` cancel modes. |
| Input | Required | Default | Comment |
|-------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `token` | yes | | The github token passed from `${{ secrets.GITHUB_TOKEN }}` |
| `cancelMode` | no | `duplicates` | The mode to run cancel on. The available options are `duplicates`, `self`, `failedJobs`, `namedJobs` |
| `sourceRunId` | no | | Useful only in `workflow_run` triggered events. It should be set to the id of the workflow triggering the run `${{ github.event.workflow_run.id }}` in case cancel operation should cancel the source workflow. |
| `notifyPRCancel` | no | | Boolean. If set to true, it notifies the cancelled PRs with a comment containing reason why they are being cancelled. |
| `notifyPRCancelMessage` | no | | Optional cancel message to use instead of the default one when notifyPRCancel is true. |
| `notifyPRMessageStart` | no | | Only for workflow_run events triggered by the PRs. If not empty, it notifies those PRs with the message specified at the start of the workflow - adding the link to the triggered workflow_run. |
| `jobNameRegexps` | no | | An array of job name regexps. Only runs containing any job name matching any of of the regexp in this array are considered for cancelling in `failedJobs` and `namedJobs` cancel modes. |

The job cancel modes work as follows:

@@ -380,6 +381,7 @@ jobs:
cancelMode: duplicates
token: ${{ secrets.GITHUB_TOKEN }}
notifyPRCancel: true
notifyPRCancelMessage: Cancelled because image building failed.
notifyPRMessageStart: |
Note! The Docker Images for the build are prepared in a separate workflow,
that you will not see in the list of checks.
@@ -16,6 +16,10 @@ inputs:
Boolean. If set to true, it notifies the cancelled PRs with a comment containing reason why
they are being cancelled.
required: false
notifyPRCancelMessage:
description: |
Optional cancel message to use instead of the default one when notifyPRCancel is true.
required: false
notifyPRMessageStart:
description: |
Only for workflow_run events triggered by the PRs. If not empty, it notifies those PRs with the
@@ -1756,7 +1756,7 @@ function findAndCancelRuns(octokit, selfRunId, sourceWorkflowId, sourceRunId, ow
}
for (const pullRequestNumber of pullRequestToNotify) {
const selfWorkflowRunUrl = `https://github.com/${owner}/${repo}/actions/runs/${selfRunId}`;
yield addCommentToPullRequest(octokit, owner, repo, pullRequestNumber, `[The Build Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}`);
yield addCommentToPullRequest(octokit, owner, repo, pullRequestNumber, `[The Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}`);
}
core.info('\n###### Finished cancelling runs ##########\n');
}
@@ -1832,7 +1832,7 @@ function getOrigin(octokit, runId, owner, repo) {
];
});
}
function performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRMessageStart, jobNameRegexps) {
function performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRCancelMessage, notifyPRMessageStart, jobNameRegexps) {
return __awaiter(this, void 0, void 0, function* () {
core.info('\n###################################################################################\n');
core.info(`All parameters: owner: ${owner}, repo: ${repo}, run id: ${sourceRunId}, ` +
@@ -1842,7 +1842,9 @@ function performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, own
let reason = '';
if (cancelMode === CancelMode.SELF) {
core.info(`# Cancelling source run: ${sourceRunId} for workflow ${sourceWorkflowId}.`);
reason = `The job has been cancelled by another workflow.`;
reason = notifyPRCancelMessage
? notifyPRCancelMessage
: `The job has been cancelled by another workflow.`;
}
else if (cancelMode === CancelMode.FAILED_JOBS) {
core.info(`# Cancel all runs for workflow ${sourceWorkflowId} where job names matching ${jobNameRegexps} failed.`);
@@ -1876,6 +1878,7 @@ function run() {
const eventName = getRequiredEnv('GITHUB_EVENT_NAME');
const cancelMode = core.getInput('cancelMode') || CancelMode.DUPLICATES;
const notifyPRCancel = (core.getInput('notifyPRCancel') || 'false').toLowerCase() === 'true';
const notifyPRCancelMessage = core.getInput('notifyPRCancelMessage');
const notifyPRMessageStart = core.getInput('notifyPRMessageStart');
const sourceRunId = parseInt(core.getInput('sourceRunId')) || selfRunId;
const jobNameRegexpsString = core.getInput('jobNameRegexps');
@@ -1923,8 +1926,8 @@ function run() {
body: `${notifyPRMessageStart} [The workflow run](${selfWorkflowRunUrl})`
});
}
const cancelledRuns = yield performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRMessageStart, jobNameRegexps);
core.setOutput('cancelledRuns', JSON.stringify(cancelledRuns));
const cancelledRuns = yield performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRCancelMessage, notifyPRMessageStart, jobNameRegexps);
verboseOutput('cancelledRuns', JSON.stringify(cancelledRuns));
});
}
run()
@@ -444,7 +444,7 @@ async function findAndCancelRuns(
owner,
repo,
pullRequestNumber,
`[The Build Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}`
`[The Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}`
)
}
core.info(
@@ -568,6 +568,7 @@ async function performCancelJob(
sourceEventName: string,
cancelMode: CancelMode,
notifyPRCancel: boolean,
notifyPRCancelMessage: string,
notifyPRMessageStart: string,
jobNameRegexps: string[]
): Promise<number[]> {
@@ -587,7 +588,9 @@ async function performCancelJob(
core.info(
`# Cancelling source run: ${sourceRunId} for workflow ${sourceWorkflowId}.`
)
reason = `The job has been cancelled by another workflow.`
reason = notifyPRCancelMessage
? notifyPRCancelMessage
: `The job has been cancelled by another workflow.`
} else if (cancelMode === CancelMode.FAILED_JOBS) {
core.info(
`# Cancel all runs for workflow ${sourceWorkflowId} where job names matching ${jobNameRegexps} failed.`
@@ -643,6 +646,7 @@ async function run(): Promise<void> {
(core.getInput('cancelMode') as CancelMode) || CancelMode.DUPLICATES
const notifyPRCancel =
(core.getInput('notifyPRCancel') || 'false').toLowerCase() === 'true'
const notifyPRCancelMessage = core.getInput('notifyPRCancelMessage')
const notifyPRMessageStart = core.getInput('notifyPRMessageStart')
const sourceRunId = parseInt(core.getInput('sourceRunId')) || selfRunId
const jobNameRegexpsString = core.getInput('jobNameRegexps')
@@ -732,11 +736,12 @@ async function run(): Promise<void> {
sourceEventName,
cancelMode,
notifyPRCancel,
notifyPRCancelMessage,
notifyPRMessageStart,
jobNameRegexps
)

core.setOutput('cancelledRuns', JSON.stringify(cancelledRuns))
verboseOutput('cancelledRuns', JSON.stringify(cancelledRuns))
}

run()

0 comments on commit 0ba85ca

Please sign in to comment.