跳到主要内容

云托管

init

1. 接口描述

接口功能:初始化云托管代码项目

接口声明:init(params: { serverName: string; template?: string; targetPath?: string }): Promise<{ projectDir: string }>

2. 输入参数

字段必填类型说明
serverNameString服务名称,将作为项目目录名
templateString模板标识符,默认为'helloworld'
targetPathString目标路径,默认为当前目录

3. 返回结果

字段类型说明
projectDirString初始化后的项目目录

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
const { projectDir } = await manager.cloudrun.init({
serverName: "my-server",
template: "helloworld",
targetPath: "./projects",
});
console.log("项目已初始化到:", projectDir);
}

test();

download

1. 接口描述

接口功能:下载云托管服务代码到本地目录

接口声明:download(params: { serverName: string; targetPath: string }): Promise<void>

2. 输入参数

字段必填类型说明
serverNameString要下载的服务名称
targetPathString下载的目标路径(绝对或相对路径)

3. 返回结果

无直接返回数据,成功时 Promise 解析,失败时拒绝。

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
await manager.cloudrun.download({
serverName: "my-server",
targetPath: "./downloads",
});
console.log("代码下载完成");
}

test();

list

1. 接口描述

接口功能:获取云托管服务列表

接口声明:list(params?: { pageSize?: number; pageNum?: number; serverName?: string; serverType?: CloudrunServerType }): Promise<ICloudrunListResponse>

2. 输入参数

字段必填类型说明
pageSizeNumber每页数量,默认 10
pageNumNumber页码,默认 1
serverNameString服务名称筛选
serverTypeCloudrunServerType服务类型筛选(function/container)

3. 返回结果

字段类型说明
ServerListICloudrunServerBaseInfo[]服务列表数组
ServerList[].ServerNameString服务名称
ServerList[].DefaultDomainNameString默认服务域名
ServerList[].CustomDomainNameString自定义域名
ServerList[].StatusString服务状态(running/deploying/deploy_failed)
ServerList[].UpdateTimeString更新时间
ServerList[].AccessTypesString[]公网访问类型数组
ServerList[].CustomDomainNamesString[]自定义域名数组
ServerList[].ServerTypeString服务类型(function/container)
ServerList[].TrafficTypeString流量类型
TotalNumber服务总数
RequestIdString请求 ID

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
const { ServerList, Total } = await manager.cloudrun.list({
pageSize: 20,
pageNum: 1,
});
console.log(`${Total} 个服务:`, ServerList);
}

test();

detail

1. 接口描述

接口功能:查询云托管服务详情

接口声明:detail(params: { serverName: string }): Promise<ICloudrunDetailResponse>

2. 输入参数

字段必填类型说明
serverNameString要查询的服务名称

3. 返回结果

