首页 > 解决方案 > 谷歌云构建无法找到 git 路径

问题描述

我有一个正在运行的 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 博客文章中的建议进行)。我没有意识到这些图像还包括 nodejs 经常需要的其他程序,如 git 等。

标签: google-cloud-platformgoogle-cloud-build

解决方案


有两件事不能混合:

  • 混帐tag
  • 云构建tag

gittag是您放入存储库中的值,用于在某个时间点获取代码。该git ls-remote命令是正确的。但是标签是空的,并且ls-remotessh://git url 作为标签名称。

Cloud Buildtag(如果您的容器在 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,请使用替换变量。您可以使用必须以下划线开头的自己的替换变量,或者使用预定义的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 的定义完全一样。

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

更新

使用您的其他详细信息,您的基本映像中没有安装 git。在你之前添加这一行npm install

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


推荐阅读