简体   繁体   English

在 Azure 管道中,如何在变量中使用 Docker 容器名称以用于替换令牌任务?

[英]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.

相关问题 如何在Azure Pipeline中设置一个JSON文件作为变量,并在后续任务中使用? - How to set a JSON file as a variable in Azure Pipeline and use it in subsequent task? Docker任务:如何通过build / release变量设置Azure容器注册表名称或ID? - Docker task : How to set the Azure container registry name or id from build/release variable? Azure Pipeline - 如何使 delayForMinutes 任务使用变量中的值而不是硬编码值 - Azure Pipeline - How to make delayForMinutes task use value from variable instead of hardcoded value Azure 任务步骤名称的 devops 管道变量 - Azure devops pipeline variable for a task step name 在 Azure Pipeline 中使用运行 Azure 容器实例 - Use Running Azure Container Instance in Azure Pipeline 如何在 Azure 文件复制管道任务中使用 SAS 令牌? - How to use SAS Token in Azure File Copy Pipeline Task? 如何在 Azure 管道中运行 docker 容器? - How to run a docker container in Azure pipeline? Azure docker 容器提供“未经授权的容器使用” - Azure docker container gives 'Container use unauthorized' Azure DevOps 发布管道 - 将环境变量传递给 docker 容器 - Azure DevOps Release Pipeline - Pass environment variable to docker container 如何为 Azure 管道容器作业构建 windows docker 容器? - How to build a windows docker container for Azure pipeline container job?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM