My workflow is using a windows runner.
The workflow runs an aws cli command to get instance IDs. I then try to set the returned IDs as a github environment variable to use in future steps
Here is the snippet from my workflow file:
- name: Get IDs
id: get_instances
run: |
$getinfo = aws ec2 describe-instances --filters 'Name=tag:Name,Values=project-bell' --output text --query 'Reservations[*].Instances[*].InstanceId' --region "eu-west-1"
$instances = ($getinfo -Join " ")
echo $instances
echo "INSTANCE_ID=$instances" >> $GITHUB_ENV
- name: Print IDs
run: |
echo {{ env.INSTANCE_ID }}
echo "{{ env.INSTANCE_ID }}"
So the 1st line in my run command of the "Get IDs" step returns the IDs as an array. So $getinfo looks like this:
instanceID 1
instanceID 2
2nd line I use -Join command to get the IDs to be returned in a single line: instanceID 1 instanceID 2
I then try and set this single line as a github environment variable but it isn's working and not sure why.
When i echo $instances i can see that the -Join command has worked and i see the IDs printed as a single line instanceID 1 instanceID 2.
But on the "Print IDs" step {{ env.INSTANCE_ID }} is returned rather than the instanceID 1 instanceID 2 which i tried to set as a github environment variable in the previous step.
Notes:
I have to 2 echos on the "Print IDs" job as i was just testing if i needed quotations for it to work"
I have the -join command because when i tried to set $getinfo (1st run command) as a github env var i got an error saying it was an array
Question
Am I writing the command wrong to set github env?
Or is it something to do with the values themselves - like does it need to be a string?
Related
I don't have much experience with PowerShell and this simple issue has been driving me up the wall. I'm hoping someone can point me in the right direction.
I have a CSV-file with IP-range values
I wish to pass these IP values as a parameter to a Bicep template
The parameter is of type array, see code snippets below
CSV-file:
IP,Comment
10.0.0.1, Comment blabla
10.0.0.52, Comment more blabla
I wish to pass the IP-values into a Azure Bicep template with the following parameter:
param ipArray array
The cli command is as follows:
az deployment group validate -g test-rg -f .\main.bicep -p ipArray=$ipRange
I am unable to populate $ipRange properly. I have tested the following and know it works:
az deployment group validate -g test-rg -f .\main.bicep -p ipArray="['10.0.0.1','10.0.0.52']"
So I need to figure out how to build my Powershell variable according to above syntax
$ipRange = ((Get-Content .\ip_list.csv) | ConvertFrom-Csv).IP
Failed to parse string as JSON:
10.0.0.1 10.0.0.52
Error detail: Extra data: line 1 column 6 (char 5)
Any nudge in the right direction will be greatly appreciated
Thanks!
This code will convert the ip range as you asked for:
$ipRange = ((Get-Content C:\Temp\ip.csv) | ConvertFrom-Csv).IP | ConvertTo-Json
$ipRange = $ipRange.ToString() -replace '"',"'"
$ipRange
one final thing, in your param, it is mentioned as vlkIpArray and in deployment it is mentioned as ipArray. is this a typo error?
param vlkIpArray array and
az deployment group validate -g test-rg -f .\main.bicep -p ipArray="['10.0.0.1','10.0.0.52']"
I am running
$nameLine= az boards iteration team list --team "DevOpsTesting Team" --timeframe current --project DevOpsTesting | findstr "name"
$nameOnly = (select-string ":(.*)" -inputobject $name).Matches.Groups[1].Value.Replace(",","")
az boards work-item create --title Test --type Task --project DevOpsTesting --iteration DevOpsTesting\\$nameOnly --assigned-to A.B#gmail.com --fields "Description=PR is open, please test the new exe version and approve Priority=2"
But because the value of nameOnly is "Sprint 7" I am getting an error
az : ERROR: unrecognized arguments: 7
At line:4 char:1
..?
thx.
If spaces are used within a iteration, then enclose in double-quotes, in your case you could add the doule-quotes like:
--iteration "DevOpsTesting\\$nameOnly"
I need to change the environment variable under container definition in the ecs Task Definition.
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition $TASKDEFINITION_ARN --output json)
echo $TASK_DEFINITION | jq '.taskDefinition.containerDefinitions[0] | ( .environment[] |= if
.name == "ES_PORT_9200_TCP_ADDR" then .value = "vpc-kkslke-shared-3-abcdkalssdfy.us-east-1.es.amazonaws.com"
else . end)' | jq -s . >container-definition.json
CONTAINER_DEF=$(<$container-definition.json)
aws ecs register-task-definition --family $FAMILY_NAME --container-definitions $CONTAINER_DEF
Error Message:
Error parsing parameter '--container-definitions': Invalid JSON:
Expecting property name enclosed in double quotes: line 1 column 2
(char 1) JSON received: {
One observation not sure if it is related to a bug in VScode or not . When I try to use the view the variable value in debug mode. I only get partial text as seen below. But when I do echo for the same variable I do see the full json. Not sure if the whole value is being passed to the container definition.
I have a requirement which is as follows:
Variable Group A, has 7 set of key=value pairs
Variable Group B, has 7 set of key=value pairs.
In both cases keys are the same, values are only different.
I am asking from the user, the value of be injected in variable group B, user provides me the variable group A name.
Code snippet to perform such update is as below:
export reference_env="Variable Group A"
export target_env="Variable Group B"
values=(addressSearchBaseUrl addressSearchSubscriptionKey cacheUrl calendarApiUrl checkoutBffApiUrl cpCode)
groupId=$(az pipelines variable-group list --group-name "${reference_env}" | jq -r '. [].id')
#grab groupid for further checks later on.
az_create_options=()
for ptr in "${values[#]}"
do
result=$(
az pipelines variable-group list --group-name "${Variable Group ${reference_env}" | jq '.[0].variables.'${ptr}'.value'
)
printf "%s\t%s\t%d\n" "$ptr" "$result" $?
if [[ "$ptr" = "calendarApiUrl" ]]
then
echo "INF: Updating $ptr with new value"
result="https://noname-api.platform.test.com.au/marketing/calendar/v1/AvailableDates/market1/"
insertString=";rev=${target_env}"
lookingfor="v1"
result=$(echo $result| sed s/"${lookingfor}"/"${insertString}"/g)
echo $result
fi
az_create_options+=("$ptr"="$result" )
done
declare -p az_create_options
az pipelines variable-group variable update --group-id 1202 --name "${target_env}" "${az_create_options[#]}"
However, when I run this, I get error as below:
Note: I have masked the values in light of IP but error is the same.
ERROR: unrecognized arguments: addressSearchBaseUrl="https://xxxxxxxxxx" addressSearchSubscriptionKey="yyyyyyyyyyyyyyy" cacheUrl="qqqqqqqqqqqqq" calendarApiUrl=tttttttttttttttt checkoutBffApiUrl="sssssssssssssssss" cpCode="333333"
Can someone guide me please?
You wrongly used update command:
az pipelines variable-group variable update --group-id
--name
[--detect {false, true}]
[--new-name]
[--org]
[--project]
[--prompt-value {false, true}]
[--secret {false, true}]
[--subscription]
[--value]
name is a vairable name. So if you want to update each variable you need to update it one by one.
I have my release pipeline Variables tab set like:
I would like to access my123 variable in task's display name by concatenating initialVariable's result.
Outputs
I have tried so far referencing only initialVariable and it returned proper value in Job's display name.
But when I try to create my123 value by using initialVariable(=123), I am not getting proper value (was hoping that $(initialVariable) would convert to 123 and $(my123) would get proper "finalValue").
Azure DevOps: Getting variable value by concatenating other variables'value as task input
This is a known issue. Because the value of nested variables (like $(my$(initialVariable)) are not yet supported in the build/release pipelines.
Check my other thread for some details.
The workaround is add a Run Inline Powershell task to set the variable based on the input pipeline variables, just like Josh answered.
For you case, I test it by following Powershell scripts:
if ($(initialVariable)-eq "123")
{
Write-Host "##vso[task.setvariable variable=my123]finalvalue"
}
else
{
Write-Host "##vso[task.setvariable variable=my123]otherValue"
}
Then we could get the variable my123 based on the value of variable initialVariable in following task, I add command line task to display the value:
In the result, the value in the command line task is correct finalvalue. But the display name is still $(my123):
Important:
That is also the question in your comment. This behavior is expected. That because the variable in the display name is just to get the predefined value. It's static acquisition, not dynamic. The variable my123 is assigned when running powershell. The static variable my123 in the display name does not go in to the environment where the powershell code is running.
So, the variable my123 in the title could not get the value in the task powershell. But other task could use it very well.
Hope this answer clean your puzzle.
It's ugly, but...
Like I mentioned in my comment, I don't think you're going to get this to work in the UI by default.
Luckily you can use PowerShell to hack this together if your REALLY need the ability to address a variable name based on the value of another variable.
All the variables (secrets are handled a little differently) in your build or release pipeline definition are made available to your powershell script FILE (not inline) via environment variables (ie. $env:initialVariable).
Suppose your situation is thus:
selector = selectable1 //this is the value that can change
selectable1 = theFirstSelection
selectable2 = theSecondSelection
selectable3 = theThirdSelection
In this case (assuming I understand your request) you want to be able to change the value of the selector and force tasks to access the appropriate selectable variable.
So...
Define a new variable in your pipeline.
selector = selectable1 //this is the value that can change
selected = "" //this is the variable you use in your tasks
selectable1 = theFirstSelection
selectable2 = theSecondSelection
selectable3 = theThirdSelection
Write a VariableSelection.ps1 script. This powershell script will be what you need to run to assign the value of $(selected) before it gets used.
# VariableSelection.ps1
Write-Host "select variable: $env:selector"
$selectedValue = (gci env:"$env:selector").value
Write-Host "##vso[task.setvariable variable=selected]$selectedValue"
Note: it is my observation that if you write this script inline, it will not work b/c the environment variable functionality is different for scripts run from a file.
Given the value of $(selector) is selectable2, when the script is run, then the value of the $(selected) will be theSecondSelection.
Example in a Pipeline
Powershell
YAML
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
pool:
name: Hosted VS2017
variables:
- name: "selector"
value: "var1"
- name: "selected"
value: ""
- name: "var1"
value: "var1_value"
- name: "var2"
value: "var2_value"
steps:
- task: PowerShell#2
inputs:
filePath: '$(build.sourcesdirectory)/varSelector.ps1'
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Write-Host "env:selected: $env:selected"
Write-Host "selected: $(selected)"
Results