Google 云构建无法找到 git 路径

Google Cloud build unable to find git path

我有一个 docker 文件,它是 运行 npm install。当我将此提交给 gcloud builds submit --tag <tag> 时,出现以下错误:

....
npm ERR! path git
npm ERR! code ENOENT
npm ERR! errno ENOENT
npm ERR! syscall spawn git
npm ERR! enoent Error while executing:
npm ERR! enoent undefined ls-remote -h -t ssh://git@github.com/web3-js/WebSocket-Node.git
npm ERR! enoent
npm ERR! enoent
npm ERR! enoent spawn git ENOENT
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

从上面的错误消息和谷歌搜索 "undefined ls-remote -h -t ssh://git@github.com" 的结果来看,问题似乎是 git 路径未定义。

有解决办法吗?


编辑:

# reference: https://www.docker.com/blog/keep-nodejs-rockin-in-docker/

# Operating System

FROM node:10.16.3-slim

# create app directory
WORKDIR /usr/src/app

COPY package-lock.json ./
COPY package.json ./

RUN npm install --no-optional
# for production: RUN npm ci

COPY . .

#EXPOSE 8080
# Environment variables
ENV mode help


CMD ["sh", "-c", "node src/app.js ${mode}"]

我现在认为这是因为我使用了 -slim 版本的 nodejs docker 图像(按照 docker 博客 post 中的推荐)。我没有意识到这些图像还包括 nodejs 经常需要的其他程序,如 git 等。

有两件事不能混用:

  • git tag
  • 云构建tag

git tag 是您放入存储库以在特定时间点获取代码的值。 git ls-remote 命令是正确的命令。但是标签是空的,ls-remotessh:// git url 作为标签名称。

Cloud Build tag 如果你的容器在 gcr docker hub 中的名称。通常是 gcr.io/<project_id>/<name that you want>

为了解决您的问题,您有 2 个解决方案:

  • 使用docker build命令。使用 tag 命名您的容器,并使用环境变量 -e GIT_TAG=xxx 在您的 Dockerfile 中使用以指定 git 标签
  • 默认使用 Cloud Build 配置文件 cloudbuild.yaml 并在 Dockerfile 中使用与环境变量相同的逻辑。要将 GIT_TAG 传递给 Cloud Build,请使用 substitution variables。您可以使用自己的必须以下划线开头的替换变量,或使用预定义的 TAG_NAME 变量。在这两种情况下,您都必须在 运行 您的 Cloud Build 命令
  • 时指定它

命令:gcloud builds submit --substitutions=TAG_NAME="test"gcloud builds submit --substitutions=_MY_TAG_NAME="test"

cloudbuild.yaml 文件

- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/my-image', '.', '-e', 'GIT_TAG=$_MY_TAG_NAME']

# my-image is pushed to Container Registry
images:
- 'gcr.io/$PROJECT_ID/my-image'

可以看到docker build命令行和Cloud Build定义完全一致

顺便说一句,您可以使用 docker 在本地(或在云 Shell 上)构建来测试您的构建,以便更快地进行测试和迭代,然后将其打包到 cloudbuild.yaml 文件中。

更新

根据您的其他详细信息,您没有在基本映像中安装 git。 在 npm install

之前添加此行

RUN apt-get update && apt-get install -y git