|
| 1 | +# Github.Actions.Core |
| 2 | + |
| 3 | +> Core functions for setting results, logging, registering secrets and exporting variables across actions |
| 4 | +
|
| 5 | +This module exposes bindings to the [@actions/core module](https://github.com/actions/toolkit/tree/main/packages/core). |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +#### Inputs/Outputs |
| 10 | + |
| 11 | +Action inputs can be read with `getInput`. Outputs can be set with `setOutput` which makes them available to be mapped into inputs of other actions to ensure they are decoupled. |
| 12 | + |
| 13 | +```purescript |
| 14 | +void $ runExceptT do |
| 15 | + myInput <- Core.getInput' "inputName" |
| 16 | + liftEffect $ Core.setOutput "outputKey" "outputVal" |
| 17 | +``` |
| 18 | + |
| 19 | +#### Exporting variables |
| 20 | + |
| 21 | +Since each step runs in a separate process, you can use `exportVariable` to add it to this step and future steps environment blocks. |
| 22 | + |
| 23 | +```purescript |
| 24 | + Core.exportVariable { key: "envVar", val: "Val"} |
| 25 | +``` |
| 26 | + |
| 27 | +#### Setting a secret |
| 28 | + |
| 29 | +Setting a secret registers the secret with the runner to ensure it is masked in logs. |
| 30 | + |
| 31 | +```purescript |
| 32 | +example = do |
| 33 | + Core.setSecret "myPassword" |
| 34 | +``` |
| 35 | + |
| 36 | +#### PATH Manipulation |
| 37 | + |
| 38 | +To make a tool's path available in the path for the remainder of the job (without altering the machine or containers state), use `addPath`. The runner will prepend the path given to the jobs PATH. |
| 39 | + |
| 40 | +```purescript |
| 41 | +example = do |
| 42 | + Core.addPath "/path/to/mytool" |
| 43 | +``` |
| 44 | + |
| 45 | +#### Exit codes |
| 46 | + |
| 47 | +You should use this library to set the failing exit code for your action. If status is not set and the script runs to completion, that will lead to a success. |
| 48 | + |
| 49 | +```purescript |
| 50 | +task = do stuff that might fail here |
| 51 | +
|
| 52 | +example = do |
| 53 | + case runExceptT task of |
| 54 | + Left err -> Core.setFailed $ "Action failed with error " <> message err |
| 55 | + Right _ -> mempty -- task succeded |
| 56 | +``` |
| 57 | + |
| 58 | +#### Logging |
| 59 | + |
| 60 | +Finally, this library provides some utilities for logging. Note that debug logging is hidden from the logs by default. This behavior can be toggled by enabling the [Step Debug Logs](../../docs/action-debugging.md#step-debug-logs). |
| 61 | + |
| 62 | +```js |
| 63 | +task = do |
| 64 | + myInput <- Core.getInput' "input" |
| 65 | + Core.debug "" |
| 66 | + core.debug('Inside try block'); |
| 67 | + isDebug <- Core.isDebug |
| 68 | + liftEffect $ when isDebug do |
| 69 | + -- handle debug messages here |
| 70 | + Core.info "output to actions build log" |
| 71 | + |
| 72 | + |
| 73 | +example = do |
| 74 | + case runExceptT task of |
| 75 | + Left err -> Core.error $ "task failed with error " <> message err -- Note: action may still succeed |
| 76 | + Right _ -> mempty |
| 77 | +``` |
| 78 | +
|
| 79 | +This library can also wrap chunks of output in foldable groups. |
| 80 | +
|
| 81 | +```js |
| 82 | +example = do |
| 83 | + -- manually wrap output |
| 84 | + Core.startGroup "do some function" |
| 85 | + doSomeFunction |
| 86 | + Core.endGroup |
| 87 | + |
| 88 | + -- wrap an asynchronous function call |
| 89 | + result <- Core.group |
| 90 | + { name: "do something async" |
| 91 | + , fn: do |
| 92 | + -- get the result from something asynchronous in the Aff monad |
| 93 | + result <- taskInAff |
| 94 | + pure result |
| 95 | + } |
| 96 | +``` |
| 97 | +
|
| 98 | +#### Action state |
| 99 | +
|
| 100 | +You can use this library to save state and get state for sharing information between a given wrapper action: |
| 101 | +
|
| 102 | +**action.yml** |
| 103 | +```yaml |
| 104 | +name: 'Wrapper action sample' |
| 105 | +inputs: |
| 106 | + name: |
| 107 | + default: 'GitHub' |
| 108 | +runs: |
| 109 | + using: 'node12' |
| 110 | + main: 'main.js' |
| 111 | + post: 'cleanup.js' |
| 112 | +``` |
| 113 | +
|
| 114 | +In action's `main.js`: |
| 115 | + |
| 116 | +```js |
| 117 | +Core.saveState { name: "pidToKill", value: "12345" } |
| 118 | +``` |
| 119 | + |
| 120 | +In action's `cleanup.js`: |
| 121 | +```js |
| 122 | +pid <- Core.getState "pidToKill" |
| 123 | +-- Do something with pid |
| 124 | +``` |
0 commit comments