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.
CodePudding user response:
The below is a sample, and it works fine on my side.
xxx.sh
#pass arguments to the script
echo "Argument 1 is $1"
echo "Argument 2 is $2"
echo "Argument 3 is $3"
RunBashScript.yml
trigger:
- none
pool:
vmImage: ubuntu-latest
steps:
- task: Bash@3
inputs:
filePath: 'xxx.sh'
arguments: '1 2 3'
Successfully on my side:
Structure on my side:
CodePudding user response:
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.