Dockerfiles 通过--build-args
. 这些变量对于 NextJS 是必需的,用于静态页面(调用远程 API),并且在构建的图像中“硬编码”。
Gitlab-CI AutoDevOps 有一个 ENV var 来传递这些 args ( AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS
)。但这仅在您使用一个环境/图像时才可使用。当需要多个环境 (staging
和production
) 时,URL 会有所不同 (https://staging.exmpl.com
和https://www.exmpl.com
)。
如何修改 Gitlab AutoDevOps 以构建两个不同的图像?
在 CI/CD 设置中,我AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS
设置了:
--build-arg=API_URL=https://staging.exmpl.at/backend --build-arg=NEXT_PUBLIC_API_URL=https://staging.exmpl.at/backend
# as well $NEXT_PUBLIC_API_URL is set there
目前这是我的完整gitlab-ci.yml
:
include:
- template: Auto-DevOps.gitlab-ci.yml
# added vars for build
build:
stage: build
variables:
API_URL: $NEXT_PUBLIC_API_URL
NEXT_PUBLIC_API_URL: $NEXT_PUBLIC_API_URL
如何在不“离开” AutoDevOps 的情况下构建两个映像?我假设我必须自定义构建阶段。
另一个想法是创建一个名为production的第二个 Git 存储库,其生产 URL 设置为$NEXT_PUBLIC_API_URL
:
- 暂存构建并运行测试。
- 如果成功,它将被发布
- 暂存仓库内容将被复制到生产仓库
- 生产 repo 被构建(带有生产 URL)并测试然后发布
然后我有两个图像。
有人请一个更好的主意吗?
先感谢您