[英]In Azure Pipeline how to use the Docker container name in a variable to use in a replace tokens task?
I have created an Azure DevOps pipeline and I can not figure out how to get and use the docker container name that is created in the docker task to use later in the pipeline when I run a shell script task to deploy the container. I have created an Azure DevOps pipeline and I can not figure out how to get and use the docker container name that is created in the docker task to use later in the pipeline when I run a shell script task to deploy the container.
1 - I build a Docker image. 1 - 我构建了一个 Docker 图像。 I see that is has the image name "containerapp$(Build.BuildId)" populated:我看到它填充了图像名称“containerapp$(Build.BuildId)”:
2 - I have a "Push image to Amazon ECR" task which also has the "containerapp$(Build.BuildId)" populated: 2 - 我有一个“将图像推送到 Amazon ECR”任务,其中也填充了“containerapp$(Build.BuildId)”:
3 - I try populating a variable in the variable group I am using to use the "containerapp$(Build.Build.id)" but that does not work later on when I try and use the variable: 3 - 我尝试在我用来使用“containerapp$(Build.Build.id)”的变量组中填充一个变量,但是稍后当我尝试使用该变量时它不起作用:
I have my Kubernetes yaml file populated with the IMAGE_ID variable which is utilized in a replace tokens task -我的 Kubernetes yaml 文件填充了 IMAGE_ID 变量,该变量用于替换令牌任务 -
containers:
- image: #{IMAGE_ID}#
name: app
volumeMounts:
- name: app-files mountPath: /var/www/html
I know that the replace tokens works because if I change the IMAGE_ID variable to be a valid container image location my deploy will work.我知道替换令牌有效,因为如果我将 IMAGE_ID 变量更改为有效的容器映像位置,我的部署将起作用。
How do I populate the variable to use whatever is autogenerated to be the container name that is created by Azure DevOps?如何填充变量以使用自动生成的任何内容作为由 Azure DevOps 创建的容器名称?
Is there another way?还有其他方法吗?
Test to use the Replace Token
task to replace the value in K8s yaml file, the variable containerapp$(Build.BuildId)
could work as expected.测试使用Replace Token
任务替换 K8s yaml 文件中的值,变量containerapp$(Build.BuildId)
可以正常工作。
Here is Replace Token Task Settings:这是替换令牌任务设置:
Variable Group:变量组:
Result:结果:
Based on my test, docker task can't directly output image name variable, so we could create a variable to save the name.根据我的测试,docker 任务不能直接 output 图像名称变量,所以我们可以创建一个变量来保存名称。 Just like you did.就像你做的那样。
To manage variables more convenient, you can improve your method.为了更方便地管理变量,您可以改进您的方法。
You could direclty set the $(IMAGE_ID) variable as the Image name in Docker task and Push task.您可以直接将 $(IMAGE_ID) 变量设置为 Docker 任务和推送任务中的图像名称。
In this case, you only need to change the variables in the variable group once, and you can change the variables in the image name and yaml files at the same time.在这种情况下,您只需更改一次变量组中的变量,即可同时更改镜像名称和 yaml 文件中的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.