---
url: /en/build/internal-steps/cnb/read-file.md
---
`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](#cnb-read-file-applicable-events)
* [Parameters](#cnb-read-file-parameters)
* [Output Results](#cnb-read-file-output)
* [Configuration Examples](#cnb-read-file-configuration-examples)

## Applicable Events {#cnb-read-file-applicable-events}

[All Events](../../trigger-rule.md#trigger-event)

## Parameters {#cnb-read-file-parameters}

### filePath

* type: String
* required: true

Local file path.

## Output Results {#cnb-read-file-output}

```json
{
  // Object read from the file
}
```

## Configuration Examples {#cnb-read-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.

```json title="myJson.json"
{
  "myVar": "myVar",
  "deep": {
    "myVar": "myVar in deep"
  },
  "deepWithEnvKey": {
    "myVar": "myVar in deepWithEnvKey"
  }
}
```

```yaml title=".cnb.yml"
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
```
