【问题标题】:Gitlab CI/CD pipeline passed, but no changes were applied to the serverGitlab CI/CD 管道通过,但未对服务器应用任何更改
【发布时间】:2023-01-13 02:49:56
【问题描述】:

我正在通过将 Gitlab CI/CD 应用于 GKE 集群来测试自动化。应用已成功部署,但未应用源代码更改(例如重命名 html 标题)。

我已经确认代码已经在 gitlab 存储库 master 分支中更改。没有其他分支。

CI/CD 简单地经历了以下过程。

  1. 推送代码到master分支
  2. 构建 NextJS 代码
  3. 构建 docker 镜像并将其推送到 GCR
  4. 拉取 docker 镜像并将其部署到其中。

    清单文件的内容如下。

    .gitlab-ci.yml

    stages:
      - build-push
      - deploy
    
    image: docker:19.03.12
    variables:
      GCP_PROJECT_ID: PROJECT_ID..
      GKE_CLUSTER_NAME: cicd-micro-cluster
      GKE_CLUSTER_ZONE: asia-northeast1-b
      DOCKER_HOST: tcp://docker:2375/
      DOCKER_TLS_CERTDIR: ""
      REGISTRY_HOSTNAME: gcr.io/${GCP_PROJECT_ID}
      DOCKER_IMAGE_NAME: ${CI_PROJECT_NAME}
      DOCKER_IMAGE_TAG: latest
    services:
      - docker:19.03.12-dind
    
    build-push:
     stage: build-push
     before_script:
       - docker info
       - echo "$GKE_ACCESS_KEY" > key.json
       - docker login -u _json_key --password-stdin https://gcr.io < key.json
     script:
       - docker build --tag $REGISTRY_HOSTNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG .
       - docker push $REGISTRY_HOSTNAME/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
    
    deploy:
      stage: deploy
      image: google/cloud-sdk
      script:
        - export USE_GKE_GCLOUD_AUTH_PLUGIN=True
        - echo "$GKE_ACCESS_KEY" > key.json
        - gcloud auth activate-service-account --key-file=key.json
        - gcloud config set project $GCP_PROJECT_ID
        - gcloud config set container/cluster $GKE_CLUSTER_NAME
        - gcloud config set compute/zone $GKE_CLUSTER_ZONE
        - gcloud container clusters get-credentials $GKE_CLUSTER_NAME --zone $GKE_CLUSTER_ZONE --project $GCP_PROJECT_ID
        - kubectl apply -f deployment.yaml
        - gcloud container images list-tags gcr.io/$GCP_PROJECT_ID/${CI_PROJECT_NAME} --filter='-tags:*' --format="get(digest)" --limit=10 > tags && while read p; do gcloud container images delete "gcr.io/$GCP_PROJECT_ID/${CI_PROJECT_NAME}@$p" --quiet; done < tags
    

    文件

    # Install dependencies only when needed
    FROM node:16-alpine AS deps
    # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
    RUN apk add --no-cache libc6-compat
    WORKDIR /app
    
    # Install dependencies based on the preferred package manager
    COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
    RUN \
      if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
      elif [ -f package-lock.json ]; then npm ci; \
      elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
      else echo "Lockfile not found." && exit 1; \
      fi
    
    
    # Rebuild the source code only when needed
    FROM node:16-alpine AS builder
    WORKDIR /app
    COPY --from=deps /app/node_modules ./node_modules
    COPY . .
    
    # Next.js collects completely anonymous telemetry data about general usage.
    # Learn more here: https://nextjs.org/telemetry
    # Uncomment the following line in case you want to disable telemetry during the build.
    # ENV NEXT_TELEMETRY_DISABLED 1
    
    RUN yarn build
    
    # If using npm comment out above and use below instead
    # RUN npm run build
    
    # Production image, copy all the files and run next
    FROM node:16-alpine AS runner
    WORKDIR /app
    
    ENV NODE_ENV production
    # Uncomment the following line in case you want to disable telemetry during runtime.
    # ENV NEXT_TELEMETRY_DISABLED 1
    
    RUN addgroup --system --gid 1001 nodejs
    RUN adduser --system --uid 1001 nextjs
    
    COPY --from=builder /app/public ./public
    
    # Automatically leverage output traces to reduce image size
    # https://nextjs.org/docs/advanced-features/output-file-tracing
    COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
    COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
    
    USER nextjs
    
    EXPOSE 3000
    
    ENV PORT 3000
    
    CMD ["node", "server.js"]
    

    部署.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: frontweb-lesson-prod
      labels:
        app: frontweb-lesson
    spec:
      selector:
        matchLabels:
          app: frontweb-lesson
      template:
        metadata:
          labels:
            app: frontweb-lesson
        spec:
          containers:
          - name: frontweb-lesson-prod-app
            image: gcr.io/PROJECT_ID../REPOSITORY_NAME..:latest
            ports:
            - containerPort: 3000
            resources:
              requests:
                cpu: 200m
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: frontweb-lesson-prod-svc
      labels:
        app: frontweb-lesson
    spec:
      selector:
        app: frontweb-lesson
      ports:
      - name: http
        port: 80
        protocol: TCP
        targetPort: 3000
      type: LoadBalancer
      loadBalancerIP: "EXTERNAL_IP.."
    

    有什么我想念的吗?

【问题讨论】:

    标签: gitlab gitlab-ci google-kubernetes-engine cicd


    【解决方案1】:

    默认情况下,镜像拉取策略将总是但如果没有变化,可能会有机会部署应用文件时可能不会更新部署.

    因为 kubectl apply 和 kubectl patch 命令不同

    你可以做的是添加 minor标签改变或注解在某一方面的变化部署并且检查图像也将使用kubectl apply命令进行更新,否则它将主要是不变kubectl apply的回复

    参考:imagepullpolicy

    在部署容器时,你应该避免使用 :latest 标签 生产,因为很难跟踪图像的哪个版本 运行并且更难正确回滚。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2021-11-30
      • 1970-01-01
      • 2022-01-07
      • 2022-08-11
      • 2022-10-23
      • 2018-08-03
      • 1970-01-01
      • 2020-01-10
      相关资源
      最近更新 更多