I am trying to do web job creation, if not exist. for single webapp this line of command worked.
az webapp deployment source config-zip --resource-group "rg1" --name "webapp1" --src "path\file.zip"
Zip file structure was created as described as in https://github.com/projectkudu/kudu/wiki/WebJobs.
I can see the webjob created, but when I am putting it in the loop I am getting error. here is the code I have so far.
# Resource group name........
resourceGroup="rg1"
# Path to the zip file containing the web job
zipFilePath="zipfilepath/file.zip"
# Web job name
webJobName="webjob1"
# Get a list of all web apps within the resource group
webAppNames=$(az webapp list --resource-group $resourceGroup --query "[].name" -o tsv)
# Loop over each web app name
for webAppName in $webAppNames; do
# Check if the web job already exists in the web app
if az webapp webjob triggered list --resource-group $resourceGroup --name $webAppName --query "[?name=='$webJobName']" -o tsv | grep -q "$webJobName"; then
echo "Web job '$webJobName' already exists in web app '$webAppName'. Skipping deployment."
else
echo "Deploying web job '$webJobName' to web app: $webAppName"
az webapp deployment source config-zip --resource-group $resourceGroup --name $webAppName --src $zipFilePath
fi
done
Error
At line:14 char:4
+ for webAppName in "${webAppNames[@]}"; do
+ ~ Missing opening '(' after keyword 'for'. At line:14 char:42
+ for webAppName in "${webAppNames[@]}"; do
+ ~ Missing statement body in do loop.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
Before running the script, I found an error when pasting your code into my PowerShell environment.
As the provided script by you is completely a CLI code format, it is properly executing in Cloud Shell environment as shown below.
To make it work as expected when you execute in Windows PowerShell ISE environment, run the below modified script with PowerShell syntax.
Here I have used a foreach
loop to loop over all the listed webapps and also used an if
loop to check the existence.
$resourceGroup = "xxxx"
$zipFilePath = "C:\Users\xxx\source\repos\ConsoleApp7\ConsoleApp7\xxxx"
$webJobName = "xxxx"
$webAppNames = (az webapp list --resource-group $resourceGroup --query "[].name" -o tsv)
foreach ($webApp in $webAppNames) {
# Check if the web job already exists in the web app
$existing = az webapp webjob triggered list --resource-group $resourceGroup --name $webApp --query "[?name=='$webJobName']" -o tsv
if ($existing -contains $webJobName) {
Write-Host "Web job '$webJobName' already exists in web app '$webApp'. Skipping deployment."
}
else {
Write-Host "Deploying web job '$webJobName' to web app: $webApp"
az webapp deployment source config-zip --resource-group $resourceGroup --name $webApp --src $zipFilePath
}
}