How to replace AZ File copy task with Azure Cli task - azure-devops

I have a task in my Yaml pipeline to upload artifacts in Blob Storage. I was able to do it for Windows agent using AZ File Copy task(shown below) but it failed for Linux and Mac agent. I was suggested here to use Azure CLI task instead to make it work with Linux and Mac agent. Below is the Azure Cli task which I have written but it fails saying error as "[error]Script failed with error: Error: Unable to locate executable file: 'powershell'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
"
I would like some help in framing the Azure Cli task in proper way which corresponds to my Azure File copy task. here are both tasks. Please suggest what wrong am I doing?
- task: AzureFileCopy#2
displayName: 'Publish to Blob'
inputs:
SourcePath: '$(Build.SourcesDirectory)/ABC-$(osSuffix)'
azureSubscription: 'Azure CICD'
Destination: AzureBlob
storage: '$(BlobStorageAccount)'
ContainerName: '$(BlobContainer)'
BlobPrefix: '$(BlobPrefix)/ABC/$(DeploymentVersion)/ABC-$(osSuffix)'
AdditionalArgumentsForBlobCopy: '/V /S'
outputStorageUri: BlobUri
outputStorageContainerSasToken: BlobSASToken
task: AzureCLI#2
displayName: PublishToBlob
inputs:
azureSubscription: 'Azure CICD'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: 'az storage blob upload -f '$(Build.SourcesDirectory)/ABC-$(osSuffix)' -c '$(BlobContainer)' -n '$(BlobPrefix)/ABC/$(DeploymentVersion)/ABC-$(osSuffix)''

Finally figured it out:
Here you go:
- task: AzureCLI#2
displayName: PublishToBlob
inputs:
azureSubscription: 'Azure CICD'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: az storage blob upload-batch -d "$(BlobContainer)/ABC/$(DeploymentVersion)/ABC-$(osSuffix)" --account-name "mystorageaccount" -s "$(Build.SourcesDirectory)/ABC-$(osSuffix)"

Related

AzureCLI task in Azure DevOps pipeline cannot find the location of scripts

I am using Azure DevOps pipeline and in one of my tasks I need to run a bash script which contains some Azure CLI scripts. I have put this script in a folder called scripts, and my pipeline is running in pipelines folder. Pipelines and script folders are at the same level in root directory. The following shows the part of my pipeline where I run the AzureCLI#2 task, but when the pipeline runs it raises the error that it cannot find the file!
I have already pushed everything in the repository and I can see the files. However, the pipeline cannot find it. I am using AzureCLI#2 documentation link to provide values for this task. The part of pipeline that uses AzureCLI is as follows:
pool:
vmImage: ubuntu-20.04
trigger:
branches:
include:
- "feature/ORGTHDATAMA-4810"
exclude:
- "main"
- "release"
paths:
include:
- "dip-comma-poc/**"
variables:
- group: proj-comma-shared-vg
stages:
- stage: DownloadArtifact
displayName: "Download python whl from artifactory"
jobs:
- job: DownloadArtifactJob
steps:
- checkout: self
## To download from devops artifactory with AZ CLI
## https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/azure-cli-v2?view=azure-pipelines
- task: AzureCLI#2
inputs:
azureSubscription: "sc-arm-pa042-man"
scriptType: 'bash'
scriptLocation: 'scriptPath'
scriptPath: 'dip-comma-poc/deployment-pipelines/scripts/sp-login.sh'
arguments: '$(SVCApplicationID) $(SVCSecretKey) $(SVCDirectoryID)'
displayName: "Download python whl from artifactory"
This caused the following error:
To resolve the error I tried using relative path in scriptPath as following but it caused the same error:
- task: AzureCLI#2
inputs:
azureSubscription: "sc-arm-pa042-man"
scriptType: 'bash'
scriptLocation: 'scriptPath'
scriptPath: './scripts/sp-login.sh'
arguments: '$(SVCApplicationID) $(SVCSecretKey) $(SVCDirectoryID)'
displayName: "Download python whl from artifactory"
I also tried inlineScript but again it cannot find the file.
- task: AzureCLI#2
inputs:
azureSubscription: "sc-arm-pa042-man"
scriptType: 'bash'
scriptLocation: 'inlineScript'
arguments: '$(SVCApplicationID) $(SVCSecretKey) $(SVCDirectoryID)'
inlineScript: './scripts/sp-login.sh $1 $2 $3'
displayName: "Download python whl from artifactory"
This also raised the same error:
How can I refer to my script in the pipeline yaml file so that it does not raise "No such file or directory error" as shown above? Thank you.
Open the Git repository on the web UI of your Azure DevOps and then check whether its file structure is looking like as below image shows.
If it is same as this file structure. You need to change the file path set on the Azure CLI task to be "deployment-pipelines/scripts/sp-login.sh" instead of "dip-comma-poc/deployment-pipelines/scripts/sp-login.sh".
- task: AzureCLI#2
inputs:
azureSubscription: "sc-arm-pa042-man"
scriptType: 'bash'
scriptLocation: 'scriptPath'
scriptPath: 'deployment-pipelines/scripts/sp-login.sh'
arguments: '$(SVCApplicationID) $(SVCSecretKey) $(SVCDirectoryID)'
displayName: "Download python whl from artifactory"

