7

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?

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

3 Comments

Can you provide an example using the code from the question?
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.
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.
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

4 Comments

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.
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.
@Sawtaytoes Well, I suggest you submitting an issue on Developer Community site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.