使用yq替换Docker镜像的名称

huangapple go评论65阅读模式
英文:

Using yq to replace name of docker image

问题

我正在使用GitHub Actions创建新的镜像并将其推送到镜像仓库。
这部分代码可以不翻译:

    - name: Build the Docker image
      run: docker build . --file Dockerfile  --tag ${{secrets.DOCKER_USER}}/book:$GITHUB_SHA
这个方法运行得很好。现在,我需要替换镜像的值。所以我考虑使用yq:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - image: xxxxx/book:main
    - name: 替换镜像
      run: yq -i e '.spec.template.spec.containers.image |= ${{secrets.DOCKER_USER}}/guestbook:$GITHUB_SHA' argo/deployment.yaml
但是出现了这个错误:“错误:1:43:无效的输入文本“xxxx/book:...”
错误:进程以退出码1完成。”
我应该如何进行这个替换操作?
英文:

I'm using github actions for creating new images and pushing them to a registry.

    - name: Build the Docker image
      run: docker build . --file Dockerfile  --tag ${{secrets.DOCKER_USER}}/book:$GITHUB_SHA

This works perfectly. Now, I need to replace the value of the image. So I thought of using yq

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - image: xxxxx/book:main
    - name: Replace Image
      run: yq -i e '.spec.template.spec.containers.image |= ${{secrets.DOCKER_USER}}/guestbook:$GITHUB_SHA' argo/deployment.yaml

But getting this error Error: 1:43: invalid input text "xxxx/book:..."
Error: Process completed with exit code 1.

How I would need to do this replacement?

答案1

得分: 2

使用正确的 JSON 路径表达式来定位 image 属性,并引用替换值。

示例:

$ yq '.spec.template.spec.containers.[0].image = "STRING"' argo/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - image: STRING
$ yq --version
yq (https://github.com/mikefarah/yq/) 版本 v4.34.1
英文:

Use the correct json path expression to the image property and quote the replacement value.

Example:

$ yq '.spec.template.spec.containers.[0].image = "STRING"' argo/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - image: STRING
$ yq --version
yq (https://github.com/mikefarah/yq/) version v4.34.1

huangapple
  • 本文由 发表于 2023年5月29日 11:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定