字段类型说明
BaseInfoICloudrunServerBaseInfo服务基本信息
BaseInfo.ServerNameString服务名称
BaseInfo.DefaultDomainNameString默认服务域名
BaseInfo.CustomDomainNameString自定义域名
BaseInfo.StatusString服务状态
BaseInfo.UpdateTimeString更新时间
BaseInfo.AccessTypesString[]公网访问类型
BaseInfo.CustomDomainNamesString[]自定义域名数组
BaseInfo.ServerTypeString服务类型
BaseInfo.TrafficTypeString流量类型
ServerConfigICloudrunServerBaseConfig服务配置信息
ServerConfig.EnvIdString环境 ID
ServerConfig.ServerNameString服务名称
ServerConfig.OpenAccessTypesString[]公网访问类型
ServerConfig.CpuNumberCPU 规格
ServerConfig.MemNumber内存规格
ServerConfig.MinNumNumber最小副本数
ServerConfig.MaxNumNumber最大副本数
ServerConfig.PolicyDetailsICloudrunHpaPolicy[]扩缩容配置
ServerConfig.PolicyDetails[].PolicyTypeString扩缩容类型,可选值:
- "cpu": 基于 CPU 利用率的扩缩容
- "mem": 基于内存利用率的扩缩容;
- "cpu/mem": 基于 CPU 和内存利用率的扩缩容
ServerConfig.PolicyDetails[].PolicyThresholdNumber扩缩容阈值(百分比),取值范围 0-100,如 60 表示当资源利用率达到 60%时触发扩缩容
ServerConfig.CustomLogsString日志采集路径
ServerConfig.EnvParamsString环境变量
ServerConfig.InitialDelaySecondsNumber延迟检测时间
ServerConfig.CreateTimeString创建时间
ServerConfig.PortNumber服务端口
ServerConfig.HasDockerfileBoolean是否有 Dockerfile
ServerConfig.DockerfileStringDockerfile 文件名
ServerConfig.BuildDirString构建目录
ServerConfig.LogTypeString日志类型
ServerConfig.LogSetIdStringCLS 日志集 ID
ServerConfig.LogTopicIdStringCLS 主题 ID
ServerConfig.LogParseTypeString日志解析类型
ServerConfig.TagString服务标签
ServerConfig.InternalAccessString内网访问开关
ServerConfig.InternalDomainString内网域名
ServerConfig.OperationModeString运行模式
ServerConfig.TimerScaleICloudrunTimerScale[]定时扩缩容配置
ServerConfig.TimerScale[].CycleTypeString循环类型,可选值:
- "none": 无循环
- "daily": 每日循环
- "weekly": 每周循环
- "monthly": 每月循环
ServerConfig.TimerScale[].StartDateString循环起始日期(格式: YYYY-MM-DD)
ServerConfig.TimerScale[].EndDateString循环结束日期(格式: YYYY-MM-DD)
ServerConfig.TimerScale[].StartTimeString起始时间(格式: HH:mm:ss)
ServerConfig.TimerScale[].EndTimeString结束时间(格式: HH:mm:ss)
ServerConfig.TimerScale[].ReplicaNumNumber副本个数(最小值: 0)
ServerConfig.EntryPointString[]Dockerfile EntryPoint 参数
ServerConfig.CmdString[]Dockerfile Cmd 参数
OnlineVersionInfosICloudrunOnlineVersionInfo[]在线版本信息
OnlineVersionInfos[].VersionNameString版本名称
OnlineVersionInfos[].ImageUrlString镜像 URL
OnlineVersionInfos[].FlowRatioString流量比例
RequestIdString请求 ID

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
const detail = await manager.cloudrun.detail({
serverName: "my-server",
});
console.log("服务详情:", detail);
}

test();

delete

1. 接口描述

接口功能:删除指定的云托管服务

接口声明:delete(params: { serverName: string }): Promise<IResponseInfo>

2. 输入参数

字段必填类型说明
serverNameString要删除的服务名称

3. 返回结果

字段类型说明
RequestIdString请求 ID

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
await manager.cloudrun.delete({
serverName: "my-server",
});
console.log("服务删除成功");
}

test();

deploy

1. 接口描述

接口功能:本地代码部署云托管服务

接口声明:deploy(params: { serverName: string; targetPath: string; deployInfo: { ReleaseType: ReleaseTypeEnum }; imageUrl?: string; serverConfig?: Partial<ICloudrunServerBaseConfig> }): Promise<IResponseInfo>

2. 输入参数

