Azure DevOps Yaml: Gaining secret variable out of Azure KeyVault Task from Variable - azure

I'm trying to obtain a secret out of my KeyVault.
The variable name is secretVar.
Obtaining the secret like this: $(secretVar) works fine however I would like to retrieve it from a variable like this:
I keep getting command not found and I've no idea why this shouldn't be working.
So the name of the secret I want to extract is inside a bash variable. For this question I've simplified the problem but in my real use case I have a bash for loop which loops through secret names and inside the for loop I want to extract the appropriate value from the KeyVault with the corresponding secret name like this:
for secretname in secrets; do
echo $($secretname) # This should contain the value of the secret but gives command not found
done
If anyone has an idea what could be happening, any help is very appreciated.
Thanks in Advance!

Look at the syntax you're using.
variable=secretVar
You are creating an environment variable with the literal value secretVar
Then you try to execute the value of the variable $variable with $($variable). So it tries to run the command secretVar, which obviously doesn't exist, and you get an error message.
The syntax you're looking for is
variable=$(secretVar)
just like you used in the first echo command in the script.
If you don't want to run the variable value as a command, the syntax would be $variable, not $($variable)
$variable is the syntax for a Bash environment variable.
$(variable) is the syntax for referencing Azure DevOps variables.

First of all, the script keyword is a shortcut for the command-line task. The task runs a script using cmd.exe on Windows and Bash on other platforms. You need to pay attention to the agent you are using.
If you want to set variables in scripts, you can use task.setvariable logging command. For example:
- script: |
echo $(secretvar)
echo "##vso[task.setvariable variable=variable]$(secretvar)"
- script: |
echo $(variable)
You can find more detailed information in this document.

Related

How to concatenate two variables from azure variable group and assign them to one with single quotes

I'm having 2 Variables inside a variable group and their values are as shown below:
cr 2200
tr cd1200
I would like to assign these two variables to a third variable cd as shown below:
cd '2200;cd1200;abc.txt'
I'm trying to use below script but it is showing too many arguments.
Can someone please help in this.
I'm using below code:
cd = \'"$cr";"$tr";abc.txt\'
echo "$cd"
I need output as:
cd = '2200;cd1200;abc.txt'
Using Azure macro syntax ($(<varName>)):
$cd = '$(cr);$(tr);abc.txt'
Note:
PowerShell always needs the $ sigil when accessing a variable - even when assigning to it (unlike in POSIX-compatible shells such as Bash).
Azure's macro syntax - which textually expands references to Azure variables up front, before PowerShell sees the code, is not be confused with PowerShell's subexpression operator ($(...))

Assign Azure Powershell variable to DevOps Pipeline variable

How can I assigned the $NewIP variable precalculated in this step to a DevOps pipeline variable called $pipeline_ip?
You should use logging command if you want to assign powershell variable to Azure DevOps variable
echo "##vso[task.setvariable variable=pipeline_ip;]$NewIP"
Update after clarification:
If you use syntax like:
$NewIP = $(pipeline_ip)
Then $(pipeline_ip) would be replaced with the value before script will be executed.
And if you use syntax like
$NewIP = $env:PIPELINE_IP
then you will refer to environment variable and since all DevOps variables are mapped (except secret variables - here you need to express this excplicitly) it would also work.
However, these are two ways of doing that.
You can use two methods:
$NewIP = $(pipeline_ip) Macro syntax variables
$NewIP = $env:PIPELINE_IP Set variables in pipeline

Get current branch name

I'm running a script (bitbucket_pipelines.yml) and on one of the steps I need to know the current branch name, How can I get it?
I saw there is a predefined BITBUCKET_BRANCH variable, but I'm having troubles to print it so I can see its content.
I tried to do:
...
step:
script:
- echo $BITBUCKET_BRANCH
but when pipelines runs all I see is
echo $BITBUCKET_BRANCH
How can I really see the content of this variable?
I found that Bb Pipelines are sometimes picky when dealing with variables. Try changing this to echo "$BITBUCKET_BRANCH". Also, enclosing the whole line in single quotes might help.
#Shvalb, the question should be how to display the value of a variable in bitbucket pipeline.
I deal with bitbucket support on this matter before.
I want to echo a repo/pipeline variable to see the value and it is not showing correctly.
In my case, it was my repo variable conflict with my deployment/pipeline variable. However, from the support, I understand bitbucket is using search and replace the screen value to "hide" the actual value of the variable with direct echo.
in order to see the value, you can use
echo $VAR > /tmpfile
cat /tmpfile
It was the trick I used before but I am not sure whether it will still work.

Azure DevOps - pipeline variables - special char issue $$

I am using DevOps pipeline to build and deploy to different environments
For one environment I am encountering this issue where i am using a Pipeline Variable with $$ in the value
For Example:
Password pipeline variable with value = $omeCla$$Password
When i deploy it fails and when i check the logs the password is displayed as $omeCla$Password. So basically when $$ are together it drops one $
For all variable i am using regex __VaraibleValue__ and its working fine
I have tried:
$omeCla$\$Password to try and escape and it displays as $omeCla$\$Password . So basically \ doesn't work.
I tried '$omeCla$$Password' to try and escape and it displays as '$omeCla$Password'
I want to keep this value as a normal pipeline variable before review
So basically how can I escape this?
Or should I add a Secret Token here in the replace token task (see screenshot below)? and then make the pipeline variable secret? If so, what should I set for Secret Token? Also, in app.config in my repo what should I use instead of the regex __VariableName__ that I use for normal variables?
The solution was to use 4 $. So if you have $$ together you need to add $$$$
Example: $someCla$$$$Password
#JaneMa-MSFT as requested
https://developercommunity.visualstudio.com/content/problem/1296808/azure-pipeline-how-to-escape-special-characters-in.html

How to stop GitLab from evaluating $ inside variable value?

I am passing GitLab variable while running CI/CD pipeline as below.
type - variable
key - password
value - {"a": "abc$def#pqr"}
I am reading it in some GitLab pipeline stage as below.
echo $password
It is showing as below.
{"a":"abc#pqr"}
But I want it to show as below.
{"a":"abc$def#pqr"}
I don't want it to evaluate $def as blank
Note:
I tried with \ escaping and with single quotes too.
I need this to be in json kind of format itself for further use.
Works fine on using double $$ instead of single $

Resources