Deploying Function App using yaml

Abhijit M 25 Reputation points
2024-09-24T04:53:51.43+00:00

I am trying to figure out how to deploy a Function App in azure using pipeline yaml file
My runtime environment is python 3.11
this is the sample azure function i am trying to deploy

import azure.functions as func
import logging
import requests
import base64
import os

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

# Get values from environment variables
AZURE_DEVOPS_ORG = os.getenv('AZURE_DEVOPS_ORG')
PROJECT_NAME = os.getenv('PROJECT_NAME')
PIPELINE_ID = os.getenv('PIPELINE_ID')
PAT = os.getenv('PAT')

@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # Ensure all required environment variables are set
    if not all([AZURE_DEVOPS_ORG, PROJECT_NAME, PIPELINE_ID, PAT]):
        return func.HttpResponse("Environment variables are missing.", status_code=500)

    # Construct the full URL
    url = f"https://dev.azure.com/{AZURE_DEVOPS_ORG}/{PROJECT_NAME}/_apis/pipelines/{PIPELINE_ID}/runs?api-version=7.1"

    # Encode the PAT for Basic Authentication
    encoded_pat = base64.b64encode(f":{PAT}".encode()).decode()

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Basic {encoded_pat}"
    }

    # Prepare the request body
    body = {
        "resources": {
            "repositories": {
                "self": {
                    "refName": "refs/heads/main"
                }
            }
        }
    }

    # Make the API call
    try:
        response = requests.post(url, headers=headers, json=body)
        response.raise_for_status()  # Raises a HTTPError if the status is 4xx, 5xx
    except requests.exceptions.RequestException as e:
        logging.error(f"Error calling Azure DevOps API: {str(e)}")
        return func.HttpResponse(f"Error calling Azure DevOps API: {str(e)}", status_code=500)

    # Process the successful response
    if response.status_code in [200, 201]:
        response_data = response.json()
        run_id = response_data.get('id')
        run_state = response_data.get('state')
        pipeline_name = response_data.get('pipeline', {}).get('name')

        success_message = f"Pipeline '{pipeline_name}' (ID: {PIPELINE_ID}) triggered successfully. Run ID: {run_id}, State: {run_state}"
        logging.info(success_message)
        return func.HttpResponse(success_message, status_code=200)
    else:
        error_message = f"Unexpected response. Status code: {response.status_code}. Response: {response.text}"
        logging.error(error_message)
        return func.HttpResponse(error_message, status_code=response.status_code)

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,004 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.