Condividi tramite


Creare una definizione di pipeline

Se il azd modello non include un file di definizione della pipeline CI/CD, è possibile crearne uno per automatizzare la compilazione e la distribuzione dell'applicazione. Una definizione di pipeline ben strutturata include in genere quattro sezioni principali:

  • Trigger: specifica gli eventi quando deve essere eseguita la pipeline, ad esempio i push di codice in rami specifici, richieste pull o trigger manuali. La definizione dei trigger garantisce che le esecuzioni della pipeline vengano eseguite automaticamente in risposta alle attività di sviluppo, abilitando l'integrazione e la distribuzione continue.

  • Autorizzazioni: specifica l'accesso necessario per l'interazione sicura della pipeline con le risorse. Ad esempio, concedere le autorizzazioni per leggere il contenuto del repository o richiedere token di identità. Le autorizzazioni corrette sono essenziali per le distribuzioni sicure e riuscite.

  • Sistema operativo o pool: imposta l'ambiente per i processi della pipeline, ad esempio un'immagine di macchina virtuale specifica (ad esempio, ubuntu-latest) o un pool di agenti. La scelta dell'ambiente corretto garantisce la compatibilità con i requisiti di compilazione e distribuzione dell'applicazione.

  • Passaggi: elenca le attività eseguite dalla pipeline, ad esempio il controllo del codice, l'installazione di dipendenze, la compilazione, l'infrastruttura di provisioning e la distribuzione in Azure. Ogni passaggio deve essere chiaramente definito per automatizzare il processo di distribuzione end-to-end.

Gli esempi seguenti illustrano come creare un file di definizione della pipeline e le configurazioni correlate per GitHub Actions e Azure Pipelines.

Per eseguire azd in GitHub Actions, configurare le impostazioni seguenti:

  • Concedere id-token: write e contents: read accedere agli ambiti.
  • Installare l'azione azd, a meno che non si usi un'immagine Docker con azd preinstallata.

Usare questo modello come punto di partenza per la definizione della pipeline:

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