Latihan - menerbitkan modul ke registri Anda

Selesai

Anda telah membuat registri privat untuk digunakan oleh perusahaan mainan Anda. Dalam latihan ini, Anda akan:

  • Buat modul untuk sumber daya situs web.
  • Buat modul lain untuk sumber daya di CDN.
  • Publikasikan modul ke registri Anda.
  • Daftar modul dalam registri.

Latihan ini menggunakan ekstensi Bicep untuk Visual Studio Code. Pastikan untuk menginstal ekstensi ini di Visual Studio Code.

Buat modul untuk situs web

Anda sebelumnya membuat modul yang menyebarkan situs web. Di sini, Anda menyimpan file modul sehingga Anda dapat memublikasikannya.

  1. Buka Visual Studio Code.

  2. Buat file baru bernama website.bicep.

  3. Tempelkan kode berikut ke dalam file website.bicep:

    @description('The Azure region into which the resources should be deployed.')
    param location string
    
    @description('The name of the App Service app.')
    param appServiceAppName string
    
    @description('The name of the App Service plan.')
    param appServicePlanName string
    
    @description('The name of the App Service plan SKU.')
    param appServicePlanSkuName string
    
    resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
      name: appServicePlanName
      location: location
      sku: {
        name: appServicePlanSkuName
      }
    }
    
    resource appServiceApp 'Microsoft.Web/sites@2022-03-01' = {
      name: appServiceAppName
      location: location
      properties: {
        serverFarmId: appServicePlan.id
        httpsOnly: true
      }
    }
    
    @description('The default host name of the App Service app.')
    output appServiceAppHostName string = appServiceApp.properties.defaultHostName
    
  4. Simpan file.

    Anda dapat memilih File>Simpan Sebagai atau memilih Ctrl+S di Windows (⌘+S di macOS). Ingat tempat Anda menyimpan file. Misalnya, Anda mungkin ingin membuat folder templat untuk menyimpan filenya.

Buat modul untuk CDN

Mirip dengan langkah sebelumnya, Anda menyimpan file modul yang telah dibuat sebelumnya sehingga Anda dapat segera memublikasikannya.

  1. Buat file baru bernama cdn.bicep.

  2. Tempelkan kode berikut ke dalam file cdn.bicep:

    @description('The host name (address) of the origin server.')
    param originHostName string
    
    @description('The name of the CDN profile.')
    param profileName string = 'cdn-${uniqueString(resourceGroup().id)}'
    
    @description('The name of the CDN endpoint')
    param endpointName string = 'endpoint-${uniqueString(resourceGroup().id)}'
    
    @description('Indicates whether the CDN endpoint requires HTTPS connections.')
    param httpsOnly bool
    
    var originName = 'my-origin'
    
    resource cdnProfile 'Microsoft.Cdn/profiles@2022-11-01-preview' = {
      name: profileName
      location: 'global'
      sku: {
        name: 'Standard_Microsoft'
      }
    }
    
    resource endpoint 'Microsoft.Cdn/profiles/endpoints@2022-11-01-preview' = {
      parent: cdnProfile
      name: endpointName
      location: 'global'
      properties: {
        originHostHeader: originHostName
        isHttpAllowed: !httpsOnly
        isHttpsAllowed: true
        queryStringCachingBehavior: 'IgnoreQueryString'
        contentTypesToCompress: [
          'text/plain'
          'text/html'
          'text/css'
          'application/x-javascript'
          'text/javascript'
        ]
        isCompressionEnabled: true
        origins: [
          {
            name: originName
            properties: {
              hostName: originHostName
            }
          }
        ]
      }
    }
    
    @description('The host name of the CDN endpoint.')
    output endpointHostName string = endpoint.properties.hostName
    
  3. Simpan file.

Publikasikan modul ke registri Anda

  1. Di terminal Visual Studio Code, jalankan perintah berikut. Ganti YOUR_CONTAINER_REGISTRY_NAME dengan nama registri pribadi Anda.

    az bicep publish \
      --file website.bicep \
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/website:v1'
    
    az bicep publish \
      --file cdn.bicep \
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/cdn:v1'
    

    Perhatikan bahwa Anda tidak perlu masuk. Bicep menggunakan informasi masuk dari Azure CLI untuk mengautentikasi Anda ke registri.

  2. Jalankan perintah berikut untuk membuat daftar artefak di registri Anda:

    az acr repository list \
      --name YOUR_CONTAINER_REGISTRY_NAME
    

    Output-nya menunjukkan nama modul Anda:

    [
      "cdn",
      "website"
    ]
    
  1. Di terminal Visual Studio Code, jalankan perintah berikut. Ganti YOUR_CONTAINER_REGISTRY_NAME dengan nama registri pribadi Anda.

    bicep publish website.bicep `
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/website:v1'
    
    bicep publish cdn.bicep `
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/cdn:v1'
    

    Perhatikan bahwa Anda tidak perlu masuk. Bicep menggunakan informasi masuk dari Azure PowerShell untuk mengautentikasi Anda ke registri.

  2. Jalankan perintah berikut untuk membuat daftar artefak di registri Anda:

    Get-AzContainerRegistryRepository -RegistryName YOUR_CONTAINER_REGISTRY_NAME
    

    Output-nya menunjukkan nama modul Anda:

    cdn
    website
    

Anda juga dapat menggunakan portal Azure untuk membuat daftar modul di registri Anda. Dari tab Gambaran Umum grup sumber daya Anda, pilih YOUR_CONTAINER_REGISTRY_NAME lalu pilih Repositori. Anda terhubung ke portal Azure nanti dalam modul ini.