Search code examples
azure-devopsazure-pipelinesdevopsazure-cliazure-pipelines-tasks

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!

Folder Structure of Project

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:

No such file or directory 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"

No such file or directory in relative path

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:

No such file or directory

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.


Solution

  • 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.

    enter image description here

    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"