How can i pass an azure cli variable to Azure Devops variables? - azure

I have written an Azure cli task in Azure Devops as below
$vnetid = $(az network vnet show -n $vnet -g $(rg) --query id --output tsv)
I want to output the $vnetid to the Azure Devops Variables so it can be used in another task how can i do this?

I guess you can simply do
Write-Output("##vso[task.setvariable variable=vnetid;]$$vnetid")

Related

Zip-deploy Azure functions while public access is disabled (access regstrictions are in place)

I disabled Allow public access on an Azure Function App for security reasons:
However after that, zip deploys from Visual Studio (publish functionality) and via Github Actions fail, as also the API for deployment isn't publicly reachable anymore. In that case, how would one zip deploy updated Azure functions?
If you need ZIP deployment, you need to deploy the code from inside the VNET to the private endpoint of the AppService. For GitHub Actions, this means deploying a self-hosted runner on a VM inside the VNET to push the new code to AppService. There is an example for Azure DevOps self-hosted agents.
As an alternative, you can publish your ZIP to a storage account and let the function pull the new application code to run. There is also a full example including GitHub Actions.
The most relevant quotes from the second example are:
This article shows how to deploy to a Private Endpoint-enabled site from a Continuous Integration pipeline (such as GitHub Actions, Circle CI, Jenkins, or Travis CI) without having to self-host the CI service on a VM. Since Private Endpoints disables all inbound traffic from the internet, our CI pipeline will publish the files to a storage account and give the web app a SAS URL to files. Once the web app is given this SAS URL, it will pull the files from the storage account.
- name: Set SAS token expiration
run: echo "expiry=`date -u -d "$EXPIRY_TIME" '+%Y-%m-%dT%H:%MZ'`" >> $GITHUB_ENV
- name: Azure CLI script
uses: azure/CLI#v1
with:
azcliversion: 2.19.1
inlineScript: |
az extension add --name webapp
az storage account create -n $ACCOUNT -g $GROUP -l westus
az storage container create -n $CONTAINER --account-name $ACCOUNT
az storage blob upload -f app.zip --account-name $ACCOUNT -c $CONTAINER -n $ACCOUNT
ZIP_URL=$(az storage blob generate-sas --full-uri --permissions r --expiry ${{ env.expiry }} --account-name $ACCOUNT -c $CONTAINER -n $ACCOUNT | xargs)
az webapp deploy --name $WEBAPP --resource-group $GROUP --type zip --src-url $ZIP_URL --async false
az storage container delete -n $CONTAINER --account-name $ACCOUNT

Azure Function App - Swap deployment Slots stuck then crashing completely

