Tommy Leong
Tommy Leong

Reputation: 3030

Running bash script within a project folder of AzureDevOps

I have a script that is nicely performing all kinds of dependency installation and some manual works (NPM installation, some manual steps to do while setting up project) to setup a project before it is able to run. The script runs perfectly fine in a local environment.

Im now trying to build my pipeline in Azure DevOps, I realized I can't just fire the script right away. Running npm install inside the script is not actually running within my project folder but it always runs on the path /Users/runner/work

Question: How can I execute the script within my project folder?

Sample code in my script file

set -e

# Setup project dependencies
npm install

# some mandatory manual work
.....

# Pod installation
cd ios 
pod install

My AzurePipelines.yml

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      sh $(System.DefaultWorkingDirectory)/projectFolder/setup.sh
    failOnStderr: true

Issue log from Azure (as you can see, the npm installation is not working due to incorrect path, hence further actions within pipeline will fail)

npm WARN saveError ENOENT: no such file or directory, open '/Users/runner/work/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/Users/runner/work/package.json'
npm WARN work No description
npm WARN work No repository field.
npm WARN work No README data
npm WARN work No license field.

Upvotes: 5

Views: 23860

Answers (1)

Bruno Assis
Bruno Assis

Reputation: 1006

Firstly I'd advise you to split your script into different steps of a single job or multiple jobs with many steps because this makes it easier to parallel them in the future allowing you to speed up the build time.

In order to execute your script directly from the project folder you can leverage the option working directory:

- task: Bash@3
  inputs:
   targetType: 'inline'
   script: |
     ./setup.sh
   failOnStderr: true
   workingDirectory: "$(System.DefaultWorkingDirectory)/projectFolder/"

However in your case you could point directly to the script, without the need to run it as a "script"

   - task: Bash@3
      inputs:
       targetType: 'filePath'
       filePath: "$(System.DefaultWorkingDirectory)/projectFolder/setup.sh"
       failOnStderr: true
       workingDirectory: "$(System.DefaultWorkingDirectory)/projectFolder/"

ref.: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/bash?view=azure-devops

Upvotes: 9

Related Questions