Skip to content

Instantly share code, notes, and snippets.

@ChrisRomp
Last active February 16, 2023 18:38
Show Gist options
  • Save ChrisRomp/fa2e70b379afc76eedbc5498f91d701e to your computer and use it in GitHub Desktop.
Save ChrisRomp/fa2e70b379afc76eedbc5498f91d701e to your computer and use it in GitHub Desktop.
Azure .env File Generation
#!/bin/bash
# requires azure cli client and jq
# Get account info from Azure CLI
ACCOUNT=$(az account show)
# Get tenant name (from user query)
TENANT_NAME=$(az ad signed-in-user show --query 'userPrincipalName' | cut -d '@' -f 2 | sed 's/\"//')
# Parse tenant ID
TENANT_ID=$(echo $ACCOUNT | jq -r .tenantId)
# Parse subscription ID
SUBSCRIPTION=$(echo $ACCOUNT | jq -r .id)
# Get token from Azure CLI
AAD_TOKEN=$(az account get-access-token --query accessToken -o tsv)
# Folder path
WD=${CODESPACE_VSCODE_FOLDER:-${PWD:-.}}
# Create new .env file
cat <<EOF > .env
TENANT_ID=$TENANT_ID
TENANT_NAME=$TENANT_NAME
SUBSCRIPTION=$SUBSCRIPTION
AAD_TOKEN=$AAD_TOKEN
EOF
#!/bin/bash
# Get token from Azure CLI
TOKEN=$(az account get-access-token --query accessToken -o tsv)
# Folder path
WD=${CODESPACE_VSCODE_FOLDER:-${PWD:-.}}
# Update token in .env
sed -i "s/^AAD_TOKEN=\S*$/AAD_TOKEN=$TOKEN/gm" $WD/.env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment