如何在Jenkins上版本Docker镜像并将其传递给Kubernetes YAML文件



基本上,我试图在本地Jenkins上创建一个管道,创建一个映像,然后发送到Docker Hub。然后我将在本地Kubernetes上部署此映像(服务器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文件中将其用作标记。

知道我该怎么做吗?谢谢

您可以创建类似的管道

如果deployml.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

在管道中,您可以在部署中更改映像并替换此部分。yaml

sed -i "s,TEST_IMAGE_NAME,gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA," deployment.yaml

因此,该命令将替换deployml.yaml中的行,并通过这种方式更新图像URL,您可以应用yaml文件。

您可以在这里看到jenkin文件的示例:https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes/blob/master/sample-app/Jenkinsfile

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

最新更新