Skip to main content

Configuration File

cloudbaserc.json is the core configuration file for CloudBase projects, used to uniformly manage deployment configurations for both CLI and VS Code plugin. Through the configuration file, you can simplify command-line operations, implement multi-environment deployment, and dynamic configuration management.

The configuration file is mainly used in the following scenarios:

  • Cloud Function Deployment: Define function name, runtime, timeout, environment variables, and other configurations
  • Multi-environment Management: Support different environments such as development, testing, and production through environment variables and dynamic variables
  • Cross-tool Sharing: Share unified configuration between CLI and VS Code plugin, avoiding duplicate settings

Quick Start

When initializing a project with tcb init, a cloudbaserc.json configuration file will be automatically generated. You can also specify a custom configuration file path using the --config-file parameter.

# Initialize project and auto-generate cloudbaserc.json
tcb init

# Use custom configuration file
tcb deploy --config-file custom-config.json

Dynamic Variables

Overview

Starting from CLI version 0.9.1, the configuration file supports version 2.0 format, introducing the dynamic variables feature. By declaring "version": "2.0" in cloudbaserc.json, you can use {{}} syntax to dynamically retrieve configuration values from environment variables or other data sources.

💡 Note: Version 2.0 configuration file only supports JSON format

Basic Example:

{
"version": "2.0",
"envId": "{{env.ENV_ID}}",
"functionRoot": "./functions",
"functions": [
{
"name": "{{env.FUNCTION_NAME}}",
"timeout": 5
}
]
}

Data Sources

CloudBase provides multiple namespaces for accessing different data sources. Reference variables using the namespace.variableName format, for example {{tcb.envId}}.

Supported Data Sources:

NamespaceVariableDescriptionExample
tcbenvIdEnvironment ID specified in config file or CLI arg{{tcb.envId}}
utiluid24-character random string for unique identifiers{{util.uid}}
env*All environment variables loaded from .env file{{env.API_KEY}}

Environment Variables

CloudBase provides enhanced support for environment variables, helping you use different configurations across development stages (development, testing, production). Manage environment variables through .env files, with support for automatic loading of corresponding configurations based on run mode.

File Loading Rules

CloudBase supports the following .env file types:

.env                # Base configuration shared across all environments
.env.local # Local private configuration (recommended to add to .gitignore)
.env.[mode] # Mode-specific configuration (e.g., .env.production, .env.development)

Loading Order:

  1. Default Loading: .env and .env.local are always loaded
  2. Mode Loading: When using --mode <mode> parameter, additionally load .env.[mode] file
  3. Override Rules: .env.[mode] > .env.local > .env (later loaded files override variables with the same name)

Example:

# Deploy with test mode
tcb framework deploy --mode test

When executing the above command, the three files .env, .env.local, and .env.test will be loaded in order, and environment variables will be merged.

Best Practice

Store sensitive information such as API keys and database passwords in the .env.local file, and add it to .gitignore to prevent sensitive information leakage.

Usage Example

.env.local file:

DB_HOST=localhost
DB_USER=root
DB_PASSWORD=s1mpl3

cloudbaserc.json configuration:

{
"version": "2.0",
"envId": "xxx",
"functionRoot": "./functions",
"functions": [
{
"name": "database",
"envVariables": {
"DB_HOST": "{{env.DB_HOST}}",
"DB_USER": "{{env.DB_USER}}",
"DB_PASSWORD": "{{env.DB_PASSWORD}}"
}
}
]
}

Extended Syntax

In addition to basic key-value pairs, CloudBase supports compound key-value pair syntax in .env files, using the . symbol to build nested object and array structures.

Basic Key-Value Pairs

FOO=bar
VUE_APP_SECRET=secret

Compound Key-Value Pairs

Use the . symbol to add properties to the same key, supporting object and array nesting:

Book.Name=Test
Book.Publish=2020
Book.Authors.0=Jack
Book.Authors.1=Mike

Compilation Result:

The above configuration will be parsed into the following JSON object:

{
"Name": "Test",
"Publish": "2020",
"Authors": ["Jack", "Mike"]
}

Referencing in Configuration File

You can directly reference object properties in cloudbaserc.json:

{
"version": "2.0",
"envId": "xxx",
"functionRoot": "./functions",
"functions": [
{
"name": "app",
"envVariables": {
"BOOK_NAME": "{{env.Book.Name}}",
"FIRST_AUTHOR": "{{env.Book.Authors.0}}"
}
}
]
}
Note

When referencing an entire object (e.g., {{env.Book}}), it will be automatically converted to a JSON string during compilation:

{{env.Book}} → {"Name":"Test","Publish":"2020","Authors":["Jack","Mike"]}

Configuration Fields

The following are the main configuration fields supported by cloudbaserc.json and their descriptions.

version

  • Type: String
  • Description: Configuration file version number, currently supports "2.0". Defaults to "1.0" version when not specified
  • Example: "version": "2.0"

envId

  • Type: String
  • Description: CloudBase environment ID, the unique identifier of the environment
  • Example: "envId": "dev-abc123"

region

  • Type: String
  • Description: Region where the environment is located. Shanghai region can be omitted, other regions (such as Guangzhou, Beijing) must be filled in
  • Example: "region": "ap-guangzhou"

functionRoot

  • Type: String
  • Description: Directory where cloud function code is stored, path relative to project root directory
  • Example: "functionRoot": "./functions" or "functionRoot": "functions"

functions

  • Type: Array<CloudFunction>
  • Description: Array of function configuration items, each element describes the deployment configuration of a cloud function
  • Detailed Description: See Function Configuration Documentation

Example:

{
"functions": [
{
"name": "app",
"timeout": 10,
"runtime": "Nodejs16.13",
"envVariables": {
"API_KEY": "{{env.API_KEY}}"
}
}
]
}

Complete Configuration Example

The following is a complete example containing commonly used configurations:

{
"version": "2.0",
"envId": "{{env.TCB_ENV_ID}}",
"region": "ap-shanghai",
"functionRoot": "./functions",
"functions": [
{
"name": "api",
"timeout": 10,
"runtime": "Nodejs16.13",
"memorySize": 256,
"envVariables": {
"DB_HOST": "{{env.DB_HOST}}",
"API_KEY": "{{env.API_KEY}}"
},
"installDependency": true
},
{
"name": "task",
"timeout": 30,
"runtime": "Nodejs16.13",
"triggers": [
{
"name": "dailyTask",
"type": "timer",
"config": "0 0 2 * * * *"
}
]
}
]
}