Bagikan melalui


Membuat definisi alur

Jika templat Anda azd tidak menyertakan file definisi alur CI/CD, Anda dapat membuatnya untuk mengotomatiskan build dan penyebaran aplikasi Anda. Definisi alur yang terstruktur dengan baik biasanya mencakup empat bagian utama:

  • Pemicu: Menentukan peristiwa kapan pipeline seharusnya dijalankan, seperti push kode ke cabang tertentu, pull request, atau pemicu manual. Menentukan pemicu memastikan alur Anda berjalan secara otomatis sebagai respons terhadap aktivitas pengembangan, memungkinkan integrasi dan penyebaran berkelanjutan.

  • Izin: Menentukan akses yang diperlukan agar alur berinteraksi dengan sumber daya dengan aman. Misalnya, berikan izin untuk membaca konten repositori atau meminta token identitas. Izin yang benar sangat penting untuk penyebaran yang aman dan berhasil.

  • Sistem Operasi atau Kumpulan: Mengatur lingkungan untuk pekerjaan alur, seperti gambar komputer virtual tertentu (misalnya, ubuntu-latest) atau kumpulan agen. Memilih lingkungan yang tepat memastikan kompatibilitas dengan persyaratan build dan penyebaran aplikasi Anda.

  • Langkah-langkah: Mencantumkan tugas yang dilakukan alur, seperti memeriksa kode, menginstal dependensi, membangun, menyediakan infrastruktur, dan menyebarkan ke Azure. Setiap langkah harus didefinisikan dengan jelas untuk mengotomatiskan proses penyebaran end-to-end.

Contoh berikut menunjukkan cara membuat file definisi alur dan konfigurasi terkait untuk GitHub Actions dan Azure Pipelines.

Untuk menjalankan azd di GitHub Actions, konfigurasikan pengaturan berikut:

  • Memberikan id-token: write dan contents: read mengakses cakupan.
  • Instal tindakan azd, kecuali menggunakan gambar Docker dengan azd yang telah diinstal sebelumnya.

Gunakan templat ini sebagai titik awal untuk definisi alur Anda:

on:
  workflow_dispatch:
  push:
    # Run when commits are pushed to mainline branch (main or master)
    # Set this to the mainline branch you are using
    branches:
      - main
      - master

# Set this permission if you are using a Federated Credential.
permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    # azd build-in variables.
    # This variables are always set by `azd pipeline config`
    # You can set them as global env (apply to all steps) or you can add them to individual steps' environment
    env:
      AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
      AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
      AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
      AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
      ## Define the additional variables or secrets that are required globally (provision and deploy)
      # ADDITIONAL_VARIABLE_PLACEHOLDER: ${{ variables.ADDITIONAL_VARIABLE_PLACEHOLDER }}
      # ADDITIONAL_SECRET_PLACEHOLDER: ${{ secrets.ADDITIONAL_SECRET_PLACEHOLDER }}      
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      # using the install-azd action
      - name: Install azd
        uses: Azure/setup-azd@v1.0.0

      # # If you want to use azd-daily build, or install it from a PR, you can remove previous step and
      # # use the next one:
      # - name: Install azd - daily or from PR
      #  # Update this scrip based on the OS - pool of your pipeline. This example is for a linux pipeline installing daily build
      #  run: curl -fsSL https://aka.ms/install-azd.sh | bash -s -- --version daily
      #  shell: pwsh

      # azd set up Federated Credential by default. You can remove this step if you are using Client Credentials
      - name: Log in with Azure (Federated Credentials)
        if: ${{ env.AZURE_CLIENT_ID != '' }}
        run: |
          azd auth login `
            --client-id "$Env:AZURE_CLIENT_ID" `
            --federated-credential-provider "github" `
            --tenant-id "$Env:AZURE_TENANT_ID"
        shell: pwsh

      ## If you set up your pipeline with Client Credentials, remove previous step and uncomment this one
      # - name: Log in with Azure (Client Credentials)
      #   if: ${{ env.AZURE_CREDENTIALS != '' }}
      #   run: |
      #     $info = $Env:AZURE_CREDENTIALS | ConvertFrom-Json -AsHashtable;
      #     Write-Host "::add-mask::$($info.clientSecret)"

      #     azd auth login `
      #       --client-id "$($info.clientId)" `
      #       --client-secret "$($info.clientSecret)" `
      #       --tenant-id "$($info.tenantId)"
      #   shell: pwsh
      #   env:
      #     AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Provision Infrastructure
        run: azd provision --no-prompt
        env:
         #  # uncomment this if you are using infrastructure parameters
         #  AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ secrets.AZD_INITIAL_ENVIRONMENT_CONFIG }}
         ## Define the additional variables or secrets that are required only for provision 
         #  ADDITIONAL_VARIABLE_PLACEHOLDER: ${{ variables.ADDITIONAL_VARIABLE_PLACEHOLDER }}
         #  ADDITIONAL_SECRET_PLACEHOLDER: ${{ secrets.ADDITIONAL_SECRET_PLACEHOLDER }}

      - name: Deploy Application
        run: azd deploy --no-prompt
        env:
         ## Define the additional variables or secrets that are required only for deploy
         #  ADDITIONAL_VARIABLE_PLACEHOLDER: ${{ variables.ADDITIONAL_VARIABLE_PLACEHOLDER }}
         #  ADDITIONAL_SECRET_PLACEHOLDER: ${{ secrets.ADDITIONAL_SECRET_PLACEHOLDER }}