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

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

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"

AzureStaticWebApp#0 not recognizing deployment token from variable

Hi have the following code that deploys an artifact to an Azure Static Web App:
...
variables:
- name: staticWebAppDeploymentToken
...
# This steps reads the deployment token of the static web app and assigns it on a variable
- task: AzureCLI#2
displayName: 'Retrieve static web app deployment token'
inputs:
azureSubscription: xxxx
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
output=$(az staticwebapp secrets list --name xxxx-xxxx-$(environment) | jq .properties.apiKey)
echo "##vso[task.setvariable variable=staticWebAppDeploymentToken;]$output"
- task: AzureStaticWebApp#0
inputs:
output_location: '/'
cwd: '$(Pipeline.Workspace)/artifact'
skip_app_build: true
azure_static_web_apps_api_token: $(staticWebAppDeploymentToken)
I get the error:
I've set the System.Debug variable to true, and I see the value is set in the variable. I've also printed the variable and the value is there.
I can't understand what I'm doing wrong. What is the correct way to set a variable in bash and use it on another non-bash step?
I've tried hardcoding the value and also passing it as a parameter from the library, and that works, but that is not what I want.
Test the same script to get the token and pass it to Azure Static web APP task, I can reproduce the same issue.
The root cause of this issue is that when you set the variable in Azure CLI task with the command, the pipeline variable will contain "". For example: "tokenvalue".
The expected deployment token in Azure Static web APP task, it will not contain double quotes.
To solve this issue, you need to add a step in Azure CLI task to remove the double quotes.
Here is an example:
steps:
- task: AzureCLI#2
displayName: 'Azure CLI '
inputs:
azureSubscription: xx
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
output=$(az staticwebapp secrets list --name kevin0824 | jq .properties.apiKey)
var2=`sed -e 's/^"//' -e 's/"$//' <<<"$output"`
echo $var2
echo "##vso[task.setvariable variable=staticWebAppDeploymentToken; isOutput=true]$var2"
- task: AzureStaticWebApp#0
displayName: 'Static Web App: '
inputs:
app_location: /
api_location: api
skip_app_build: false
skip_api_build: false
is_static_export: false
verbose: false
azure_static_web_apps_api_token: '$(staticWebAppDeploymentToken)'

Use variable from AWSCLI in azure pipelines for script

I have a build process where I need to use a token, received through the AWSCLI. So far I have connected aws to my azure pipelines but I am having trouble setting up my yaml.
I want to fetch the relevant token to use it later as a variable in my script.
As you can see in my yaml I am running a powershell script with codeartifact and I am saving the value to my myOutputVar. The powershell script does not throw an error.
However, later when I run the building script that variable is not present resulting in ECHO is off.
How can I ensure the value received in the task can be used later in the script/build part?
trigger:
- azure-pipelines
pool:
vmImage: windows-latest
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- task: AWSPowerShellModuleScript#1
inputs:
awsCredentials: 'AWS Connection'
regionName: 'eu-central-1'
scriptType: 'inline'
inlineScript: '##vso[task.setvariable variable=myOutputVar;]aws codeartifact get-authorization-token --domain somedomain --domain-owner 444.... --query authorizationToken --output text; '
- script: |
echo %myOutputVar%
npm ci
npm run build
displayName: 'npm install and build'
Your inline script can be multiple lines, and since this is PowerShell you can do something like:
inlineScript: |
$authToken = aws codeartifact get-authorization-token `
--domain somedomain `
--domain-owner 444.... `
--query authorizationToken `
--output text
Write-Host "##vso[task.setvariable variable=myOutputVar;]$authToken"

How to replace AZ File copy task with Azure Cli task

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)"

How to use output variable value from one powershell script to another powershell script

Hi All I'm working on ymal release pipeline where I have two PowerShell script.
test1.ps1
test2.ps1
step 1)
In test1.ps1 I'm setting a output variable using :
$link="google.com"
Write-Host "##vso[task.setvariable variable=packageurl]$link"
step 2)
Now I want to use this pacakgeurl variable value in test2.ps1 script.
Ymal code will look like :
- task: AzurePowerShell#3
displayName: 'Query Latest Package'
inputs:
azureSubscription: 'test1'
ScriptType: FilePath
ScriptPath: 'source\test1.ps1'
- task: PowerShell#2
displayName: 'Download package'
inputs:
targetType: filePath
filePath: 'source\test2.ps1'
So basically I have to use the output variable value from 1 task to 2 task via PowerShell script.
I also tried to follow this link : https://developercommunity.visualstudio.com/content/problem/676342/how-to-use-output-variable-from-a-powershell-scrip.html
Can anyone help me on this ..?
Thanks in advance.
This works as you expect:
trigger: none
pr: none
pool:
vmImage: 'windows-latest'
steps:
- task: AzurePowerShell#3
displayName: 'Query Latest Package'
inputs:
azureSubscription: 'rg-the-code-manual'
ScriptType: FilePath
ScriptPath: 'stackoverflow\94-variables\script-1.ps1'
azurePowerShellVersion: LatestVersion
- task: PowerShell#2
displayName: 'Download package'
inputs:
targetType: filePath
filePath: 'stackoverflow\94-variables\script-2.ps1'
Yaml file is as you have it.
script-1.ps1 sets variable:
$link="google.com"
Write-Host "##vso[task.setvariable variable=packageurl]$link"
And script-2.ps1 uses that variable:
Write-Host $env:PACKAGEURL