azure-devopsazure-pipelinesazure-pipelines-build-taskazure-task-groups

Azure DevOps creating variables for actions in inline bash scripts


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?


Solution

  • 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