read file

cnb:read-file

Reads the content of a file and parses it to be used as output for subsequent tasks.

Using the ##[set-output key=value] command is more convenient for outputting variables, but it will also output the content to the logs, which is not suitable for sensitive information.

For sensitive information that is predetermined, you can use imports to import it. 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 that exist in remote repositories, while this built-in task only supports reading files that exist locally.

The file type is determined by the file extension. Currently, JSON, YAML (YML), and plain text (key=value structure) are supported.

# Applicable Events

所有事件

# Parameters

# filePath

  • type: String
  • required: true

本地文件路径

# Output Result

{
  // The object that was read
}

# Configuration Example

Write some variables needed for subsequent tasks to a file and use this built-in task to read the file and export them as environment variables for use in subsequent tasks.

myJson.json

{
  "myVar": "myVar",
  "deep": {
    "myVar": "myVar in deep"
  },
  "deepWithEnvKey": {
    "myVar": "myVar in deepWithEnvKey"
  }
}
# 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 # Specify the key to retrieve using an environment variable
        - name: echo env
          script:
            - echo $ENV_MY_VAR
            - echo $ENV_DEEP_MY_VAR
            - echo $ENV_ENV_KEY_MY_VAR