-------
1. Prerequisites
-Azure Subscription and an Azure Function App created
-Azure CLI installed
-GitLab repository with Python function code
-Deployment credentials (Service Principal or Publish Profile)
---
2. Setup Deployment Credentials in GitLab
Option 1: Use Azure Service Principal (Recommended)
1. Create a Service Principal in Azure:
az ad sp create-for-rbac --name "gitlab-deploy" --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP>
Output:
{
    "appId": "xxxx-xxxx-xxxx-xxxx",
    "displayName": "gitlab-deploy",
    "password": "xxxx-xxxx-xxxx-xxxx",
    "tenant": "xxxx-xxxx-xxxx-xxxx"
}
2. Store these credentials in GitLab CI/CD Variables:
AZURE_APP_ID: xxxx-xxxx-xxxx-xxxx
AZURE_PASSWORD: xxxx-xxxx-xxxx-xxxx
AZURE_TENANT_ID: xxxx-xxxx-xxxx-xxxx
AZURE_SUBSCRIPTION_ID: <Your Azure Subscription ID>
AZURE_FUNCTIONAPP_NAME: <Your Function App Name>
---
3. Create .gitlab-ci.yml for CI/CD Pipeline
Add the following .gitlab-ci.yml file to the root of your repository:
stages:
  - deploy
variables:
  AZURE_RESOURCE_GROUP: "my-resource-group"
  FUNCTION_APP_NAME: "$AZURE_FUNCTIONAPP_NAME"
deploy_to_azure:
  image: mcr.microsoft.com/azure-cli
  stage: deploy
  script:
    - echo "Logging into Azure..."
    - az login --service-principal -u "$AZURE_APP_ID" -p "$AZURE_PASSWORD" --tenant "$AZURE_TENANT_ID"
    - az account set --subscription "$AZURE_SUBSCRIPTION_ID"
    - echo "Deploying Function App..."
    - func azure functionapp publish $FUNCTION_APP_NAME
  only:
    - main
This script:
Logs into Azure using Service Principal
Sets the subscription
Deploys the function app from the GitLab repository
---------
4. Enable Git Deployment in Azure
Ensure Git-based deployment is enabled on the Azure Function App:
az functionapp deployment source config --name <YOUR_FUNCTION_APP_NAME> --resource-group <YOUR_RESOURCE_GROUP> --repo-url <YOUR_GITLAB_REPO_URL> --branch main
-------------
5. Commit and Push Changes
git add .gitlab-ci.yml
git commit -m "Added GitLab CI/CD for Azure Function"
git push origin main
This will trigger the pipeline and deploy your function to Azure.
---------
6. Verify Deployment
After deployment, check logs with:
az functionapp log tail --name $FUNCTION_APP_NAME --resource-group $AZURE_RESOURCE_GROUP
Or visit the Azure Portal -> Function App -> "Deployment Center" to verify the latest deployment.
 
 

No comments:
Post a Comment