简体   繁体   English

如何使用 yaml 模板创建 azure 容器注册表?

[英]How to create azure container registry with yaml template?

I have a php application我有一个 php 应用程序

And I am bussy with a pipeline for creating a azure container registry and building and pushing a image to the azure container registry.我正忙于创建 azure 容器注册表以及构建映像并将其推送到 azure 容器注册表的管道。 So this is the part of building an pushing a image to the azure container registry:因此,这是构建将映像推送到 azure 容器注册表的一部分:

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'hackaton_internetsuite_connection'
  imageRepository: 'internetsuite-webscraper'
  containerRegistry: 'internetsuite.azurecr.io'
  dockerfilePath: '**/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)


and this is the template for creating the registry container:这是创建注册表容器的模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "registryName": {
            "type": "String"
        },
        "registryLocation": {
            "type": "String"
        },
        "registrySku": {
            "defaultValue": "Standard",
            "type": "String"
        }
    },
    "resources": [
        {
            "type": "Microsoft.ContainerRegistry/registries",
            "sku": {
                "name": "[parameters('registrySku')]"
            },
            "name": "[parameters('registryName')]",
            "apiVersion": "2017-10-01",
            "location": "[parameters('registryLocation')]",
            "properties": {
                "adminUserEnabled": "true"
            }
        }
    ]
}

But now I want to include the code in the yaml file for building the azure container registry.但现在我想在 yaml 文件中包含用于构建 azure 容器注册表的代码。

So what I have to include in the yaml file so that a azure container registry will be build?那么我必须在 yaml 文件中包含什么才能构建一个 azure 容器注册表?

I have it now like this:我现在有这样的:

# Docker
# Build and push an image to Azure Container Registry
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
- master

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'hackaton_internetsuite_connection'
  imageRepository: 'internetsuite-webscraper'
  acrHostName: 'internetsuite.azurecr.io'
  dockerfilePath: '**/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

     # Build container image
    - task: Docker@1
      displayName: 'Build container image'
      inputs:
        azureSubscriptionEndpoint: 'hackaton_internetsuite_connection'
        azureContainerRegistry: '$(acrHostName)'
        imageName: '$(imageName):$(Build.BuildId)'
        useDefaultContext: false
        buildContext: '$(System.DefaultWorkingDirectory)/PublishedWebApp'

    # Push container image
    - task: Docker@1
      displayName: 'Push container image'
      inputs:
        azureSubscriptionEndpoint: 'hackaton_internetsuite_connection'
        azureContainerRegistry: '$(acrHostName)'
        command: 'Push an image'
        imageName: '$(imageName):$(Build.BuildId)'

    

and the name of my service connection is: hackaton_internetsuite_connection我的服务连接的名称是:hackaton_internetsuite_connection

But then I get this error:但后来我得到这个错误:

The pipeline is not valid. Job Build: Step Docker2 input azureSubscriptionEndpoint expects a service connection of type AzureRM but the provided service connection hackaton_internetsuite_connection is of type dockerregistry. Job Build: Step Docker3 input azureSubscriptionEndpoint expects a service connection of type AzureRM but the provided service connection hackaton_internetsuite_connection is of type dockerregistry.

From the template sample, this is an ARM template.在模板示例中,这是一个 ARM 模板。

In your YAML sample, the docker task is used to deploy the Docker image to existing Azure Container Registry.在您的 YAML 示例中,docker 任务用于将 Docker 映像部署到现有的 Azure 容器注册表。 It will not create a new Azure Container Registry.它不会创建新的 Azure 容器注册表。

To deploy the ARM template, you need to use the task: Azure Resource Group Deployment task要部署 ARM 模板,需要使用任务: Azure Resource Group Deployment task

For example:例如:

- task: AzureResourceManagerTemplateDeployment@3
  displayName: 'ARM Template deployment: Resource Group scope'
  inputs:
    azureResourceManagerConnection: serviceconnection
    subscriptionId: xx
    resourceGroupName: xx
    location: xx
    csmFile: templatefile(template.json)
    csmParametersFile: paramtersfile(paramters.json)

For the service connection, you need to use the Azure Resource Manager type service connection.对于服务连接,需要使用Azure 资源管理器类型的服务连接。

For example:例如:

在此处输入图像描述

In your case, the service connect:hackaton_internetsuite_connection is a Docker Registry service connection.在您的情况下,服务连接:hackaton_internetsuite_connection 是一个 Docker Registry 服务连接。 This is also why you see the error in the pipeline.这也是您在管道中看到错误的原因。 azureSubscriptionEndpoint field needs to input Azure Resource Manager type service connection. azureSubscriptionEndpoint 字段需要输入 Azure Resource Manager 类型的服务连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM