GoogleContainerTools / skaffold

Easy and Repeatable Kubernetes Development
https://skaffold.dev/
Apache License 2.0
14.94k stars 1.62k forks source link

file pattern [./dist] must match at least one file in Cloud Build #8562

Closed bogdan-pechounov closed 1 year ago

bogdan-pechounov commented 1 year ago

Expected behavior

Build images first without checking if folder exists, since the folder "dist" will be created in the first stage of a multi-stage docker build.

#6 [stage-1 3/4] RUN npm install --global serve
#6 sha256:072555c385ddcdd7aeaab46ebe63604930c6847ff3364fd2fbbc51a2363a9374
#6 CACHED

#8 [stage-1 4/4] COPY ./dist .
#8 sha256:4cd172fe75523ac226790dfd619e592bc7248bada1b6674ac5949b92a42b40da
#8 CACHED

Actual behavior

Output from Cloud Build:

From https://github.com/bogdan-pechounov/microservices-quiz-app
 * branch            8983e6456e00967896adbdb30b47f6af7be1bd08 -> FETCH_HEAD
HEAD is now at 8983e64 COPY ./dist .
BUILD
Pulling image: gcr.io/k8s-skaffold/skaffold:v2.2.0
v2.2.0: Pulling from k8s-skaffold/skaffold
Digest: sha256:a11f3a9d9b8c2c873277a83ac1b2e67659883a05834ca75e5eb66ab87aca19d9
Status: Downloaded newer image for gcr.io/k8s-skaffold/skaffold:v2.2.0
gcr.io/k8s-skaffold/skaffold:v2.2.0
Fetching cluster endpoint and auth data.
kubeconfig entry generated for autopilot-cluster.
Generating tags...
 - auth-img -> gcr.io/disco-ethos-380514/auth-img:8983e64
 - auth-img-dev -> gcr.io/disco-ethos-380514/auth-img-dev:8983e64
 - react-client-img -> gcr.io/disco-ethos-380514/react-client-img:8983e64
 - react-client-img-dev -> gcr.io/disco-ethos-380514/react-client-img-dev:8983e64
Checking cache...
 - auth-img: Not found. Building
 - auth-img-dev: Not found. Building
 - react-client-img: Error checking cache.
getting hash for artifact "react-client-img": getting dependencies for "react-client-img": file pattern [./dist] must match at least one file
ERROR
ERROR: build step 0 "gcr.io/k8s-skaffold/skaffold:v2.2.0" failed: step exited with non-zero status: 1

Information

I have a multi-stage build to serve static files built from a react app.

FROM node:18-alpine
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm i
COPY . .
RUN npm run build

FROM node:18-alpine
WORKDIR /app
RUN npm install --global serve
COPY ./dist .
CMD ["serve", "-l", "5173"]

It works fine locally but not when deploying to the cloud. I am not sure if the issue is with skaffold or Cloud Build.

apiVersion: skaffold/v4beta2
kind: Config
metadata:
  name: microservices-quiz-app
build:
  artifacts:
    # auth
    - image: auth-img
      context: auth
      docker:
        dockerfile: Dockerfile
    - image: auth-img-dev
      context: auth
      docker:
        dockerfile: Dockerfile.dev
    # react client
    - image: react-client-img
      context: react-client
      docker:
        dockerfile: Dockerfile
    - image: react-client-img-dev
      context: react-client
      docker:
        dockerfile: Dockerfile.dev
test:
  # auth
  - image: auth-img-dev
    context: auth
    custom:
      # run test in container
      - command: echo $IMAGE && docker run -e CI=true $IMAGE npm test
  # react-client
  - image: react-client-img-dev
    context: react-client
    custom:
      # run test in container
      - command: docker run -e CI=true $IMAGE npm test
manifests:
  rawYaml:
  - auth/k8s/*.yaml
  - react-client/k8s/*.yaml
  # - jwt-secret.yaml created manually
  - ingress-srv.yaml

Steps to reproduce the behavior

  1. https://github.com/bogdan-pechounov/microservices-quiz-app/tree/8983e6456e00967896adbdb30b47f6af7be1bd08
  2. kubectl create secret generic jwt-secret --from-literal=JWT_SECRET=secret
  3. skaffold run --skip-tests

(To reproduce it with Cloud Build, it would creating a repo and setting up a trigger with substitution variables _CLUSTER=autopilot-cluster and _ZONE=us-central1)

ericzzzzzzz commented 1 year ago

Hi @bogdan-pechounov , I think you may need to change your dockerfile a little bit, you need to copy the dist folder from your builder, not from your context folder, something like this.

FROM node:18-alpine as builder
WORKDIR /app
COPY ["package.json", "package-lock.json*", "./"]
RUN npm i
COPY . .
RUN npm run build

FROM node:18-alpine
WORKDIR /app
RUN npm install --global serve
COPY --from=builder app/dist .
CMD ["serve", "-l", "5173"]
bogdan-pechounov commented 1 year ago

Ah that makes more sense. Thank you. It worked locally because I had a dist folder to see what the files looked like.