bashazure-devopsazure-pipelinesazure-pipelines-tasks

How to run a bash script with arguments in azure devops pipeline?


I've a script which works locally fine and while running the script i'm passing 2 arguments in the script and its work perfectly fine, Here's how i'm running the bash script locally:

       ./changeDB_connection.sh "fdevtestcuskv04" "test"

But, I want to do it through azure devops pipeline and for that I've a pipeline task in which I'm calling a bash script with script arguments but it failed with this error message: ##[error]Bash exited with code '1'

Here's the pipeline task:

  - task: Bash@3
    displayName: 'Update Mongo Connection String'   
    condition: and(succeeded(), eq('${{ parameters.UpdateDBstr }}', 'true'))   
    inputs:
      azureSubscription: $(azureSubscription)
      workingDirectory: '$(System.DefaultWorkingDirectory)/Terraform/templates'
      targetType: 'filePath'
      failOnStderr: true
      filePath: "$(System.DefaultWorkingDirectory)/Terraform/Terraform-Scripts/changeDB_connection.sh"
      ScriptArguments: '-keyVaultName $(kvname) -Stringintials $(strinitial)'

let me know what am i doing wrong.


Solution

  • Bowman's answer regarding passing arguments to the script works, but can become troublesome if many arguments should be passed.

    I would instead recommend passing arguments as environment variables to the script and then consuming the same variables in the script. This would require some rewriting of your script.

    The documentation for the bash task specifies how to pass environment variables to the script-execution. In short, just add them under env in your task definition.

    Example:

    steps:
    - task: Bash@3
      inputs:
        targetType: 'filpath'
        filepath: 'my/file/path'
      env:
        MYFIRSTVARIABLE: 'some text'
        MYSECONDVARIABLE: $(aPipelineVariable)
    

    Consume the envrionment variables in the bash script by referencing $MYFIRSTVARIABLE and $MYSECONDVARIABLE respectively. If the variables are secrets, you should keep them in variable groups which the pipeline consumes.