mirror of https://github.com/actions/toolkit
Merge pull request #149 from actions/users/tihuang/statecommand
add core method to saveState and getState.pull/150/head^2
commit
e8d384d3af
|
@ -104,4 +104,37 @@ const result = await core.group('Do something async', async () => {
|
||||||
const response = await doSomeHTTPRequest()
|
const response = await doSomeHTTPRequest()
|
||||||
return response
|
return response
|
||||||
})
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Action state
|
||||||
|
|
||||||
|
You can use this library to save state and get state for sharing information between a given wrapper action:
|
||||||
|
|
||||||
|
**action.yml**
|
||||||
|
```yaml
|
||||||
|
name: 'Wrapper action sample'
|
||||||
|
inputs:
|
||||||
|
name:
|
||||||
|
default: 'GitHub'
|
||||||
|
runs:
|
||||||
|
using: 'node12'
|
||||||
|
main: 'main.js'
|
||||||
|
post: 'cleanup.js'
|
||||||
|
```
|
||||||
|
|
||||||
|
In action's `main.js`:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const core = require('@actions/core');
|
||||||
|
|
||||||
|
core.saveState("pidToKill", 12345);
|
||||||
|
```
|
||||||
|
|
||||||
|
In action's `cleanup.js`:
|
||||||
|
```js
|
||||||
|
const core = require('@actions/core');
|
||||||
|
|
||||||
|
var pid = core.getState("pidToKill");
|
||||||
|
|
||||||
|
process.kill(pid);
|
||||||
```
|
```
|
|
@ -17,7 +17,10 @@ const testEnvVars = {
|
||||||
INPUT_MY_INPUT: 'val',
|
INPUT_MY_INPUT: 'val',
|
||||||
INPUT_MISSING: '',
|
INPUT_MISSING: '',
|
||||||
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
|
'INPUT_SPECIAL_CHARS_\'\t"\\': '\'\t"\\ response ',
|
||||||
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces'
|
INPUT_MULTIPLE_SPACES_VARIABLE: 'I have multiple spaces',
|
||||||
|
|
||||||
|
// Save inputs
|
||||||
|
STATE_TEST_1: 'state_val'
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('@actions/core', () => {
|
describe('@actions/core', () => {
|
||||||
|
@ -165,6 +168,15 @@ describe('@actions/core', () => {
|
||||||
core.debug('\r\ndebug\n')
|
core.debug('\r\ndebug\n')
|
||||||
assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`])
|
assertWriteCalls([`::debug::%0D%0Adebug%0A${os.EOL}`])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('saveState produces the correct command', () => {
|
||||||
|
core.saveState('state_1', 'some value')
|
||||||
|
assertWriteCalls([`::save-state name=state_1,::some value${os.EOL}`])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('getState gets wrapper action state', () => {
|
||||||
|
expect(core.getState('TEST_1')).toBe('state_val')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// Assert that process.stdout.write calls called only with the given arguments.
|
// Assert that process.stdout.write calls called only with the given arguments.
|
||||||
|
|
|
@ -173,3 +173,27 @@ export async function group<T>(name: string, fn: () => Promise<T>): Promise<T> {
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
// Wrapper action state
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves state for current action, the state can only be retrieved by this action's post job execution.
|
||||||
|
*
|
||||||
|
* @param name name of the state to store
|
||||||
|
* @param value value to store
|
||||||
|
*/
|
||||||
|
export function saveState(name: string, value: string): void {
|
||||||
|
issueCommand('save-state', {name}, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of an state set by this action's main execution.
|
||||||
|
*
|
||||||
|
* @param name name of the state to get
|
||||||
|
* @returns string
|
||||||
|
*/
|
||||||
|
export function getState(name: string): string {
|
||||||
|
return process.env[`STATE_${name}`] || ''
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue