octocloudlabs / travel-weather

A Python HTTP API to demonstrate Codespaces working with Copilot. Used during the 2024 Microsoft AI Tour events
MIT License
2 stars 2 forks source link

Automation for Azure deployment #3

Open DaveBurnisonMS opened 10 months ago

DaveBurnisonMS commented 10 months ago

Requires issue #2 to be complete !

Create a GitHub Actions workflow to automate the process of deploying this application to Azure Web Apps using a containerized workflow. This process will require setting up a container registry on Azure and creating an Azure Service Principal that has enough permissions for the deployment to initiate from the GitHub Action.

The Service Principal process creates a JSON blob that needs to be stored as a secret to be used within the GitHub Action.

DaveBurnisonMS commented 10 months ago

GitHub Copilot suggested steps

To build and deploy this solution to Azure API Management using GitHub Actions, you need to follow these steps:

Here is a sample GitHub Actions workflow file that you can use as a starting point:

name: Build and Deploy to Azure API Management

on:
  push:
    branches:
      - main  # Trigger this workflow on push to the main branch

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Login to Azure
      uses: azure/login@v1
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}

    - name: Deploy to Azure API Management
      run: |
        az apim create --name MyApiManagementService --resource-group MyResourceGroup --location WestUS --publisher-email email@example.com --publisher-name Contoso
        az apim import --name MyApiManagementService --resource-group MyResourceGroup --path '/api' --specification-format OpenApiJson --specification-url https://myapi.example.com/openapi.json
      env:
        AZURE_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
        AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
        AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
        AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}

Replace MyApiManagementService, MyResourceGroup, email@example.com, Contoso, and https://myapi.example.com/openapi.json with your actual values.

This workflow file does the following:

  1. Checks out your code.
  2. Sets up Python.
  3. Installs your Python dependencies.
  4. Logs in to Azure using the service principal.
  5. Creates an API Management service (if it doesn't exist).
  6. Imports your API into the API Management service.

Please note that you need to replace the placeholders in the az apim create and az apim import commands with your actual values. Also, you need to store your Azure service principal credentials as secrets in your GitHub repository.