字段必填类型说明
serverNameString要部署的服务名称
targetPathString本地代码路径
deployInfoObject部署信息,包含:
ReleaseType: ReleaseTypeEnum - 发布类型,可选值:"GRAY"(灰度发布)、"FULL"(全量发布)
imageUrlString镜像地址,传入时走镜像部署模式,无需上传代码包
serverConfigPartial<ICloudrunServerBaseConfig>服务配置项,包含以下可选字段:
- OpenAccessTypes: string[] - 公网访问类型数组,可选值:
"OA" - 办公网访问
"PUBLIC" - 公网访问
"MINIAPP" - 小程序访问
"VPC" - VPC 访问
- Cpu: number - CPU 规格
- Mem: number - 内存规格
- MinNum: number - 最小实例数
- MaxNum: number - 最大实例数
- PolicyDetails: ICloudrunHpaPolicy[] - 扩缩容配置数组,每个元素包含:
PolicyType: string - 扩缩容类型,可选值:"cpu""mem""cpu/mem"
PolicyThreshold: number - 扩缩容阈值(百分比),如 60 表示 60%
- CustomLogs: string - 自定义日志配置
- EnvParams: string - 环境变量 JSON 字符串
- Port: number - 服务端口(函数型服务固定为 3000)
- Dockerfile: string - Dockerfile 文件名
- BuildDir: string - 构建目录
- InternalAccess: string - 内网访问开关
- InternalDomain: string - 内网域名
- EntryPoint: string[] - Dockerfile EntryPoint 参数
- Cmd: string[] - Dockerfile Cmd 参数
- InstallDependency: boolean - 是否在线安装依赖,true 则本地不打包 node_modules
- OperationMode: string - 运行模式
- SessionAffinity: string - 会话亲和性,可选值:"open""close"
- LogType: string - 日志类型,可选值:"none""default""custom"
- LogSetId: string - CLS 日志集 ID
- LogTopicId: string - CLS 主题 ID
- LogParseType: string - 日志解析类型,可选值:"json""line"
- Tag: string - 服务标签
- TimerScale: ICloudrunTimerScale[] - 定时扩缩容配置
- VpcConf: IVpcConf - VPC 网络配置,包含:
VpcId: string - VPC ID
VpcCIDR: string - VPC 网段
SubnetId: string - 子网 ID
SubnetCIDR: string - 子网网段
- VolumesConf: IVolumeConf[] - 存储挂载配置(COS/CFS)
- PublicNetConf: IPublicNetConf - 公网访问配置,包含:
PublicNetStatus: string - 公网状态,可选值:"ENABLE""DISABLE"

3. 返回结果

字段类型说明
RequestIdString请求 ID

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
await manager.cloudrun.deploy({
serverName: "my-server",
targetPath: "./my-project",
deployInfo: {
ReleaseType: "FULL",
},
serverConfig: {
Cpu: 0.5,
Mem: 1,
MinNum: 1,
MaxNum: 5,
VpcConf: {
VpcId: "vpc-xxxxxxxx",
VpcCIDR: "172.16.0.0/16",
SubnetId: "subnet-xxxxxxxx",

},
},
});
console.log("服务部署成功");
}

test();

镜像部署示例:

async function test() {
await manager.cloudrun.deploy({
serverName: "my-server",
targetPath: "./",
deployInfo: {
ReleaseType: "FULL",
},
imageUrl: "ccr.ccs.tencentyun.com/my-repo/my-image:latest",
});
console.log("服务部署成功");
}

test();

getTemplates

1. 接口描述

接口功能:获取云托管服务模板列表

接口声明:getTemplates(): Promise<ITemplate[]>

2. 输入参数

3. 返回结果

字段类型说明
ITemplate[]Array模板数组
[].identifierString模板唯一标识符
[].titleString模板标题
[].descriptionString模板描述
[].runtimeVersionString运行时版本
[].languageString编程语言
[].zipFileStoreString模板压缩包存储地址

4. 示例代码

import CloudBase from "@cloudbase/manager-node";

const manager = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId",
});

async function test() {
const templates = await manager.cloudrun.getTemplates();
console.log("可用模板:", templates);
templates.forEach((template) => {
console.log(`模板ID: ${template.identifier}`);
console.log(`标题: ${template.title}`);
console.log(`描述: ${template.description}`);
console.log(`运行时版本: ${template.runtimeVersion}`);
console.log(`语言: ${template.language}`);
console.log(`下载地址: ${template.zipFileStore}`);
});
}

test();

describeServerManageTask

1. 接口描述

接口功能:查询云托管服务的管理任务。未指定任务 ID 时,查询该服务当前最新的任务。

接口声明:describeServerManageTask(params: { serverName: string; taskId?: number; operatorRemark?: string }): Promise<{ IsExist?: boolean; Task?: IServerManageTaskInfo; RequestId?: string }>

2. 输入参数

字段必填类型说明
serverNameString服务名称
taskIdNumber任务 ID,默认 0,表示查询服务当前最新的任务
operatorRemarkString操作标识

3. 返回结果

