cnb:read-file
About 304 wordsAbout 1 min
cnb:read-file
Reads the content of a specified file, which can be exported as an environment variable for use in subsequent tasks.
Using the ##[set-output key=value] command to output variables is more convenient but will display the content in the logs, making it unsuitable for sensitive information.
For sensitive information that is predetermined, you can use imports for importing, and for sensitive information generated during the build process, you can write it to a file and use this built-in task to read it.
imports only supports files existing in remote repositories, while this built-in task only supports reading files that exist locally.
The file type is determined by the suffix, currently supporting .json, .yml, .yaml, and plain text (key=value structure).
Applicable Events
Parameters
filePath
- type: String
- required: true
Local file path.
Output Results
{
// Object read from the file
}Configuration Examples
Write some variables needed for subsequent tasks to a file, then use this built-in task to read and export them as environment variables.
{
"myVar": "myVar",
"deep": {
"myVar": "myVar in deep"
},
"deepWithEnvKey": {
"myVar": "myVar in deepWithEnvKey"
}
}main:
push:
- env:
myKey: deepWithEnvKey
stages:
- name: write env file
script: echo "write env to myJson.json"
- name: export env
type: cnb:read-file
options:
filePath: myJson.json
exports:
myVar: ENV_MY_VAR
deep.myVar: ENV_DEEP_MY_VAR # Specify a multi-level key
$myKey.myVar: ENV_ENV_KEY_MY_VAR # Get the key specified through an environment variable
- name: echo env
script:
- echo $ENV_MY_VAR
- echo $ENV_DEEP_MY_VAR
- echo $ENV_ENV_KEY_MY_VAR