GCP cloudbuild反应脚本构建找不到env.file

发布于 2025-01-09 20:26:51 字数 2313 浏览 1 评论 0原文

我正在做一些我认为很简单的事情:

# Fetch config
- name: 'gcr.io/cloud-builders/gsutil'
  volumes:
    - name: 'vol1'
    path: '/persistent_volume'
  args: [ 'cp', 'gs://servicesconfig/devs/react-app/env.server', '/persistent_volume/env.server' ]

# Install dependencies
- name: node:$_NODE_VERSION
  entrypoint: 'yarn'
  args: [ 'install' ]

  # Build project
  - name: node:$_NODE_VERSION
    volumes:
      - name: 'vol1'
        path: '/persistent_volume'
    entrypoint: 'bash'
    args:
      - -c
      - |
        cp /persistent_volume/env.server .env.production &&
        cat .env.production &&
        ls -la &&
        yarn run build:prod

在我的 package.json 中:

"build:prod": "sh -ac '. .env.production; react-scripts build'",

所有这些在本地都运行良好,但 gcp 云构建中的输出:

    Already have image: node:14
REACT_APP_ENV="sandbox"
REACT_APP_CAPTCHA_ENABLED=true
REACT_APP_CAPTCHA_PUBLIC_KEY="akey"
REACT_APP_DEFAULT_APP="home-btn"
REACT_APP_API_URL="akey2"
REACT_APP_STRIPE_KEY="akey3"
REACT_APP_COGNITO_POOL_ID="akey4"
REACT_APP_COGNITO_APP_ID="akey5"
total 2100
drwxr-xr-x    6 root root    4096 Feb 25 12:15 .
drwxr-xr-x    1 root root    4096 Feb 25 12:15 ..
-rw-r--r--    1 root root     382 Feb 25 12:15 .env.production <- it's here!
drwxr-xr-x    8 root root    4096 Feb 25 12:13 .git
-rw-r--r--    1 root root     230 Feb 25 12:13 .gitignore
-rw-r--r--    1 root root     371 Feb 25 12:13 Dockerfile
-rw-r--r--    1 root root    3787 Feb 25 12:13 README.md
-rw-r--r--    1 root root    1019 Feb 25 12:13 cloudbuild.yaml
drwxr-xr-x 1089 root root   36864 Feb 25 12:14 node_modules
-rw-r--r--    1 root root 1580131 Feb 25 12:13 package-lock.json
-rw-r--r--    1 root root    1896 Feb 25 12:13 package.json
drwxr-xr-x    2 root root    4096 Feb 25 12:13 public
drwxr-xr-x    9 root root    4096 Feb 25 12:13 src
-rw-r--r--    1 root root     535 Feb 25 12:13 tsconfig.json
-rw-r--r--    1 root root  478836 Feb 25 12:13 yarn.lock
/workspace
yarn run v1.22.17
$ sh -ac '. .env.production; react-scripts build'
sh: 1: .: .env.production: not found
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我不确定我是否在做完全错误的事情,或者它是否是一个错误GCP 方面?

I'm doing something I thought was simple:

# Fetch config
- name: 'gcr.io/cloud-builders/gsutil'
  volumes:
    - name: 'vol1'
    path: '/persistent_volume'
  args: [ 'cp', 'gs://servicesconfig/devs/react-app/env.server', '/persistent_volume/env.server' ]

# Install dependencies
- name: node:$_NODE_VERSION
  entrypoint: 'yarn'
  args: [ 'install' ]

  # Build project
  - name: node:$_NODE_VERSION
    volumes:
      - name: 'vol1'
        path: '/persistent_volume'
    entrypoint: 'bash'
    args:
      - -c
      - |
        cp /persistent_volume/env.server .env.production &&
        cat .env.production &&
        ls -la &&
        yarn run build:prod

while in my package.json:

"build:prod": "sh -ac '. .env.production; react-scripts build'",

All of this works well in local but the output in gcp cloud build:

    Already have image: node:14
REACT_APP_ENV="sandbox"
REACT_APP_CAPTCHA_ENABLED=true
REACT_APP_CAPTCHA_PUBLIC_KEY="akey"
REACT_APP_DEFAULT_APP="home-btn"
REACT_APP_API_URL="akey2"
REACT_APP_STRIPE_KEY="akey3"
REACT_APP_COGNITO_POOL_ID="akey4"
REACT_APP_COGNITO_APP_ID="akey5"
total 2100
drwxr-xr-x    6 root root    4096 Feb 25 12:15 .
drwxr-xr-x    1 root root    4096 Feb 25 12:15 ..
-rw-r--r--    1 root root     382 Feb 25 12:15 .env.production <- it's here!
drwxr-xr-x    8 root root    4096 Feb 25 12:13 .git
-rw-r--r--    1 root root     230 Feb 25 12:13 .gitignore
-rw-r--r--    1 root root     371 Feb 25 12:13 Dockerfile
-rw-r--r--    1 root root    3787 Feb 25 12:13 README.md
-rw-r--r--    1 root root    1019 Feb 25 12:13 cloudbuild.yaml
drwxr-xr-x 1089 root root   36864 Feb 25 12:14 node_modules
-rw-r--r--    1 root root 1580131 Feb 25 12:13 package-lock.json
-rw-r--r--    1 root root    1896 Feb 25 12:13 package.json
drwxr-xr-x    2 root root    4096 Feb 25 12:13 public
drwxr-xr-x    9 root root    4096 Feb 25 12:13 src
-rw-r--r--    1 root root     535 Feb 25 12:13 tsconfig.json
-rw-r--r--    1 root root  478836 Feb 25 12:13 yarn.lock
/workspace
yarn run v1.22.17
$ sh -ac '. .env.production; react-scripts build'
sh: 1: .: .env.production: not found
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I'm unsure if I'm doing something completely wrong or if it's a bug on GCP side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2025-01-16 20:26:51

好吧,我对 bash 和 zh 文档不够专业,无法理解问题所在,但我最终解决了它。

需要注意一件事:
所有内容实际上都是在 cloudbuild 中的原始步骤之间共享的,不需要卷或任何特定路径

因此在 cloudbuild 方面,我更改了 yaml 以反映:

- name: node:$_NODE_VERSION
entrypoint: 'bash'
args:
  - -c
  - |
    mv env.server .env.production &&
    yarn run build:prod

在 package.json 上,我现在使用额外的 lib env- cmd

将构建命令更改为:
"build:prod": "env-cmd -f .env.Production React-scripts build",

这就像一个魅力。

我有点恼火,我不得不为此添加另一个库,但是,好吧。

Alright, I'm not expert enough into bash and zh documentation to understand what the issue is, but I ended up solving it.

One thing to pay attention to:
everything is actually shared between raw steps in cloudbuild, no need for a volume or any specific path

So on the cloudbuild side I changed the yaml to reflect:

- name: node:$_NODE_VERSION
entrypoint: 'bash'
args:
  - -c
  - |
    mv env.server .env.production &&
    yarn run build:prod

And on the package.json I'm now using an extra lib env-cmd

which changes the build command to:
"build:prod": "env-cmd -f .env.production react-scripts build",

this works like a charm.

I'm a bit annoyed I had to add another lib for this but, well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文