如何在 Jenkins 上对 Docker 图像进行版本控制并将其传递给 Kubernetes YAML 文件

How to version Docker images on Jenkins and pass it to the Kubernetes YAML files

基本上,我正在尝试在我的本地 Jenkins 上创建一个管道,创建一个图像,然后发送到 Docker Hub。然后我将把这个镜像部署到我们本地的 Kubernetes(Server IP:10.10.10.4).

下面是 Jenkins 管道脚本;

docker build -t test123/demo:$BUILD_NUMBER . 
docker login --username=testuser --password=testpass
docker push test123/demo:$BUILD_NUMBER
ssh -tt root@10.10.10.4 'kubectl apply -f hybris-deployment.yaml'

所以问题是;我可以使用 $BUILD_NUMBER 成功标记图像并推送到 Docker 中心。然后我必须在 Kubernetes 服务器的 YAML 文件中使用这个 $BUILD_NUMBER 并部署它。

但是我无法将这个 $BUILD_NUMBER 传递给 Kubernetes 服务器。我应该以某种方式使用 ssh 命令发送此内部版本号,并将其用作 YAML 文件中的标记。

知道我该怎么做吗? 谢谢!

您创建的管道类似于

如果你deployment.yaml文件像

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: test-image
  labels:
    app: test-image
spec:
  selector:
    matchLabels:
      app: test-image
      tier: frontend
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: test-image
        tier: frontend
    spec:
      containers:
      - image: TEST_IMAGE_NAME
        name: test-image
        ports:
        - containerPort: 8080
          name: http
        - containerPort: 443
          name: https

在管道中,您可以更改图像并在 deployment.yaml

中替换此部分
sed -i "s,TEST_IMAGE_NAME,gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA," deployment.yaml

因此此命令将替换 deployment.yaml 中的行,这样图像 URL 已更新,您可以应用 YAML 文件。

您可以在此处查看示例 jenkin 文件:https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes/blob/master/sample-app/Jenkinsfile

整个项目 link : https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes