major: bump to v2 and added lots of examples

This commit is contained in:
Nick Fields
2020-09-29 14:56:52 -04:00
parent 915303cda5
commit 3f5463b526
5 changed files with 105 additions and 24 deletions

View File

@@ -71,7 +71,7 @@ jobs:
expected: failure expected: failure
actual: ${{ steps.sad_path_error.outcome }} 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 id: retry_on_timeout_fail
uses: ./ uses: ./
continue-on-error: true continue-on-error: true
@@ -93,27 +93,27 @@ jobs:
expected: 2 expected: 2
actual: ${{ steps.retry_on_timeout_fail.outputs.exit_code }} actual: ${{ steps.retry_on_timeout_fail.outputs.exit_code }}
- name: retry_on (nonzero) - name: retry_on (error)
id: retry_on_nonzero id: retry_on_error
uses: ./ uses: ./
continue-on-error: true continue-on-error: true
with: with:
timeout_minutes: 1 timeout_minutes: 1
max_attempts: 2 max_attempts: 2
retry_on: nonzero retry_on: error
command: node -e "process.exit(2)" command: node -e "process.exit(2)"
- uses: nick-invision/assert-action@v1 - uses: nick-invision/assert-action@v1
with: with:
expected: 2 expected: 2
actual: ${{ steps.retry_on_nonzero.outputs.total_attempts }} actual: ${{ steps.retry_on_error.outputs.total_attempts }}
- uses: nick-invision/assert-action@v1 - uses: nick-invision/assert-action@v1
with: with:
expected: failure expected: failure
actual: ${{ steps.retry_on_nonzero.outcome }} actual: ${{ steps.retry_on_error.outcome }}
- uses: nick-invision/assert-action@v1 - uses: nick-invision/assert-action@v1
with: with:
expected: 2 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) # timeout tests (takes longer to run so run last)
@@ -152,27 +152,27 @@ jobs:
expected: failure expected: failure
actual: ${{ steps.retry_on_timeout.outcome }} actual: ${{ steps.retry_on_timeout.outcome }}
- name: retry_on (nonzero) fails early if timeout encountered - name: retry_on (error) fails early if timeout encountered
id: retry_on_nonzero_fail id: retry_on_error_fail
uses: ./ uses: ./
continue-on-error: true continue-on-error: true
with: with:
timeout_seconds: 15 timeout_seconds: 15
max_attempts: 2 max_attempts: 2
retry_on: nonzero retry_on: error
command: node -e "(async()=>await new Promise(r => setTimeout(r, 120000)))()" command: node -e "(async()=>await new Promise(r => setTimeout(r, 120000)))()"
- uses: nick-invision/assert-action@v1 - uses: nick-invision/assert-action@v1
with: with:
expected: 1 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 - uses: nick-invision/assert-action@v1
with: with:
expected: failure expected: failure
actual: ${{ steps.retry_on_nonzero_fail.outcome }} actual: ${{ steps.retry_on_error_fail.outcome }}
- uses: nick-invision/assert-action@v1 - uses: nick-invision/assert-action@v1
with: with:
expected: 1 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) - name: sad-path (timeout minutes)
id: sad_path_timeout_minutes id: sad_path_timeout_minutes

View File

@@ -24,14 +24,95 @@ 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` **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 ```yaml
uses: nick-invision/retry@v1 uses: nick-invision/retry@v2
with: with:
timeout_minutes: 10 timeout_minutes: 10
max_attempts: 3 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 ## Requirements

View File

@@ -23,7 +23,7 @@ inputs:
required: false required: false
default: 1 default: 1
retry_on: retry_on:
description: Event to retry on. Currently supported [any, timeout, nonzero] description: Event to retry on. Currently supported [any, timeout, error]
outputs: outputs:
total_attempts: total_attempts:
description: The final number of attempts made description: The final number of attempts made

6
dist/index.js vendored
View File

@@ -533,11 +533,11 @@ async function runAction() {
} catch (error) { } catch (error) {
if (attempt === MAX_ATTEMPTS) { if (attempt === MAX_ATTEMPTS) {
throw new Error(`Final attempt failed. ${error.message}`); throw new Error(`Final attempt failed. ${error.message}`);
} else if (!done && RETRY_ON == 'nonzero') { } else if (!done && RETRY_ON === 'error') {
// error: timeout // error: timeout
throw error; throw error;
} else if (exit > 0 && RETRY_ON == 'timeout') { } else if (exit > 0 && RETRY_ON === 'timeout') {
// error: nonzero // error: error
throw error; throw error;
} else { } else {
warning(`Attempt ${attempt} failed. Reason: ${error.message}`); warning(`Attempt ${attempt} failed. Reason: ${error.message}`);

View File

@@ -117,11 +117,11 @@ async function runAction() {
} catch (error) { } catch (error) {
if (attempt === MAX_ATTEMPTS) { if (attempt === MAX_ATTEMPTS) {
throw new Error(`Final attempt failed. ${error.message}`); throw new Error(`Final attempt failed. ${error.message}`);
} else if (!done && RETRY_ON == 'nonzero') { } else if (!done && RETRY_ON === 'error') {
// error: timeout // error: timeout
throw error; throw error;
} else if (exit > 0 && RETRY_ON == 'timeout') { } else if (exit > 0 && RETRY_ON === 'timeout') {
// error: nonzero // error: error
throw error; throw error;
} else { } else {
warning(`Attempt ${attempt} failed. Reason: ${error.message}`); warning(`Attempt ${attempt} failed. Reason: ${error.message}`);