2019-09-09 11:58:49 -04:00
# :: Commands
2020-07-21 11:33:05 -04:00
The [core toolkit package ](https://github.com/actions/toolkit/tree/main/packages/core ) offers a number of convenience functions for
2019-09-09 11:58:49 -04:00
setting results, logging, registering secrets and exporting variables across actions. Sometimes, however, its useful to be able to do
these things in a script or other tool.
2019-10-01 12:56:09 -04:00
To allow this, we provide a special `::` syntax which, if logged to `stdout` on a new line, will allow the runner to perform special behavior on
2019-09-09 11:58:49 -04:00
your commands. The following commands are all supported:
### Set outputs
To set an output for the step, use `::set-output` :
``` sh
2019-10-23 11:06:34 -04:00
echo "::set-output name=FOO::BAR"
2019-09-09 11:58:49 -04:00
```
Running `steps.[step-id].outputs.FOO` in your Yaml will now give you `BAR`
``` yaml
steps :
- name : Set the value
id : step_one
2019-10-23 11:06:34 -04:00
run : echo "::set-output name=FOO::BAR"
2019-09-09 11:58:49 -04:00
- name : Use it
run : echo ${{ steps.step_one.outputs.FOO }}
```
2019-09-24 21:08:02 -04:00
This is wrapped by the core setOutput method:
``` javascript
export function setOutput ( name : string , value : string ) : void { }
```
### Register a secret
If a script or action does work to create a secret at runtime, it can be registered with the runner to be masked in logs.
2019-09-09 11:58:49 -04:00
2019-10-01 17:13:05 -04:00
To mask a value in the logs, use `::add-mask` :
2019-09-09 11:58:49 -04:00
``` sh
2019-10-23 11:06:34 -04:00
echo "::add-mask::mysecretvalue"
2019-09-24 21:08:02 -04:00
```
2019-10-01 17:13:05 -04:00
This is wrapped by the core setSecret method
2019-09-24 21:08:02 -04:00
``` javascript
2019-10-01 17:13:05 -04:00
function setSecret ( secret : string ) : void { }
2019-09-09 11:58:49 -04:00
```
Now, future logs containing BAR will be masked. E.g. running `echo "Hello FOO BAR World"` will now print `Hello FOO **** World` .
2023-05-25 11:07:37 +01:00
**WARNING ** The add-mask and setSecret commands only support single-line
2023-05-25 11:00:25 +01:00
secrets or multi-line secrets that have been escaped. `@actions/core`
`setSecret` will escape the string you provide by default. When an escaped
multi-line string is provided the whole string and each of its lines
individually will be masked. For example you can mask `first\nsecond\r\nthird`
using:
``` sh
echo "::add-mask::first%0Asecond%0D%0Athird"
```
This will mask `first%0Asecond%0D%0Athird` , `first` , `second` and `third` .
2019-11-01 10:05:01 -04:00
**WARNING ** Do **not ** mask short values if you can avoid it, it could render your output unreadable (and future steps' output as well).
2019-09-09 11:58:49 -04:00
For example, if you mask the letter `l` , running `echo "Hello FOO BAR World"` will now print `He*********o FOO BAR Wor****d`
2019-09-24 21:08:02 -04:00
### Group and Ungroup Log Lines
2021-04-27 16:41:51 +02:00
Emitting a group with a title will instruct the logs to create a collapsible region up to the next endgroup command.
2019-09-24 21:08:02 -04:00
``` bash
2019-10-23 11:06:34 -04:00
echo "::group::my title"
echo "::endgroup::"
2019-09-24 21:08:02 -04:00
```
This is wrapped by the core methods:
``` javascript
function startGroup ( name : string ) : void { }
function endGroup ( ) : void { }
```
2019-11-06 16:24:16 -05:00
### Problem Matchers
2021-04-27 16:41:51 +02:00
2019-11-06 16:24:16 -05:00
Problems matchers can be used to scan a build's output to automatically surface lines to the user that matches the provided pattern. A file path to a .json Problem Matcher must be provided. See [Problem Matchers ](problem-matchers.md ) for more information on how to define a Problem Matcher.
``` bash
echo "::add-matcher::eslint-compact-problem-matcher.json"
2019-11-15 16:02:47 -05:00
echo "::remove-matcher owner=eslint-compact::"
2019-11-06 16:24:16 -05:00
```
`add-matcher` takes a path to a Problem Matcher file
`remove-matcher` removes a Problem Matcher by owner
2021-04-27 16:41:51 +02:00
2019-10-18 15:35:13 -04:00
### Save State
2020-05-27 10:40:32 -04:00
Save a state to an environmental variable that can later be used in the main or post action.
2019-10-18 15:35:13 -04:00
``` bash
2019-10-23 11:06:34 -04:00
echo "::save-state name=FOO::foovalue"
2019-10-18 15:35:13 -04:00
```
2020-11-04 13:50:18 -05:00
Because `save-state` prepends the string `STATE_` to the name, the environment variable `STATE_FOO` will be available to use in the post or main action. See [Sending Values to the pre and post actions ](https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#sending-values-to-the-pre-and-post-actions ) for more information.
2020-05-27 10:40:32 -04:00
2019-09-09 11:58:49 -04:00
### Log Level
2020-04-13 13:25:54 -04:00
There are several commands to emit different levels of log output:
2019-09-09 11:58:49 -04:00
| log level | example usage |
|---|---|
2019-11-06 16:24:16 -05:00
| [debug ](action-debugging.md ) | `echo "::debug::My debug message"` |
2023-01-11 14:18:20 -05:00
| notice | `echo "::notice::My notice message"` |
2019-10-23 11:06:34 -04:00
| warning | `echo "::warning::My warning message"` |
| error | `echo "::error::My error message"` |
2023-01-11 14:18:20 -05:00
Additional syntax options are described at [the workflow command documentation ](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-a-debug-message ).
2020-04-13 13:25:54 -04:00
### Command Echoing
2021-04-27 16:41:51 +02:00
2020-04-24 02:55:31 -07:00
By default, the echoing of commands to stdout only occurs if [Step Debugging is enabled ](./action-debugging.md#How-to-Access-Step-Debug-Logs )
2020-04-13 13:25:54 -04:00
You can enable or disable this for the current step by using the `echo` command.
``` bash
echo "::echo::on"
```
You can also disable echoing.
``` bash
echo "::echo::off"
```
This is wrapped by the core method:
``` javascript
function setCommandEcho ( enabled : boolean ) : void { }
```
The `add-mask` , `debug` , `warning` and `error` commands do not support echoing.
2020-09-23 11:19:20 -04:00
### Command Prompt
2019-10-23 11:06:34 -04:00
CMD processes the `"` character differently from other shells when echoing. In CMD, the above snippets should have the `"` characters removed in order to correctly process. For example, the set output command would be:
2021-04-27 16:41:51 +02:00
2019-10-23 11:06:34 -04:00
``` cmd
echo ::set-output name=FOO::BAR
```
2020-09-23 11:19:20 -04:00
2021-04-27 16:41:51 +02:00
## Environment files
2020-09-23 11:19:20 -04:00
During the execution of a workflow, the runner generates temporary files that can be used to perform certain actions. The path to these files are exposed via environment variables. You will need to use the `utf-8` encoding when writing to these files to ensure proper processing of the commands. Multiple commands can be written to the same file, separated by newlines.
### Set an environment variable
To set an environment variable for future out of process steps, write to the file located at `GITHUB_ENV` or use the equivalent `actions/core` function
``` sh
echo "FOO=BAR" >> $GITHUB_ENV
```
Running `$FOO` in a future step will now return `BAR`
2021-04-27 16:41:51 +02:00
For multiline strings, you may use a heredoc style syntax with your choice of delimeter. In the below example, we use `EOF` .
2020-09-23 11:19:20 -04:00
```
steps:
- name: Set the value
id: step_one
run: |
echo 'JSON_RESPONSE<<EOF' >> $GITHUB_ENV
curl https://httpbin.org/json >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
```
This would set the value of the `JSON_RESPONSE` env variable to the value of the curl response.
The expected syntax for the heredoc style is:
2021-04-27 16:41:51 +02:00
2020-09-23 11:19:20 -04:00
```
{VARIABLE_NAME}<<{DELIMETER}
{VARIABLE_VALUE}
{DELIMETER}
```
This is wrapped by the core `exportVariable` method which sets for future steps but also updates the variable for this step.
``` javascript
export function exportVariable ( name : string , val : string ) : void { }
```
### PATH Manipulation
To prepend a string to PATH write to the file located at `GITHUB_PATH` or use the equivalent `actions/core` function
``` sh
echo "/Users/test/.nvm/versions/node/v12.18.3/bin" >> $GITHUB_PATH
```
Running `$PATH` in a future step will now return `/Users/test/.nvm/versions/node/v12.18.3/bin:{Previous Path}` ;
This is wrapped by the core addPath method:
2021-04-27 16:41:51 +02:00
2020-09-23 11:19:20 -04:00
``` javascript
export function addPath ( inputPath : string ) : void { }
```
### Powershell
Powershell does not use UTF8 by default. You will want to make sure you write in the correct encoding. For example, to set the path:
2021-04-27 16:41:51 +02:00
2020-09-23 11:19:20 -04:00
```
steps:
2020-10-08 16:43:28 -04:00
- run: echo "mypath" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
```