google-cloud-platform - 谷歌云构建无法找到 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 等。
解决方案
有两件事不能混合:
- 混帐
tag
- 云构建
tag
gittag
是您放入存储库中的值,用于在某个时间点获取代码。该git ls-remote
命令是正确的。但是标签是空的,并且ls-remote
将ssh://
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
推荐阅读
- asp.net-core-mvc - 如何通过自定义图标更改传单地图的图标
- javascript - 我在使用 setInterval 时遇到问题
- javascript - Array.map() 如何与反应组件一起使用?
- selenium-webdriver - 未发现此类元素异常
- javascript - 我的 Promise 捕获到异常,但我不知道如何打印错误消息
- javascript - 我可以通过使用该对象中的另一个值来获取数组中对象内部的特定值吗?
- neo4j - 展开命令中的错误“Neo.ClientError.Statement.SyntaxError: Invalid input 'p': expected whitespace”
- url-redirection - 如何完全检测网页?
- c# - 无法使用 C# 更新 Oracle 数据库中表的列
- ios - 我不在 swift 中使用带有 AKSideMenu 的自定义导航栏