AzurePowerShell task cannot find filePath - azure

I'm trying to run a PowerShell script in Azure Yaml Pipelines and I'm getting this error:
##[error]The term 'D:\a\1\s\myPowershellFile.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Code:
jobs:
- deployment: Deploy
displayName: Deploy
environment: $(myEnvironment)
pool:
vmImage: 'windows-latest'
strategy:
runOnce:
deploy:
steps:
- task: AzurePowerShell#5
displayName: 'Run Powershell script'
inputs:
azureSubscription: $(azureConnectionName)
scriptType: filePath
scriptPath: './myPowershellFile.ps1'
azurePowerShellVersion: latestVersion
The file is pushed out to the repo for the branch that is triggering the build. I've also tried referencing the path explicitly with $(Pipeline.Workspace) and $(Build.SourcesDirectory). Version 4 also does not work. According to the docs this should be working!

After some more research I discovered this article which says that files are not automatically downloaded for deployment jobs. I added this step, and that fixed the issue!
- checkout: self

Couldn't help noticing but you have used the forward slash "/" as the seperator for the filepath instead of a back-slash "\" like in the example you are pointing to in the azure docs :
- task: AzurePowerShell#5
inputs:
azureSubscription: my-arm-service-connection
scriptType: filePath
scriptPath: $(Build.SourcesDirectory)\myscript.ps1
This looks like the reason why.
There is a similar issue here. looks like it depends on whether you are using a windows or linux agent : cannot locate file in azure devops pipeline

Related

Azure pipeline can't find script when preceded by other task

In my yaml file I have two tasks : CopyFiles and run a Powershell script.
If only the Powershell task is there, then no problem. I runs fine. So the ScriptPath is OK.
But with the CopyFiles task preceding, it isn't able to find the Powershell file anymore (see screenshot).
Anyone has an idea?
jobs:
- job: sync_wiki_repo
displayName: 'Sync wiki'
steps:
- checkout: wiki
- task: CopyFiles#2
displayName: Copy wiki files
inputs:
contents: 'Wiki/Distribution/Client-Welcome-Page/Readme.md'
**targetFolder**: '$(System.DefaultWorkingDirectory)/wiki-staging'
overWrite: true
- checkout: git://something/set-wiki #checkout specific branch
- task: AzurePowerShell#5
displayName: 'Set wiki'
inputs:
azureSubscription: 'mysub'
ScriptType: 'FilePath'
**ScriptPath**: '1.0/scripts/sync-wiki-repo.ps1'
ScriptArguments: '-wikipat $(e2e-pipelines-manage-wiki-secret) -project ${{parameters.project}}'
azurePowerShellVersion: 6.4.0
workingDirectory: '$(System.DefaultWorkingDirectory)'
As this is running on an Ubuntu Linux machines, the names of the folders and files are case sensitive. I doublechecked that. And I added "E2E-Pipelines" to the Scriptpath.
ScriptPath:'E2E-Pipelines/1.0/scripts/sync-wiki-repo.ps1'
Now my powershell file can be found.
It is solved.

Mutiple repositories checkout issue in Azure Devops

My code has multiple checkout's and when I run the pipeline, the directory path is not recognized for one of the checkout.
jobs:
- job: job1
steps:
- checkout: rep1
path: test1
- checkout: rep2
path: test2
- task: AzurePowerShell#5
inputs:
azureSubscription:xxxxx
ScriptType: 'FilePath'
ScriptPath: '$(System.DefaultWorkingDirectory)/folder1/myPowershell.ps1'
azurePowerShellVersion: 'LatestVersion'
I see a warning which says "Module path not present as expected in hosted agent, skipping step to make module available". The Powershell script is never executed. Any help, please?
According to the official docs:
If you have multiple checkout steps in your job, your source code is checked out into directories named after the repositories as a subfolder of s in (Agent.BuildDirectory). If (Agent.BuildDirectory) is C:\agent\_work\1 and your repositories are named tools and code, your code is checked out to C:\agent\_work\1\s\tools and C:\agent\_work\1\s\code.
So, it looks like the ScriptPath property of AzurePowerShell#5 task in your pipeline should be built keeping this in mind. If the PowerShell file resides in folder1 directory of the repository rep1, then the path should look like
$(Agent.BuildDirectory)/rep1/folder1/myPowershell.ps1.

Azure DevOps Parameters not recognised by AZ CLI

