|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +# Set up Azure credentials for GitHub Actions |
| 4 | +# This script creates a service principal with Contributor access to the GitHub resource group |
| 5 | +# If the service principal already exists, it will retrieve it instead of creating a new one |
| 6 | + |
| 7 | +# Login to Azure |
| 8 | +echo "Logging in to Azure..." |
| 9 | +az login |
| 10 | + |
| 11 | +# Service principal name |
| 12 | +SP_NAME="ai-agent-github" |
| 13 | +SUBSCRIPTION_ID=$(az account show --query id -o tsv) |
| 14 | +RESOURCE_GROUP="GitHub" |
| 15 | + |
| 16 | +# Check if service principal already exists |
| 17 | +echo "Checking if service principal '$SP_NAME' already exists..." |
| 18 | +SP_ID=$(az ad sp list --display-name "$SP_NAME" --query "[0].appId" -o tsv) |
| 19 | + |
| 20 | +if [ -n "$SP_ID" ]; then |
| 21 | + echo "Service principal '$SP_NAME' already exists." |
| 22 | + |
| 23 | + # Get existing service principal information without resetting credentials |
| 24 | + # Create JSON output in the format expected by GitHub Actions |
| 25 | + CLIENT_ID=$SP_ID |
| 26 | + TENANT_ID=$(az account show --query tenantId -o tsv) |
| 27 | + SUBSCRIPTION_ID=$(az account show --query id -o tsv) |
| 28 | + |
| 29 | + echo "Using existing service principal." |
| 30 | + echo "Important: To use this service principal, ensure your GitHub repository has" |
| 31 | + echo "the correct credentials already configured as AZURE_CREDENTIALS secret." |
| 32 | + echo "If you need to reset credentials, you can do so manually with:" |
| 33 | + echo "az ad sp credential reset --id $SP_ID --sdk-auth" |
| 34 | + |
| 35 | + # Display service principal information (without secret) |
| 36 | + echo "Service Principal Information:" |
| 37 | + echo "- Client ID: $CLIENT_ID" |
| 38 | + echo "- Tenant ID: $TENANT_ID" |
| 39 | + echo "- Subscription ID: $SUBSCRIPTION_ID" |
| 40 | + |
| 41 | + # Set SERVICE_PRINCIPAL to empty to avoid displaying sensitive info |
| 42 | + SERVICE_PRINCIPAL='{}' |
| 43 | +else |
| 44 | + # Create service principal |
| 45 | + echo "Creating service principal for GitHub Actions..." |
| 46 | + SERVICE_PRINCIPAL=$(az ad sp create-for-rbac \ |
| 47 | + --name "$SP_NAME" \ |
| 48 | + --role contributor \ |
| 49 | + --scopes /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP \ |
| 50 | + --sdk-auth) |
| 51 | + |
| 52 | + echo "Service principal created. Add the following secret to your GitHub repository as AZURE_CREDENTIALS:" |
| 53 | +fi |
| 54 | + |
| 55 | +echo $SERVICE_PRINCIPAL |
0 commit comments