5

When creating an inline bash command like this in Azure DevOps:

checksum="$(cksum file.txt)"

I'll wind up seeing cksum file.txt as a required parameter. For whatever reason, this behavior isn't consistent so sometimes I've setup build pipelines that work fine with inline Bash scripts, but inevitably, I'll run into this issue and be unable to fix it.

I even tried setting the cksum file.txt parameter to cksum file.txt, but replaces the space with an encoded string: %20. It becomes cksum%20file.txt which isn't a valid command in bash.

Here's the full script:

yarnCacheFilename="$(cksum yarn.lock).yarnCache.docker.tgz"

wget "https://example.azureedge.net/yarn-cache/$yarnCacheFilename"

if [ -f "$yarnCacheFilename" ]; then
    mkdir node_modules
    tar -xzvf "$yarnCacheFilename"
else
    yarn install --production
fi

Simple enough. That's code I can run in any bash terminal. Sadly, Azure DevOps is adding a parameter to the Task Group:

Azure DevOps adding parameter for inline <code>bash</code> command

The fact that this parameter exists means Azure DevOps is preventing my bash file from executing properly and string-replacing the most-crucial part.

How do I work around this issue?

Kevin Ghadyani
  • 6,829
  • 6
  • 44
  • 62

2 Answers2

8

You can use command substitution so replace the $() with a back tick ` at both sides.

https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html for more information about command substitution.

checksum="`cksum file.txt`"

Full Script Example

yarnCacheFilename="`cksum yarn.lock`.yarnCache.docker.tgz"

wget "https://example.azureedge.net/yarn-cache/$yarnCacheFilename"

if [ -f "$yarnCacheFilename" ]; then
    mkdir node_modules
    tar -xzvf "$yarnCacheFilename"
else
    yarn install --production
fi
Jdsmith.It
  • 160
  • 1
  • 8
  • Can you provide an example using the code from the question? – Kevin Ghadyani Mar 21 '19 at 17:52
  • Sure I have added examples @Sawtaytoes. If it helps you out it would be great if you could mark it as the answer to help others. – Jdsmith.It May 13 '19 at 08:44
  • when I created this question, I was working for a different company. I can no longer verify any fixes for Azure DevOps without creating a new personal project and trying it out. I'm going to assume this fixes the issue. – Kevin Ghadyani May 13 '19 at 16:35
1

If you mean you want to pass the string "cksum file.txt" as a parameter in inline bash script, then you can set a variable and use the variable in subsequent tasks/scripts.

For example:

  1. Set the checksum variable:

    echo "##vso[task.setvariable variable=checksum]cksum file.txt"
    
  2. Read the variables

    echo "No problem reading $CHECKSUM"
    

Please see Define and modify your variables in a script for details.

If you run a script specified with a path something like $/project/script/Test0427/bash.sh, then you can directly pass the string as a parameter:

  1. Arguments:

    "cksum file.txt" "Test Arguments 1018"

  2. Read the variables

    echo "No problem reading $1 and $2"
    

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • My issue is with the inline option under Type. There's no "Arguments" area. Also, it's different when you pass in `$()` because it allows you to put CLI commands in there like `$(ls -lah | grep time)`. This is a command, not a replacement string. Bash uses `$(cksum file.txt)` naively, but Azure DevOps is parsing those as url-encoded replacement parameters instead of CLI commands. – Kevin Ghadyani Oct 18 '18 at 10:24
  • I apologize for being so confusing. I added more information to my original question so give you a better idea of what's happening. I believe it's a bug in Azure DevOps. The bug's been there for months now; although, this is the first time I didn't want to work-around it by copy-pasting the same 4 `.sh` scripts into 15 repos. Any time a change is needed, I used to have to replicate that change in every repo. My goal is to have them in one place: the build pipeline. – Kevin Ghadyani Oct 18 '18 at 10:33
  • @Sawtaytoes Well, I suggest you submitting an issue on [Developer Community site](https://developercommunity.visualstudio.com/spaces/21/index.html). – Andy Li-MSFT Oct 19 '18 at 09:27
  • 1
    https://developercommunity.visualstudio.com/content/problem/362083/inline-bash-script-task-doesnt-let-you-write-shell.html Thanks! – Kevin Ghadyani Oct 19 '18 at 14:40