I am having issues passing parameters defined in an Azure Pipeline YAML to AZ Cli located in a bash script. I found the following solution on Stackoverflow but it doesn't seem to work:
Pass parameter to Azure CLI Task in DevOps
Here is my YAML file:
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI#2
displayName: Azure CLI
inputs:
azureSubscription: templateresourceconnection
scriptType: bash
scriptPath: ./deployment.sh
arguments:
-resourceGrp 'TEST-RG'
-location 'westeurope'
I would expect to be able to access the arguments in my deployment.sh, which fails:
#!/bin/bash
# Create Resource Group
az group create -n $(resourceGrp) -l $(location)
If I don't pass any arguments and hardcode the values in deployment.sh it all works fine.
Any ideas what could cause this issue? I also tried with UPPERCASE and just without brackets.
I get the following error message
Do you have any idea what else I could try to make it work. Seems like the documentation doesn't contain any example for az cli. Just how to define parameters but not how to pass them afterwards.
Thank you.
Do you have any idea what else I could try to make it work
You could try to use the Environment variable. Environment variables can be accessed by bash script files.
First of all, you need to define pipeline variable instead of task argument.
variables:
- name: one
value: initialValue
Here is my example: Used in Linux system.
az group create -n $RESOURCEGRP -l $LOCATION
Note: All characters in environment variables need to be capitalized.
e.g.: resourceGrp -> $RESOURCEGRP
Yaml sample:
variables:
- name: resourceGrp
value: TEST-RG
- name: location
value: westeurope
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI#2
displayName: 'Azure CLI deploy.sh'
inputs:
azureSubscription: kevin0209
scriptType: bash
scriptPath: ./deployment.sh

AzurePowerShell#4 - powershell commands fail to execute on azure pipeline

As a part of my yml pipeline definition, I have a AzurePowerShell#4 task, following is an extract from my pipeline definition
stages:
- stage: DeployDemoCluster
jobs:
- job: 'DeployAKSAndAll'
pool:
vmImage: 'windows-latest'
steps:
- task: AzurePowerShell#4
displayName: Store AI instrumentation key for Inbound Processor in central KeyVault
inputs:
azureSubscription: 'service-connection'
azurePowerShellVersion: LatestVersion
pwsh: true
ScriptType: 'FilePath'
ScriptPath: 'AKS/ps/update_kv_firewall.ps1'
The issue is, within my update_kv_firewall.ps1, all the powershell commands fail with the error, for example:
[error]Login-AzureRmAccount : The term 'Login-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file, or operable program.
The script when executed individually / standalone, works perfectly fine.
what am I missing here?
As per your comment: the command "Get-AzKeyVault" runs without any errors, while 'Get-AzureRmVirtualNetwork' leads to errors.
Then I'm sure that you're installing the new Az module of azure powershell. So the command like Get-AzKeyVault can work.
Since you're installing Az module, please use all the commands from Az module. Almost each azure Rm command has an equivalent azure Az command, you can find it from the Az command list.
Note: the command like Get-AzureRmVirtualNetwork / Login-AzureRmAccount is from azure RM module, which will be retired this year later.

Deploy a Bot Framework solution using Azure DevOps Pipeline

We have a Bot Framework solution Virtual Assistant which we would like to deploy via an Azure Pipeline connected to GitHub.
Due to the Virtual Assistant template we need to run a specific PowerShell Script to achieve this but I'm getting stuck with the actual script arguments with Azure CLI.
What would be the best way to achieve this. There is no documentation for deploying the Virtual Assistant using the pipeline.
I've got so far but I'm now getting the error: ERROR: az bot prepare-deploy: error: argument --proj-file-path: expected one argument
All I can see is that the publish.ps1 script should get this argument.
Below is the pipeline:
# Branch trigger needs changing to master/release etc.
trigger:
- production-cicd
# # Do not trigger build.
# trigger: none
# Disable PR builds
pr: none
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- task: CopyFiles#2
displayName: Copy files before publishing script run.
inputs:
SourceFolder: '$(Agent.BuildDirectory)'
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
CleanTargetFolder: true
- task: AzureCLI#2
displayName: Deploy Core Bot
inputs:
azureSubscription: 'My Bot Subscription Name(xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx)'
scriptType: 'pscore'
scriptLocation: 'scriptPath'
scriptPath: '$(Build.ArtifactStagingDirectory)/s/botname/botname/Deployment/Scripts/publish.ps1'
arguments: '-resourceGroup "assistantname-rg" -name "assistantname" -projFolder "."'

Resources