VivekAnandChakravarthy
VivekAnandChakravarthy

Reputation: 677

How to replace files from one folder to another folder in azure web app kudu site using the script

Context:

  1. Deploying the .NET Core Web app through Azure DevOps CI/CD Pipelines
  2. After Release Pipeline, I need to replace files from one folder to another folder in the Kudu Site.

For example, source path is C:\home\License\*.* and the destination path is C:\home\site\wwwroot\wwwroot\License\.

Release Pipeline:

enter image description here

Error:

2024-01-03T12:09:23.8052852Z ##[section]Starting: Command Line Script
2024-01-03T12:09:23.8178628Z ==============================================================================
2024-01-03T12:09:23.8178789Z Task         : Command line
2024-01-03T12:09:23.8178893Z Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
2024-01-03T12:09:23.8179040Z Version      : 2.231.1
2024-01-03T12:09:23.8179117Z Author       : Microsoft Corporation
2024-01-03T12:09:23.8179229Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
2024-01-03T12:09:23.8179378Z ==============================================================================
2024-01-03T12:09:24.9976497Z Generating script.
2024-01-03T12:09:25.0087762Z Script contents: shell
2024-01-03T12:09:25.0096759Z az webapp copy --resource-group Practice-RG --name basicdotnetwebapp --src-path C:\home\License\*.* --dest-path C:\home\site\wwwroot\wwwroot\License\
2024-01-03T12:09:25.0441739Z ========================== Starting Command Output ===========================
2024-01-03T12:09:25.0730789Z ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\<guid>.cmd""
2024-01-03T12:10:37.0437891Z ERROR: 'copy' is misspelled or not recognized by the system.
2024-01-03T12:10:37.0439572Z 
2024-01-03T12:10:37.0440014Z Examples from AI knowledge base:
2024-01-03T12:10:37.0440430Z https://aka.ms/cli_ref
2024-01-03T12:10:37.0440810Z Read more about the command in reference docs
2024-01-03T12:10:39.0472120Z ##[error]Cmd.exe exited with code '2'.
2024-01-03T12:10:39.1911632Z ##[section]Finishing: Command Line Script

Upvotes: 0

Views: 341

Answers (1)

SiddheshDesai
SiddheshDesai

Reputation: 8167

You need to make use of Kudu Rest API via Powershell to access and replace a file in Kudu:-

Reference

My Azure DevOps Release pipeline:- Powershell Code:-

$userName = "`$siliconwebapp098"  
# Provide Password from WebDeploy Publish Profile  
$password = "xxxxxxx9reT0tX"  
# Encode username and password to base64 string  
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $userName, $password)))  
# Construct command API Body. DOS command to create a folder called TempLogsForDebugging  
$bodyToPOST = @{  
command = "md TempLogsForDebugging"
                 dir = "D:\home\site\wwwroot"  
}  
# Splat all parameters together in $param  
$param = @{  
            # command REST API url  
            Uri = "https://siliconwebapp098.scm.azurewebsites.net/api/command"  
            Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}  
            UserAgent = "powershell/1.0"  
            Method = "POST"  
            Body = (ConvertTo-Json $bodyToPOST)  
            ContentType = "application/json"  
}  
# Invoke REST call  
Invoke-RestMethod @param 

enter image description here

Output:-

enter image description here

One more alternative is to use Kudu Tasks Upload

enter image description here

And specify your destination path accordingly:-

enter image description here

In order to get Publish profile via powershell and get it into a variable refer this SO answer by Jahnavi

Upvotes: 3

Related Questions