Skip to content

构建代码包

由于智能体运行时的运行环境 (Linux debian9 或 debian10) 与本地的开发环境可能存在比较大的不同,这就导致一部分本地安装/构建的依赖 (比如第三方库) 或代码包,在线上无法正常运行。所以,Serverless Devs 开发者工具在 build 命令中,通过本地启动 Docker 容器的能力,在容器中进行项目的构建,以尽可能地保证构建出来的依赖/产物,在线上可以得到良好的使用。

⚠️ 注意:该命令对 Docker 有所依赖,所以在使用该命令时,需要先进行 Docker 安装,版本 >= 19.03。

目前支持的能力如下:

不同的运行时,在进行依赖安装/项目构建的时候,可能会有不同的依赖描述文件,其系统默认的对应关系如下:

  • Python: requirements.txt
  • NodeJS: package.json
  • Java: pom.xml
  • Custom: requirements.txt package.json pom.xml
  • Custom Container: dockerfile

⚠️ 注意:

  • 依赖描述文件需要放在 s.yamlcode 属性指定的文件夹里面。
  • 在部分语言完成项目构建之后,部署的时候可能会出现交互式操作,提醒用户是否要将安装的依赖路径加入到环境变量中,以便线上可以正确的加载到这些依赖内容。此时可以通过交互式的方法,根据提醒输入 y,也可以在部署时通过 -y 命令,默认进行环境变量等内容的添加。
  • apt-get.list 是非 Custom Container 的 runtime 均可以使用,详情见 apt-get.list 文件, 是一个可选项,绝大部分场景不需要。

命令解析

当执行命令 build -h / build --help 时,可以获取帮助文档。

参数解析

参数全称 参数缩写 参数含义
publish-layer 将构建后的产物发布成一个 Layer
use-sandbox 进入对应 runtime 的 sandbox 容器
custom-env build 时注入的自定义环境变量
custom-args 使用默认 build 行为时的附加参数,比如指定 pypi 或者 npm 源, 需要配合 use-dockeruse-buildkit 使用,默认是 use-docker
command 使用自定义命令
script-file 使用自定义脚本
dockerfile f 指定构建自定义镜像的文件, 构建 custom-container runtime 的镜像时使用
context custom-container 构建镜像时上下文, 构建 custom-container runtime 的镜像时使用

当前命令还支持部分全局参数(例如 -a/--access, --debug, --help 等),详情可参考 Serverless Devs 全局参数文档

操作案例

基础操作

Python 应用 为例:在具有 requirements.txt 的 Python 项目下,可以通过 s build 命令实现依赖安装:

Step 1. 开发编辑源代码

Step 2. 执行 s build 之后,自动根据 requirements.txtapt-get.list 下载对应的依赖到本地,并且和源码一起组成交付物,同时会提示完成依赖包的环境变量配置:

⌛ Steps for [build] of [test-py-agent]
====================

build-3.0.0: Pulling from aliyunfc/runtime-python3.12
Digest: sha256:55b362eb353734ee290d6142b60e62f32fb1da32e8ff5a2e0b888ada403a0efd
Status: Image is up to date for registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.12:build-3.0.0
registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.12:build-3.0.0

Ign:1 http://mirrors.aliyun.com/debian-archive/debian stretch InRelease
Get:2 http://mirrors.aliyun.com/debian-archive/debian stretch-backports InRelease [78.5 kB]
...

Fetched 329 kB in 0s (1487 kB/s)
Download complete and in download only mode
Preparing to unpack jq_1.5+dfsg-1.3_amd64.deb
Preparing to unpack libjq1_1.5+dfsg-1.3_amd64.deb
Preparing to unpack libonig4_6.1.3-2+deb9u2_amd64.deb
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting beautifulsoup4
  Downloading https://mirrors.aliyun.com/pypi/packages/57/f4/a69c20ee4f660081a7dedb1ac57f29be9378e04edfcb90c526b923d4bebc/beautifulsoup4-4.12.2-py3-none-any.whl (142 kB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 143.0/143.0 kB 1.7 MB/s eta 0:00:00
Collecting flask
  Downloading https://mirrors.aliyun.com/pypi/packages/fd/56/26f0be8adc2b4257df20c1c4260ddd0aa396cf8e75d90ab2f7ff99bc34f9/flask-2.3.3-py3-none-any.whl (96 kB)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.1/96.1 kB 1.8 MB/s eta 0:00:00
...
Installing collected packages: soupsieve, MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, beautifulsoup4, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-2.3.7 beautifulsoup4-4.12.2 blinker-1.6.2 click-8.1.7 flask-2.3.3 itsdangerous-2.1.2 soupsieve-2.5

[2024-01-01 10:00:00][INFO][agentDemo] You need to add a new configuration env configuration dependency in yaml to take effect. The configuration is as follows:
environmentVariables:
  LD_LIBRARY_PATH: /code/apt-archives/usr/local/lib:/code/apt-archives/usr/lib:/code/apt-archives/usr/lib/x86_64-linux-gnu:/code/apt-archives/usr/lib64:/code/apt-archives/lib:/code/apt-archives/lib/x86_64-linux-gnu:/code
  PYTHONPATH: /code/python
  PATH: /code/apt-archives/usr/bin:/code/python/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/code:/code/bin:/opt:/opt/bin

✔ [agentDemo] completed (27.54s)

Step 3. 按照提示在 s.yaml 中完成依赖包的环境变量配置,然后执行 s deploy 将整个交付物 zip 打包,创建智能体运行时。

最佳实践:

如果您觉得智能体代码包过大,影响您的部署效率,您可以将构建的第三方包直接发布成一个层,然后在 s.yaml 中引入这个层即可。只需执行 s build --publish-layer,您可以在输出日志看到如下提示:

environmentVariables:
  PYTHONPATH: /opt/python

然后在 s.yaml 中添加 layers 配置:

layers:
  - acs:fc:cn-hangzhou:123456789:layers/my-python-layer/versions/1

Custom Container 构建

对于 Custom Container 运行时,您可以使用自定义的 Dockerfile:

s build --dockerfile Dockerfile

构建完成后,会生成一个镜像,您可以直接在 s.yaml 中引用:

customContainerConfig:
  image: your-registry/your-image:latest

权限与策略说明

build 命令主要在本地执行,不需要特殊的云权限。但是如果您使用 --publish-layer 参数,推荐配置系统策略:AliyunAgentRunFullAccess