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

标签 php azure azure-devops devops

我有一个 PHP 应用程序

我正忙于创建 Azure 容器注册表以及构建镜像并将其推送到 Azure 容器注册表的管道。因此,这是构建将镜像推送到 azure 容器注册表的部分:

# Docker
# Build and push an image to Azure Container Registry
# https://learn.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)


这是用于创建注册表容器的模板:

{
    "$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"
            }
        }
    ]
}

但现在我想将代码包含在 yaml 文件中以构建 Azure 容器注册表。

那么我必须在 yaml 文件中包含哪些内容才能构建 azure 容器注册表?

我现在是这样的:

# Docker
# Build and push an image to Azure Container Registry
# https://learn.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)'

    

我的服务连接的名称是:hackaton_internetsuite_connection

但是我收到了这个错误:

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.

最佳答案

从模板示例来看,这是一个 ARM 模板。

在 YAML 示例中,docker 任务用于将 Docker 镜像部署到现有的 Azure 容器注册表。它不会创建新的 Azure 容器注册表。

要部署ARM模板,您需要使用任务:Azure Resource Group Deployment task

例如:

- 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)

对于服务连接,您需要使用Azure Resource Manager类型的服务连接。

例如:

enter image description here

在您的情况下,服务 connect:hackaton_internetsuite_connection 是 Docker 注册表服务连接。这也是您在管道中看到错误的原因。 azureSubscriptionEndpoint字段需要输入Azure Resource Manager类型服务连接。

关于php - 如何使用yaml模板创建azure容器注册表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72953741/

相关文章:

php - 基于 session 的服务器端数据设置和检索

php - Yii 2 创建多个用户角色 (RBAC)

azure - Application Insights - 如何在 TelemetryInitializer 中使用 ISupportProperties

azure - asp.net core 2.0 的 DevOps 构建问题

c# - 如何代表用户调用 Azure DevOps API 方法?

azure - 有什么办法可以缓存git源文件吗?

php - 使用 cURL 在 api 中拒绝承载授权

html - Magento Foreach 子类别获取产品列表

azure - 在 Visual Studio 中调试 Azure Function 时创建的日志文件在哪里

.net - 从packages.config 迁移到PackageReferences 导致无法加载文件或程序集 "..."或其依赖项之一。访问被拒绝