Azure devops deploy to function app with access restrictions

I am trying to deploy a function app via an Azure DevOps pipeline, however I am receiving the following error:
##[error]Failed to deploy web package to App Service.
##[error]To debug further please check Kudu stack trace URL : $URL_REMOVED
##[error]Error: Error: Failed to deploy web package to App Service. Ip Forbidden (CODE: 403)
From some googling a suggested solution seems to be to whitelist agent IP before the deployment, and then remove it after. I have added this to my pipeline, and I can see the agent IP get added to access restrictions, however the deployment still fails.
Here is my pipeline file:
# Node.js Function App to Linux on Azure
# Build a Node.js function app and deploy it to Azure as a Linux function app.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- main
variables:
# Azure Resource Manager connection created during pipeline creation
azureSubscription: 'xxx'
# Function app name
functionAppName: 'xxx'
# Environment name
environmentName: 'xxx'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
fi
displayName: 'Build extensions'
- script: |
npm install
npm run build --if-present
npm run test --if-present
displayName: 'Prepare binaries'
- task: ArchiveFiles#2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop
- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: $(environmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureCLI#2
inputs:
azureSubscription: '$(azureSubscription)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
agentIP=$(curl -s https://api.ipify.org/)
az functionapp config access-restriction add -g xxx -n xxx --action Allow --ip-address $agentIP --priority 200
- task: AzureFunctionApp#1
displayName: 'Azure Functions App Deploy: xxx'
inputs:
azureSubscription: '$(azureSubscription)'
appType: functionAppLinux
appName: $(functionAppName)
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
Is anyone able to advise where I am going wrong?
I've had a simmilar issue while adding the agent IP to the network restrictions of an storage account (using Powershell but you'll understand the idea), we added a 60s sleep to be sure that the setting are taken into account by Azure.
$sa_name = "sapricer$env_prefix"
if ($null -ne (Get-AzStorageAccount -ResourceGroupName $sa_rg -AccountName $sa_name -ErrorAction Ignore)) {
Write-Output "Storage account '$sa_name' exists"
if ($enable) {
Write-Output "Add ip rule for $current_ip on $sa_name..."
Add-AzStorageAccountNetworkRule -ResourceGroupName $sa_rg -AccountName $sa_name -IPAddressOrRange $current_ip
}
else {
Write-Output "Remove ip rule for $current_ip on $sa_name..."
Remove-AzStorageAccountNetworkRule -ResourceGroupName $sa_rg -AccountName $sa_name -IPAddressOrRange $current_ip
}
}
Start-Sleep -Seconds 60
I found the solution to this.
Function Apps have two IP Restriction sections, one for the App and one for the SCM site. The SCM site is the one that requires the IP to be whitelisted in order for the deployment to work:
az functionapp config access-restriction add --scm-site true -g xxx -n xxx --action Allow --ip-address $agentIP --priority 200
You can deploy Azure function app to azure devops pipeline using azure function app task from Azure devops pipeline tasks
Here is the sample snippet for deploying azure function app
variables:
azureSubscription: Contoso
# To ignore SSL error, uncomment the below variable
# VSTS_ARM_REST_IGNORE_SSL_ERRORS: true
steps:
- task: AzureFunctionApp#1
displayName: Azure Function App Deploy
inputs:
azureSubscription: $(azureSubscription)
appName: samplefunctionapp
package: $(System.DefaultWorkingDirectory)/**/*.zip
Here is the Microsoft Document for deploying azure function app.

AzureCLI#2 Task - ADO Pipeline - change the default working directory

I need help in changing the default working directory for my Azure CLI Task. The below code is not working for me.
- task: AzureCLI#2
displayName: 'dbt debug'
inputs:
azureSubscription: XXXX
ScriptType: bash
scriptLocation: inlineScript
inlineScript: |
dbt --version
dbt debug --profiles-dir $(location)
workingDirectory: '$(System.DefaultWorkingDirectory)'
It throws the error - workingDirectory:: command not found.
It throws the error - workingDirectory:: command not found.
It seems that this is a YAML format issue.
You can try the sample:
- task: AzureCLI#2
displayName: 'dbt debug'
inputs:
azureSubscription: XXXX
ScriptType: bash
scriptLocation: inlineScript
workingDirectory: '$(System.DefaultWorkingDirectory)'
inlineScript: |
dbt --version
dbt debug --profiles-dir $(location)
Here is the doc about Azure CLI task

Error: Unable to locate executable file: 'powershell' when running Azure CLI task in ADO pipeline

I am trying to run an Azure CLI task in pipeline and getting the following error :
Starting: AzureCLI
==============================================================================
Task : Azure CLI
Description : Run Azure CLI commands against an Azure subscription in a PowerShell
Core/Shell script when running on Linux agent or PowerShell/PowerShell Core/Batch script when running on Windows agent.
Version : 2.1.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-cli
==============================================================================
##[error]Script failed with error: Error: Unable to locate executable file: 'powershell'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
Finishing: AzureCLI
The pre-requisites mentioned in https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-cli?view=azure-devops is fulfilled.
**Pre-requisites mentioned in the ms site :**
Microsoft hosted agents have Azure CLI pre-installed. However if you are using private agents, install Azure CLI on the computer(s) that run the build and release agent. If an agent is already running on the machine on which the Azure CLI is installed, restart the agent to ensure all the relevant stage variables are updated.
I am not using any private agents. I am using a free subscription.
The task in pipeline yaml is as :
- task: AzureCLI#2
inputs:
azureSubscription: 'Free Trial(<My Subscription id>)'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: |
az --version
az account show
Why is the agent not able to find powershell in its system!!
Is this a bug?
Thanks!
I think you might be using a Linux agent such as 'ubuntu-latest'. Try changing it back to AzureCLI#2 and set scriptType: pscore. scriptType: ps doesn't work on Linux.
- task: AzureCLI#2
displayName: Azure CLI
inputs:
azureSubscription: 'sc-name'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
az account show
Hi try without "scriptType" and change the CLI version to 1, Please see the below script,
- task: AzureCLI#1
inputs:
azureSubscription: 'Free Trial(<My Subscription id>)'
scriptLocation: 'inlineScript'
inlineScript: |
az --version
az account show

Azure Pipeline Error Using AzureCLI Task: Wrong Number of Segments

I'm getting the following error while trying to use the Azure CLI task in my DevOps YAML pipeline:
"An error occurred while loading the YAML build pipeline. wrong number of segments"
I'm following this documentation, running on a self-hosted Windows agent with Azure CLI and PowerShell Core installed.
- task: AzureCLI#2
displayName: Azure CLI
inputs:
azureSubscription: MyServiceName
scriptType: pscore
scriptLocation: inlineScript
inlineScript: az --version
UPDATED: There are no other tasks in the file and it is valid. As an experiment I changed the task to AzureCLI#1 and it worked (v1 does not include Script Type):
- task: AzureCLI#1
displayName: Azure CLI
inputs:
azureSubscription: MyServiceName
scriptLocation: inlineScript
inlineScript: az --version
If a task is referenced by a wrong version number, we could get the error message: An error occurred while loading the YAML build pipeline. wrong number of segments
As a workaround, downgraded the task AzureCLI to version 1, then the task will run successfully.
Note: michaelrp has raised this issue in the GitHub, please follow this ticket to get the latest news.
As michaelrp found the issue was version 2 of AzureCLI task. When he downgraded to version 1, the error goes away. It appers only on self hosted agent.
This is caused by some issue with your YAML file. Please validate your YAML file and make sure it passes
It can be caused also by wrong number of task which is fine here AzureCLI#2.
It could be also an issue with service connection, please make sure you use valid.
To give you an better answer I need to see your full YAML file.
I tested this and all went fine:
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI#2
inputs:
azureSubscription: 'rg-the-code-manual'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: 'az --version'