Recently my function apps have stopped swapping slots, they worked fine for months but suddenly they have all stopped swapping by taking a long time then crashing. I have recreated the function apps using the same scripts and have replicated the issue but still can't fix.
Is there anything I am setting to stop the slots from switching?
Here is the fundamental build script I am using. I am able to run this but when trying to swap slots in Azure it freezes then crashes with a vague error message:
# Azure Login
az login
# Set deployment Environment
$environment = "stage"
# Resource Group Variables
$subscriptionId = ""
$domain = "test4"
$resourceGroup = "xx-platform-$domain-$environment"
$region = "northEurope"
# Functions app variables
$storageName = "xxfn$domain$environment" # must be less than 24 chars and all lower case
$functionAppDeploymentSlotName = "test"
$functionAppName = "xx-platform-$domain-fn-$environment"
$functionAppEnvironment = "AZURE_FUNCTIONS_ENVIRONMENT=Development"
$websiteRunFromPackage = "WEBSITE_RUN_FROM_PACKAGE=1"
#########################################################################################################################################################################
## Resource Group
Write-output "Creating resource group";
# Set subscription
az account set --subscription $subscriptionId
# Create resource group
az group create -l $region -n $resourceGroup
#########################################################################################################################################################################
## Functions App
Write-output "Creating Functions App";
# Create storage account
az storage account create --name $storageName --location $region --resource-group $resourceGroup --sku Standard_LRS
# Create functions app - using consumption plan
az functionapp create --name $functionAppName --storage-account $storageName --consumption-plan-location northEurope --resource-group $resourceGroup --functions-version 4
# Set functions app configuration settings
# Environment
az functionapp config appsettings set --name $functionAppName --resource-group $resourceGroup --settings $functionAppEnvironment
# WEBSITE_RUN_FROM_PACKAGE
az functionapp config appsettings set --name $functionAppName --resource-group $resourceGroup --settings $websiteRunFromPackage
# SET DOT NET FRAMEWORK ERSION
az functionapp config set --net-framework-version v6.0 -g $resourceGroup -n $functionAppName
# Create functions app deployment slot
az functionapp deployment slot create --name $functionAppName --resource-group $resourceGroup --slot $functionAppDeploymentSlotName
I have modified your script in initializing the variable values directly to the cmdlets:
# Azure Login
az login
az account set --subscription "<Ur_Azure_Subscription_Id>"
# Function App Create
az functionapp create --name dt-platform-fn-stage --storage-account dtfnstageenvironment --consumption-plan-location northEurope --resource-group HariTestRG --runtime dotnet --functions-version 4
# Environment
az functionapp config appsettings set --name dt-platform-fn-stage --resource-group HariTestRG --settings AZURE_FUNCTIONS_ENVIRONMENT=Development
# WEBSITE_RUN_FROM_PACKAGE
az functionapp config appsettings set --name dt-platform-fn-stage --resource-group HariTestRG --settings WEBSITE_RUN_FROM_PACKAGE=1
# SET DOT NET FRAMEWORK ERSION
az functionapp config set --net-framework-version v6.0 -g HariTestRG -n dt-platform-fn-stage
# Create functions app deployment slot
az functionapp deployment slot create --name dt-platform-fn-stage --resource-group HariTestRG --slot test
Created the Http Trigger Function using the cmdlets given in the below given MS Document.
For Continuous deployments, this app setting needs to be configured:
az functionapp config appsettings set --name dt-platform-fn-stage --resource-group HariTestRG --settings "SCM_DO_BUILD_DURING_DEPLOYMENT=true"
Deployed the Function to the Azure Functions using az cli cmdlet given in one of my workarounds.
By default, Auto Swap on Slots will be disabled:
It is working fine during the slot swapping and make sure you have followed the considerations mentioned in this MS Doc that also provides the list of az cli cmdlets for managing the slots like creating, listing, deleting, swapping and auto swapping configuration.
Above Azure CLI Commands have been taken from this MS Doc references.

How to get workspace ID of Azure Log Analytics using az CLI?

How to get workspace ID of Azure Log Analytics using az CLI ?
I want to use this ID in a script when creating AKS
az aks create --name myk8s --resource-group rg-myk8s \
--workspace-resource-id XXX
You can run the following command to get the Workspace ID:
az monitor log-analytics workspace show --resource-group MyResourceGroup --workspace-name MyWorkspace --query customerId -o tsv

how can I get the vnet id into a variable using Azure CLI

I want to get the vnet id of an azure vnet into a variable using azure cli.
i then want to use this variable in the azure devops pipeline and populate a variable there?
How can i do this?
You can use the Azure CLI task making use of the az network vnet commands, e.g.
- task: AzureCLI#2
displayName: Azure CLI
inputs:
azureSubscription: <Name of the Azure Resource Manager service connection>
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az network vnet show -g MyResourceGroup -n MyVNet
In the same task's inline script you're free to use any powershell, so could also set variables in that script for use in subsequent steps. Hope this helps!

How to edit configuration settings in Azure Function for Azure Gov

I'd like to use code like this in my Azure Function:
var key = ConfigurationManager.AppSettings["SubscriptionKey"];
Given that there's no UI for Azure Functions in the Azure Government portal yet, is there a way to add this via the Azure CLI or is there another way to make this happen?
Update
I figured it out:
az functionapp config appsettings set --name appname --settings SubscriptionKey=1234 --resource-group resourcegroupname
As you've already found out, you can do this via Azure CLI with the following command:
az functionapp config appsettings set --name appname --settings SubscriptionKey=1234 --resource-group resourcegroupname

Resources