0

Dockerfiles 通过--build-args. 这些变量对于 NextJS 是必需的,用于静态页面(调用远程 API),并且在构建的图像中“硬编码”。

Gitlab-CI AutoDevOps 有一个 ENV var 来传递这些 args ( AUTO_DEVOPS_BUILD_IMAGE_EXTRA_ARGS)。但这仅在您使用一个环境/图像时才可使用。当需要多个环境 (stagingproduction) 时,URL 会有所不同 (https://staging.exmpl.comhttps://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)并测试然后发布

然后我有两个图像。

有人请一个更好的主意吗?

先感谢您

4

1 回答 1

0

也许有几种方法可以做到这一点。如果您只关心构建工作,这很容易。

一种方法是做第二份工作,extends:build

build:
  variables:
    MY_ENV: staging

build production:
  extends: build
  variables:
    MY_ENV: production

另一种方法可能是为作业添加parallel:matrix:密钥build

build:
  parallel:
    matrix:
      - MY_ENV: staging
      - MY_ENV: production

但是请记住,如果下游的任何作业都build依赖于它的工件,那么您也需要管理这些工件。

例如:

build:
  variables:
    MY_ENV: staging

build production:
  extends: build
  variables:
    MY_ENV: production

# some other job inherited from a template that depends on `build`
# we also want two of these jobs for each environment
downstream:
  variables:
    MY_ENV: staging

# extend it for production
downstream production:
  extends: downstream
  dependencies:  # make sure we get artifacts from correct upstream build job
    - build production
  variables:
    MY_ENV: production
于 2022-02-09T06:31:01.133 回答