字段类型说明
IsExistBoolean是否存在匹配的管理任务
TaskIServerManageTaskInfo管理任务信息,不存在时可能为空
Task.IdNumber任务 ID
Task.EnvIdString环境 ID
Task.ServerNameString服务名称
Task.ChangeTypeString变更类型
Task.ReleaseTypeString发布类型
Task.DeployTypeString部署类型
Task.PreVersionNameString上一个版本名称
Task.VersionNameString当前任务关联的版本名称
Task.StatusString任务状态
Task.StepsITaskStepInfo[]任务步骤信息,包含名称、状态、开始和结束时间、耗时及失败原因
Task.FailReasonString任务失败原因
Task.OperatorRemarkString操作标识
RequestIdString请求 ID

4. 示例代码

const result = await manager.cloudrun.describeServerManageTask({
serverName: "my-server",
});
console.log("是否存在任务:", result.IsExist);
console.log("任务信息:", result.Task);

describeVersionDetail

1. 接口描述

接口功能:查询云托管服务指定版本的详细配置和运行状态。

接口声明:describeVersionDetail(params: { ServerName: string; VersionName: string; Channel?: string }): Promise<IDescribeVersionDetailResponse>

2. 输入参数

字段必填类型说明
ServerNameString服务名称
VersionNameString版本名称
ChannelString渠道标识

3. 返回结果

字段类型说明
NameString服务名称
PortNumber服务端口
CpuNumberCPU 规格
MemNumber内存规格
MinNumNumber最小实例数
MaxNumNumber最大实例数
PolicyDetailsICloudrunHpaPolicy[]扩缩容策略
DockerfileStringDockerfile 文件名
BuildDirString构建目录
EnvParamsString环境变量
StatusString版本状态
CreatedTimeString创建时间
UpdatedTimeString更新时间
LogPathString日志采集路径
EntryPointString | null容器入口命令
CmdString | null容器启动命令
VpcConfIVpcConf | nullVPC 网络配置
VolumesConfIVolumeConf[] | null存储挂载配置
BuildPacksIBuildPacksInfo | null构建包信息,包含基础镜像、启动命令、语言、上传文件名和语言版本
RequestIdString请求 ID

4. 示例代码

const version = await manager.cloudrun.describeVersionDetail({
ServerName: "my-server",
VersionName: "my-server-0001",
});
console.log("版本状态:", version.Status);
console.log("CPU/内存:", version.Cpu, version.Mem);

submitServerRollback

1. 接口描述

接口功能:提交云托管服务版本回滚任务,将当前版本回滚至指定历史版本。

接口声明:submitServerRollback(params: { ServerName: string; CurrentVersionName: string; RollbackVersionName: string; OperatorRemark?: string }): Promise<ISubmitServerRollbackResponse>

2. 输入参数

字段必填类型说明
ServerNameString服务名称
CurrentVersionNameString当前版本名称
RollbackVersionNameString目标回滚版本名称
OperatorRemarkString操作标识

3. 返回结果

字段类型说明
TaskIdNumber回滚任务 ID,可通过 describeServerManageTask 查询任务状态
RequestIdString请求 ID

4. 示例代码

const { TaskId } = await manager.cloudrun.submitServerRollback({
ServerName: "my-server",
CurrentVersionName: "my-server-0002",
RollbackVersionName: "my-server-0001",
OperatorRemark: "rollback-release",
});
console.log("已提交回滚任务:", TaskId);

deleteCloudRunVersions

1. 接口描述

接口功能:批量删除指定的云托管版本。

接口声明:deleteCloudRunVersions(params: IDeleteCloudRunVersionsParams): Promise<IDeleteCloudRunVersionsResponse>

注意

该操作不可恢复。版本删除限制由服务端校验;请检查 FailVersions,确认每个版本的删除结果。

2. 输入参数

字段必填类型说明
IsDeleteServerBoolean是否删除服务;仅删除最后一个版本时生效
IsDeleteImageBoolean是否删除镜像;仅删除服务时生效
SimpleVersionsISimpleVersion[]待删除版本列表,不能为空
SimpleVersions[].EnvIdString环境 ID;不传时使用当前环境
SimpleVersions[].ServerNameString服务名称
SimpleVersions[].VersionNameString版本名称
OperatorRemarkString操作标识

3. 返回结果

