0

I have found a really terribly annoying feature of Azure DevOps that I was not aware of, maybe it just never caused any problems in the past for me.

When I run a script using a Bash@3 task, even though I'm stating the script path, for some reason AZDO runs the script from the context of some temp folder wo which it copies the script file. Because I also use my script for local compilation and want to be able to call it from anywhere, for sourcing some other file in the same folder as the script I use the scripts actual location. But since the location now seems to be this temp folder, my other file is not there and the script fails.

This is what I do in my pipeline yml:

  - task: Bash@3
    displayName: Build something
    enabled: true
    continueOnError: false
    inputs:
      failOnStderr: true
      filePath: 'CI/build_something.sh'
      arguments: y
      # I tried it with and without stating the working directory, doesn't make a difference
      #workingDirectory: CI

This is what I try to do in my build script:

echo "pwd:"
pwd

script_dir=`dirname $0`
echo "script_dir: ${script_dir}"

# this is the trouble maker that breaks the script
source `dirname $0`/build_vars.list

And this is what I get back from AZDO when running the pipeline task:

pwd:
/opt/agent/build/_work/1/s/CI
script_dir: /opt/agent/build/_work/_temp
/opt/agent/build/_work/1/s/CI/build_something.sh: line 9: /opt/agent/build/_work/_temp/build_vars.list: No such file or directory

I was always under the impression, that only inline scripts are copied as .sh files to a temporary folder, but this is not an inline script. Is there a way to suppress this behavior? I have a hard time wrapping my head around this. Why is it done this way?

Does anybody have an elegant solution for getting around this?

PS: I should mention I'm working on a somewhat old version of AZDO from 2019

DanDan
  • 1,038
  • 4
  • 15
  • 28
  • 1
    `$0` is not a reliable way for a Bash program to get its own location. `$BASH_SOURCE` is a much better option. See [BashFAQ/028 (How do I determine the location of my script? ...)](https://mywiki.wooledge.org/BashFAQ/028). (The topic comes up fairly often on Stack Overflow (e.g. [How can I get the source directory of a Bash script from within the script itself?](https://stackoverflow.com/q/59895/4154375)) but answers almost always contain broken code.) – pjh Mar 10 '23 at 02:15
  • 1
    [Shellcheck](https://www.shellcheck.net/) identifies several problems with the code. The report includes links to more information about the problems and how to fix them. It's a good idea to run [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code. – pjh Mar 10 '23 at 02:23

0 Answers0