Skip to content

配置环境变量 ​

环境变量用于在不同运行环境中注入配置(如服务地址、密钥),避免将配置直接写在源码中。

定义环境变量 ​

在 manifest.yaml 中声明:

yaml
environment:
  variables:
    - key: REMOTE_PREFIX
      default: "https://remote.example.com"
      description: "远程服务地址前缀"
    - key: API_TOKEN
      default: ""
      description: "调用外部服务的访问令牌"

使用环境变量 ​

后端代码中读取 ​

环境变量会注入到应用的后端运行时, 可在 Resolver 等后端函数中 通过 process.env 读取。

javascript
import { Resolver } from "@pc-nexus/core";

const resolver = new Resolver();

resolver.define("getRemotePrefix", async () => {
    const remotePrefix = process.env.REMOTE_PREFIX;
    return remotePrefix;
});

export { resolver };

本地调试 ​

使用 nexus serve 本地调试时,若在本地临时覆盖或补充环境变量,变量名需加上前缀 NEXUS_USER_VAR_ 才会生效。

shell
export NEXUS_USER_VAR_REMOTE_PREFIX=https://remote.example.com
nexus serve -e development

配置环境变量 ​

根据应用的部署方式不同,需要在不同的地方配置环境变量。

公有云 ​

公有云按运行环境分别配置,修改变量后需重新部署才会生效,配置方式有两种。

CLI 配置 ​

shell
nexus variables set REMOTE_PREFIX https://remote.example.com -e development
nexus variables set API_TOKEN your-secret-token -e development --encrypt
nexus variables list -e development

开发者中心配置 ​

打开应用「环境管理」,在目标环境点击「配置」即可编辑。

image.png

私有部署 ​

私有部署中,应用安装后在 PingCode 管理后台配置环境变量:

  1. 进入「应用」→「应用管理」,打开目标应用
  2. 切换到「环境变量」页签
  3. 点击「配置」编辑变量取值后保存

企业微信截图_75f49657-c5f4-42db-8bd4-3541572bcd19.png

INFO

.npk 安装包不会携带开发者中心中已配置的变量值,每个私有部署环境需单独配置。

参考引用 ​

举报邮箱:abuse@worktile.com