字段类型说明
ResultString整体删除结果:succpartialfail
SuccessVersionsISuccessDeleteVersions[]删除成功的版本列表
SuccessVersions[].VersionISimpleVersion已删除的版本信息
SuccessVersions[].RequestIdString删除该版本的请求 ID
SuccessVersions[].ResultString删除结果
FailVersionsIFailDeleteVersions[]删除失败的版本列表
FailVersions[].VersionISimpleVersion删除失败的版本信息
FailVersions[].ErrorCodeNumber错误码
FailVersions[].ErrorMsgString错误信息
FailVersions[].RequestIdString删除该版本的请求 ID
RequestIdString请求 ID

4. 示例代码

const result = await manager.cloudrun.deleteCloudRunVersions({
IsDeleteServer: false,
IsDeleteImage: false,
SimpleVersions: [
{
ServerName: "my-server",
VersionName: "my-server-0001",
},
],
OperatorRemark: "remove-unused-version",
});

console.log("删除结果:", result.Result);
console.log("成功版本:", result.SuccessVersions);
console.log("失败版本:", result.FailVersions);

getDeployRecords

1. 接口描述

接口功能:获取云托管服务的部署记录,结果按部署时间倒序排列。

接口声明:getDeployRecords(params: { serverName: string }): Promise<IDescribeCloudRunDeployRecordResponse>

2. 输入参数

字段必填类型说明
serverNameString服务名称

3. 返回结果

字段类型说明
DeployRecordsICloudRunDeployRecordInfo[]部署记录列表
DeployRecords[].DeployIdString部署 ID
DeployRecords[].DeployTimeString部署时间
DeployRecords[].StatusString部署状态
DeployRecords[].RunIdString运行版本 ID,可用于查询运行日志
DeployRecords[].BuildIdNumber构建 ID,可用于查询构建日志
DeployRecords[].FlowRatioNumber流量比例,取值范围为 0-100
DeployRecords[].ImageUrlString镜像地址
DeployRecords[].ScaleStatusString扩缩容状态
DeployRecords[].HasTrafficBoolean是否已分配流量
DeployRecords[].TrafficTypeString流量类型
DeployRecords[].IsReleasingBoolean是否正在发布
RequestIdString请求 ID

4. 示例代码

const { DeployRecords } = await manager.cloudrun.getDeployRecords({
serverName: "my-server",
});
console.log("最新部署记录:", DeployRecords[0]);

getBuildLog

1. 接口描述

接口功能:查询云托管服务的构建日志。未传入 buildId 时,SDK 自动查询最新部署记录并获取其构建日志。

接口声明:getBuildLog(params: { serverName: string; buildId?: number }): Promise<IBuildLogResponse>

2. 输入参数

字段必填类型说明
serverNameString服务名称
buildIdNumber构建 ID;不传时使用最新部署记录的构建 ID

3. 返回结果

字段类型说明
LogIBuildLog构建日志信息
Log.TotalNumber日志总条数
Log.DeliveredNumber已返回的日志条数
Log.TextString日志内容
Log.MoreBoolean是否还有更多日志
Log.FailTypeString构建失败类型
Log.FailReasonString构建失败原因
RequestIdString请求 ID

4. 示例代码

const { Log } = await manager.cloudrun.getBuildLog({
serverName: "my-server",
});
console.log(Log.Text);
if (Log.FailReason) {
console.error("构建失败原因:", Log.FailReason);
}

getProcessLog

1. 接口描述

接口功能:根据运行版本 ID 查询云托管服务的运行日志。

接口声明:getProcessLog(params: { RunId: string }): Promise<IProcessLogResponse>

2. 输入参数

字段必填类型说明
RunIdString运行版本 ID,可通过 getDeployRecords 获取

3. 返回结果

字段类型说明
LogsString[]日志内容数组
RequestIdString请求 ID

4. 示例代码

const records = await manager.cloudrun.getDeployRecords({
serverName: "my-server",
});
const runId = records.DeployRecords[0]?.RunId;
if (!runId) {
throw new Error("未找到可查询运行日志的部署记录");
}

const { Logs } = await manager.cloudrun.getProcessLog({ RunId: runId });
console.log(Logs.join("\n"));