Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Questo articolo fa parte di una serie di esercitazioni su come inserire e distribuire un'app Web Python in App Azure Container. Container Apps consente di distribuire app in contenitori senza la necessità di gestire un'infrastruttura complessa.
In this tutorial, you:
- Configure continuous deployment for a container app by using a GitHub Actions workflow.
- Apportare una modifica a una copia del repository di esempio per attivare il flusso di lavoro di GitHub Actions.
- Troubleshoot problems that you might encounter with configuring continuous deployment.
- Rimuovere le risorse non necessarie al termine della serie di esercitazioni.
La distribuzione continua è correlata alla pratica DevOps dell'integrazione continua e della consegna continua (CI/CD), che automatizza il flusso di lavoro di sviluppo delle app.
Il diagramma seguente illustra le attività in questa esercitazione.
Prerequisiti
Per configurare la distribuzione continua, è necessario:
The resources (and their configuration) that you created in the previous tutorial, which includes an instance of Azure Container Registry and a container app in Azure Container Apps.
Un account GitHub in cui hai fatto un fork del codice di esempio (Django o Flask) al quale puoi connetterti da Azure Container Apps. (If you downloaded the sample code instead of forking, be sure to push your local repo to your GitHub account.)
Optionally, Git installed in your development environment to make code changes and push to your repo in GitHub. In alternativa, è possibile apportare le modifiche direttamente in GitHub.
Configure continuous deployment for a container
Nell'esercitazione precedente, hai creato e configurato un'applicazione contenitore in Azure Container Apps. Parte della configurazione consisteva nel recuperare un'immagine Docker da un'istanza di Azure Container Registry. L'immagine del contenitore viene estratta dal Registro di sistema quando si crea un contenitore revisione, ad esempio quando si configura per la prima volta l'app contenitore.
In questa sezione viene configurata la distribuzione continua usando un flusso di lavoro di GitHub Actions. Con la distribuzione continua, viene creata una nuova immagine Docker e una nuova revisione del contenitore in base a un trigger. The trigger in this tutorial is any change to the main branch of your repository, such as with a pull request. Quando il flusso di lavoro viene attivato, crea una nuova immagine Docker, la inserisce nell'istanza di Registro Azure Container e aggiorna l'app contenitore a una nuova revisione usando la nuova immagine.
You can run Azure CLI commands in Azure Cloud Shell or on a workstation where Azure CLI is installed.
Se si eseguono comandi in una shell Git Bash in un computer Windows, immettere il comando seguente prima di procedere:
export MSYS_NO_PATHCONV=1
Create a service principal by using the az ad sp create-for-rbac command:
az ad sp create-for-rbac \ --name <app-name> \ --role Contributor \ --scopes "/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>"Nel comando :
-
<app-name> is an optional display name for the service principal. If you leave off the
--nameoption, a GUID is generated as the display name. -
<subscription-ID> is the GUID that uniquely identifies your subscription in Azure. If you don't know your subscription ID, you can run the az account show command and copy it from the
idproperty in the output. -
<resource-group-name> is the name of a resource group that contains the Azure Container Apps container. Il controllo degli accessi in base al ruolo è a livello di gruppo di risorse. Se sono stati seguiti i passaggi dell'esercitazione precedente, il nome del gruppo di risorse è
pythoncontainer-rg.
Salvare l'output di questo comando per il passaggio successivo. In particolare, salvare l'ID client (proprietà
appId), il segreto client ( proprietàpassword) e l'ID tenant ( proprietàtenant).-
<app-name> is an optional display name for the service principal. If you leave off the
Configure GitHub Actions by using the az containerapp github-action add command:
az containerapp github-action add \ --resource-group <resource-group-name> \ --name python-container-app \ --repo-url <https://github.com/userid/repo> \ --branch main \ --registry-url <registry-name>.azurecr.io \ --service-principal-client-id <client-id> \ --service-principal-tenant-id <tenant-id> \ --service-principal-client-secret <client-secret> \ --login-with-githubNel comando :
-
<resource-group-name> is the name of the resource group. In this tutorial, it's
pythoncontainer-rg. -
<https://github.com/userid/repo> è l'URL del tuo repository GitHub. In this tutorial, it's either
https://github.com/userid/msdocs-python-django-azure-container-appsorhttps://github.com/userid/msdocs-python-flask-azure-container-apps. In questi URLuseridè l'ID utente di GitHub. - <registry-name> is the existing Azure Container Registry instance that you created in the previous tutorial, or one that you can use.
-
<client-id> is the value of the
appIdproperty from the previousaz ad sp create-for-rbaccommand. The ID is a GUID of the form00000000-0000-0000-0000-00000000. -
<tenant-id> is the value of the
tenantproperty from the previousaz ad sp create-for-rbaccommand. The ID is also a GUID that's similar to the client ID. -
<client-secret> is the value of the
passwordproperty from the previousaz ad sp create-for-rbaccommand.
-
<resource-group-name> is the name of the resource group. In this tutorial, it's
In the configuration of continuous deployment, a service principal enables GitHub Actions to access and modify Azure resources. The roles assigned to the service principal restrict access to resources. The service principal was assigned the built-in Contributor role on the resource group that contains the container app.
If you followed the steps for the portal, the service principal was automatically created for you. If you followed the steps for the Azure CLI, you explicitly created the service principal before you configured continuous deployment.
Ridistribuire l'app Web con GitHub Actions
In this section, you make a change to your forked copy of the sample repository. Successivamente, è possibile verificare che la modifica venga distribuita automaticamente nel sito Web.
If you haven't already, make a fork of the sample repository (Django or Flask). È possibile apportare la modifica del codice direttamente in GitHub o nell'ambiente di sviluppo da una riga di comando con Git.
Go to your fork of the sample repository and start in the main branch.
Apportare una modifica:
- Passare al file /templates/base.html. (For Django, the path is restaurant_review/templates/restaurant_review/base.html.)
- Seleziona Modifica e modifica la frase
Azure Restaurant ReviewinAzure Restaurant Review - Redeployed.
On the bottom of the page you're editing, make sure that Commit directly to the main branch is selected. Then select the Commit changes button.
Il commit avvia il flusso di lavoro di GitHub Actions.
Nota
Questa esercitazione illustra come apportare una modifica direttamente nel ramo principale. Nei flussi di lavoro software tipici, si apporta una modifica in un ramo diverso da main e quindi si crea una pull request per unire la modifica nel main. Le pull request avviano anche il flusso di lavoro.
Understand GitHub Actions
Visualizzazione della cronologia del flusso di lavoro
Se è necessario visualizzare la cronologia del flusso di lavoro, utilizzare una delle procedure seguenti.
On GitHub, go to your fork of the sample repository and open the Actions tab.
Segreti del flusso di lavoro
The .github/workflows/<workflow-name>.yml workflow file that was added to the repo includes placeholders for credentials that are needed for the build and container app update jobs of the workflow. The credential information is stored encrypted in the repository's Settings area, under Security>Secrets and variables>Actions.
Se le informazioni sulle credenziali cambiano, è possibile aggiornarla qui. Ad esempio, se le password del Registro Azure Container vengono rigenerate, è necessario aggiornare il valore REGISTRY_PASSWORD. Per altre informazioni, vedere Encrypted secrets nella documentazione di GitHub.
App autorizzate OAuth
When you set up continuous deployment, you designate Azure Container Apps as an authorized OAuth app for your GitHub account. Container Apps utilizza l'accesso autorizzato per creare un file YAML di GitHub Actions in .github/workflows/<nome-flusso-di-lavoro>.yml. È possibile visualizzare le app autorizzate e revocare le autorizzazioni nell'account in Integrations>Applications.
Troubleshoot
Si verificano errori durante la configurazione di un principale di servizio tramite la CLI di Azure
This section can help you troubleshoot errors that you get while setting up a service principal by using the Azure CLI az ad sp create-for-rba command.
Se ricevi un errore contenente "InvalidSchema: non sono stati trovati adattatori di connessione":
Check the shell that you're running in. Se stai usando una shell Bash, imposta le variabili
MSYS_NO_PATHCONVcomeexport MSYS_NO_PATHCONV=1.For more information, see the GitHub issue Unable to create service principal with Azure CLI from Git Bash shell.
Se viene visualizzato un errore che contiene "Più applicazioni hanno lo stesso nome visualizzato":
- The name is already taken for the service principal. Scegli un altro nome oppure elimina l'argomento
--name. A GUID will be automatically generated as a display name.
Flusso di lavoro di GitHub Actions non riuscito
Per controllare lo stato di un flusso di lavoro, passare alla scheda Azioni del repository:
- If there's a failed workflow, drill into the workflow file. There should be two jobs: build and deploy. For a failed job, check the output of the job's tasks to look for problems.
- If there's an error message that contains "TLS handshake timeout," run the workflow manually. In the repo, on the Actions tab, select Trigger auto deployment to see if the timeout is a temporary issue.
- Se si configura la distribuzione continua per l'app contenitore come illustrato in questa esercitazione, il file del flusso di lavoro (.github/workflows/<nome del flusso di lavoro>.yml) viene creato automaticamente. Non è necessario modificare questo file per questa esercitazione. Se lo hai fatto, annulla le modifiche e prova il flusso di lavoro.
Website doesn't show changes that you merged in the main branch
In GitHub:
- Verificare che il flusso di lavoro di GitHub Actions sia stato eseguito e che sia stata verificata la modifica nel ramo che attiva il flusso di lavoro.
Nel portale di Azure:
Check the Azure Container Registry instance to see if a new Docker image was created with a time stamp after your change to the branch.
Controllare i log dell'app contenitore per verificare se si verifica un errore di programmazione:
- Go to the container app, and then go to Revision Management><active container>>Revision details>Console logs.
- Choose the order of the columns to show Time Generated, Stream_s, and Log_s.
- Sort the logs by most recent and look for Python
stderrandstdoutmessages in the Stream_s column. Pythonprintoutput isstdoutmessages.
Nella CLI di Azure:
- Use the az containerapp logs show command.
You want to stop continuous deployment
Stopping continuous deployment means disconnecting your container app from your repo.
Nel portale di Azure:
- Go to the container app. On the service menu, select Continuous deployment, and then select Disconnect.
Nella CLI di Azure:
- Use the az container app github-action remove command.
Dopo la disconnessione:
- Il file .github/workflows/<nome del flusso di lavoro>.yml viene rimosso dal tuo repository.
- Le chiavi segrete non vengono rimosse dal repository.
- Azure Container Apps rimangono un'app OAuth autorizzata per il tuo account GitHub.
- In Azure, the container is left with the last deployed container. You can reconnect the container app with the Azure Container Registry instance, so that new container revisions pick up the latest image.
- In Azure, service principals that you created and used for continuous deployment aren't deleted.
Rimuovere le risorse
Se hai finito con la serie di esercitazioni e non vuoi sostenere costi aggiuntivi, rimuovi le risorse usate.
La rimozione di un gruppo di risorse rimuove tutte le risorse nel gruppo ed è il modo più rapido per rimuovere le risorse. For an example of how to remove resource groups, see Containerize tutorial cleanup.
Contenuto correlato
Se si prevede di eseguire questa esercitazione, ecco alcuni passaggi successivi che è possibile eseguire: