【问题标题】:setup skaffold to deploy changes to google cloud build设置 skaffold 以将更改部署到谷歌云构建
【发布时间】:2025-12-10 23:30:01
【问题描述】:

我是 skaffold 的新手,并已按如下方式设置了 yaml 文件,该文件应将我项目中 src 文件夹中的任何更改同步到 goolge cloud build。请注意我没有使用 git。

当我运行 skaffold dev 时,本地计算机 (macos) 上 src 文件夹中的任何更改都会同步到 google cloud build,即更新和部署映像。

如果我在 skaffold dev 运行时在 src 文件夹中进行了任何更改,我注意到 skaffold 会识别更改但未部署到谷歌云构建。

我如何更新下面的 yaml 文件,每次我更改 src 文件夹中的任何文件时,该文件也会将更改部署到谷歌云构建。目前我必须停止 skaffold 并重新运行它,以便将更改部署到 google cloud build 中。

package.json

{
  "name": "auth",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node-dev src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/express": "^4.17.12",
    "express": "^4.17.1",
    "express-validator": "^6.12.0",
    "ts-node-dev": "^1.1.6",
    "typescript": "^4.3.4"
  }
}

skaffold.yaml

apiVersion: skaffold/v2alpha3
kind: Config
deploy:
  kubectl:
    manifests:
      - ./infra/k8s/*
build:
  # local:
  #   push: false
  googleCloudBuild:
    projectId:  ticketing-dev
  artifacts:
    - image: gcr.io/ticketing-dev/auth
      context: auth
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: 'src/**/*.ts'
            dest: .

Docker 文件

FROM node:alpine

#create working dir within image called app
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .

CMD ["npm", "start"]

下面是auth-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
    spec: 
      containers:
        - name: auth
          image: gcr.io/ticketing-dev/auth
---
apiVersion: v1
kind: Service
metadata:
  name: auth-srv
spec:
  selector:
    app: auth
  ports:
    - name: auth
      protocol: TCP
      port: 3000
      targetPort: 3000

【问题讨论】:

  • 这很有趣,因为skaffold dev 应该能够部署这些更改,您能否提供此过程的任何日志?
  • 以下是我进行更改并保存文件时的 skaffold 日志: Syncing 1 files for gcr.io/ticketing-dev/auth:latest@sha256:1db861d64340e900e5b1721086c69613f1025261f4119f28b1539094068701a0

标签: kubernetes google-kubernetes-engine google-cloud-build skaffold


【解决方案1】:

Skaffold 将更改同步到正在运行的容器,而不是 GCB。其中一项要求是容器必须包含tar

您还没有包含您的Dockerfile,但一个关键点是同步文件可能还不够:您的应用需要检测并重新加载更改。大多数打字稿应用程序使用tsc-watch 重新编译和重新启动。

例如,我使用 Skaffold TypeScript example 看到类似以下内容:

[node] 2:22:47 PM - Found 0 errors. Watching for file changes.
[node] Example app listening on port 3000.
Syncing 1 files for node-typescript-example:21b757dbdb147a87b0ce4b1e538ab93cdbc114e99515fdf4e398ed62a2b3809d
Watching for changes...
[node] 2:22:52 PM - File change detected. Starting incremental compilation...
[node] 
[node] 
[node] 2:22:52 PM - Found 0 errors. Watching for file changes.
[node] Example app listening on port 3000!

[node] 行来自容器内。

【讨论】:

  • 我已经在原帖中包含了 dockerfile 和 yaml 文件