From 3f5463b526e3b125e5ccf98d3d613dc1971ec157 Mon Sep 17 00:00:00 2001 From: Nick Fields Date: Tue, 29 Sep 2020 14:56:52 -0400 Subject: [PATCH] major: bump to v2 and added lots of examples --- .github/workflows/ci_cd.yml | 26 +++++------ README.md | 89 +++++++++++++++++++++++++++++++++++-- action.yml | 2 +- dist/index.js | 6 +-- src/index.js | 6 +-- 5 files changed, 105 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index d30bbd4..efc7866 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -71,7 +71,7 @@ jobs: expected: failure actual: ${{ steps.sad_path_error.outcome }} - - name: retry_on (timeout) fails early if nonzero encountered + - name: retry_on (timeout) fails early if error encountered id: retry_on_timeout_fail uses: ./ continue-on-error: true @@ -93,27 +93,27 @@ jobs: expected: 2 actual: ${{ steps.retry_on_timeout_fail.outputs.exit_code }} - - name: retry_on (nonzero) - id: retry_on_nonzero + - name: retry_on (error) + id: retry_on_error uses: ./ continue-on-error: true with: timeout_minutes: 1 max_attempts: 2 - retry_on: nonzero + retry_on: error command: node -e "process.exit(2)" - uses: nick-invision/assert-action@v1 with: expected: 2 - actual: ${{ steps.retry_on_nonzero.outputs.total_attempts }} + actual: ${{ steps.retry_on_error.outputs.total_attempts }} - uses: nick-invision/assert-action@v1 with: expected: failure - actual: ${{ steps.retry_on_nonzero.outcome }} + actual: ${{ steps.retry_on_error.outcome }} - uses: nick-invision/assert-action@v1 with: expected: 2 - actual: ${{ steps.retry_on_nonzero.outputs.exit_code }} + actual: ${{ steps.retry_on_error.outputs.exit_code }} # timeout tests (takes longer to run so run last) @@ -152,27 +152,27 @@ jobs: expected: failure actual: ${{ steps.retry_on_timeout.outcome }} - - name: retry_on (nonzero) fails early if timeout encountered - id: retry_on_nonzero_fail + - name: retry_on (error) fails early if timeout encountered + id: retry_on_error_fail uses: ./ continue-on-error: true with: timeout_seconds: 15 max_attempts: 2 - retry_on: nonzero + retry_on: error command: node -e "(async()=>await new Promise(r => setTimeout(r, 120000)))()" - uses: nick-invision/assert-action@v1 with: expected: 1 - actual: ${{ steps.retry_on_nonzero_fail.outputs.total_attempts }} + actual: ${{ steps.retry_on_error_fail.outputs.total_attempts }} - uses: nick-invision/assert-action@v1 with: expected: failure - actual: ${{ steps.retry_on_nonzero_fail.outcome }} + actual: ${{ steps.retry_on_error_fail.outcome }} - uses: nick-invision/assert-action@v1 with: expected: 1 - actual: ${{ steps.retry_on_nonzero_fail.outputs.exit_code }} + actual: ${{ steps.retry_on_error_fail.outputs.exit_code }} - name: sad-path (timeout minutes) id: sad_path_timeout_minutes diff --git a/README.md b/README.md index e0225e2..19dc1b9 100644 --- a/README.md +++ b/README.md @@ -24,16 +24,97 @@ Retries an Action step on failure or timeout. This is currently intended to repl **Optional** Number of seconds to wait while polling for command result. Defaults to `1` -## Example usage +### `retry_on` + +**Optional** Event to retry on. Currently supports [any (default), timeout, error]. + +## Outputs + +### `total_attempts` + +The final number of attempts made + +### `exit_code` + +The final exit code returned by the command + +### `exit_error` + +The final error returned by the command + +## Examples + +### Timeout in minutes ```yaml -uses: nick-invision/retry@v1 +uses: nick-invision/retry@v2 with: timeout_minutes: 10 max_attempts: 3 - command: npm install + command: npm run some-typically-slow-script +``` + +### Timeout in seconds + +```yaml +uses: nick-invision/retry@v2 +with: + timeout_seconds: 15 + max_attempts: 3 + command: npm run some-typically-fast-script +``` + +### Only retry after timeout + +```yaml +uses: nick-invision/retry@v2 +with: + timeout_seconds: 15 + max_attempts: 3 + retry_on: timeout + command: npm run some-typically-fast-script +``` + +### Only retry after error + +```yaml +uses: nick-invision/retry@v2 +with: + timeout_seconds: 15 + max_attempts: 3 + retry_on: error + command: npm run some-typically-fast-script +``` + +### Retry but allow failure and do something with output + +```yaml +- uses: nick-invision/retry@v2 + id: retry + # see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idcontinue-on-error + continue-on-error: true + with: + timeout_seconds: 15 + max_attempts: 3 + retry_on: error + command: node -e 'process.exit(99);' +- name: Assert that action failed + uses: nick-invision/assert-action@v1 + with: + expected: failure + actual: ${{ steps.retry.outcome }} +- name: Assert that action exited with expected exit code + uses: nick-invision/assert-action@v1 + with: + expected: 99 + actual: ${{ steps.retry.outputs.exit_code }} +- name: Assert that action made expected number of attempts + uses: nick-invision/assert-action@v1 + with: + expected: 3 + actual: ${{ steps.retry.outputs.total_attempts }} ``` ## Requirements -NodeJS is required for this action to run. This runs without issue on all GitHub hosted runners but if you are running into issues with this on self hosted runners ensure NodeJS is installed. +NodeJS is required for this action to run. This runs without issue on all GitHub hosted runners but if you are running into issues with this on self hosted runners ensure NodeJS is installed. diff --git a/action.yml b/action.yml index 491bbbe..5b1b47f 100644 --- a/action.yml +++ b/action.yml @@ -23,7 +23,7 @@ inputs: required: false default: 1 retry_on: - description: Event to retry on. Currently supported [any, timeout, nonzero] + description: Event to retry on. Currently supported [any, timeout, error] outputs: total_attempts: description: The final number of attempts made diff --git a/dist/index.js b/dist/index.js index c952f30..ca41487 100644 --- a/dist/index.js +++ b/dist/index.js @@ -533,11 +533,11 @@ async function runAction() { } catch (error) { if (attempt === MAX_ATTEMPTS) { throw new Error(`Final attempt failed. ${error.message}`); - } else if (!done && RETRY_ON == 'nonzero') { + } else if (!done && RETRY_ON === 'error') { // error: timeout throw error; - } else if (exit > 0 && RETRY_ON == 'timeout') { - // error: nonzero + } else if (exit > 0 && RETRY_ON === 'timeout') { + // error: error throw error; } else { warning(`Attempt ${attempt} failed. Reason: ${error.message}`); diff --git a/src/index.js b/src/index.js index a370342..8210c0c 100644 --- a/src/index.js +++ b/src/index.js @@ -117,11 +117,11 @@ async function runAction() { } catch (error) { if (attempt === MAX_ATTEMPTS) { throw new Error(`Final attempt failed. ${error.message}`); - } else if (!done && RETRY_ON == 'nonzero') { + } else if (!done && RETRY_ON === 'error') { // error: timeout throw error; - } else if (exit > 0 && RETRY_ON == 'timeout') { - // error: nonzero + } else if (exit > 0 && RETRY_ON === 'timeout') { + // error: error throw error; } else { warning(`Attempt ${attempt} failed. Reason: ${error.message}`);