how Access Variable in Azure script btw Powershell in Dev Ops Pipeline - azure-devops

In my yaml file I defined some variables which should contain the value of the month ($MinorVersion) or year ($MajorVersion), by setting the value to '$(Month)' and/or '$(Year:yy)'. When I print them in the task script or task powershell it only displays '$(Month)' and/or '$(Year:yy)' . The expected output of the variable $(MajorVersion) is of course the current month and year 09 and 20.
Can someone tell me how I can access the variable $MinorVersion in the task script / powershell to get the actual value of it? That's the files content:
trigger:
- master
pool:
vmImage: 'windows-latest'
#import variable group resize-group, local variables are name/value pairs
variables:
- group: resize-group
- name: buildConfiguration
value: 'Release'
- name: appxPackageDir
value: '$(build.artifactStagingDirectory)\AppxPackages\\'
- name: MajorVersion
value: '$(Year:yy)'
- name: MinorVersion
value: '$(Month)'
- name: PatchVersion
value: 45
- name: RevVersion
value: '$(BuildID)'
- name: Packageappxmanifest
value: Package.appxmanifest
- name: PackageVersion
value: '$(MajorVersion).$(MinorVersion).$(PatchVersion).$(RevVersion)'
name: $(BuildDefinitionName)_$(MajorVersion).$(MinorVersion).$(PatchVersion)$(RevVersion)
steps:
- checkout: self
submodules: true
- script: |
echo %MinorVersion%
echo %variables.MinorVersion%
echo $($[variables.MinorVersion])
echo ${{ variables.MinorVersion }} # outputs initialValue
echo $(Month)
echo $(MajorVersion)
echo $(MinorVersion)
echo $(RevVersion)
echo $(PackageVersion)
echo $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)
- powershell: |
Write-Host $env:MinorVersion
Write-Host $env:variables.MinorVersion%
Write-Host "$env:MY_MAPPED_PACKAGEVERSION"
Write-Host "$env:MY_MAPPED_REVVERSION"
Output only displays the unevaluated content of the variables:

Can someone tell me how I can access the variable $MinorVersion in the
task script / powershell to get the actual value of it?
Az devops doesn't have such System variables or Predefined variables like $(Year:yy) and $(Month). The available predefined variables are defined here, we can't expand $(Year:yy) and $(Month) cause they're not predefined. As for value: '$(BuildID)', the correct format of BuildID is $(Build.BuildID) instead of $(BuildID).
The $(Date:yyyyMMdd) and $(Rev:.r) are something that only valid for Build Number(name for yaml pipeline), related document here. So normal variables can't expand the value of them, only the name element can recognize them. You should use:
name: $(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)
Instead of wrong format:
name: $(BuildDefinitionName)_$(MajorVersion).$(MinorVersion).xxx
Workaround to get/define variables with value of Date time:
Though the variables of Date time are not predefined, we can put one PowerShell step at the start of steps to define those variables manually. You can check link1 and link2 for more details.
So if you want to define data-related variables, you can check set variables in scripts:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
$year=$(Get-Date -Format 'yyyy')
Write-Host "##vso[task.setvariable variable=MajorVersion]$year"
$month=$(Get-Date -Format 'MM')
Write-Host "##vso[task.setvariable variable=MinorVersion]$month"
$day=$(Get-Date -Format 'dd')
Write-Host "##vso[task.setvariable variable=DayOfMonth]$day"
- powershell: |
Write-Host $(MajorVersion)
Write-Host $(MinorVersion)
Write-Host $(DayOfMonth)
Output of second PS task:

Related

Azure DevOps YAML: compare two variables

To determine which parameter value (my_param) I want to pass to a template (my-template.yml), I would like to compare two variables (var1 and var2) in my YAML pipeline.
- template: my-template.yml
parameters:
${{ if eq(variables.var1, variables.var2) }}:
my_param: 'abc'
${{ else }}:
my_param: 'xyz'
I tested this with different values for var1 and var2 (defined in a DevOps variable group/library). No matter what I put as variable values (whether or not they are equal), the "else" case is always triggered even if both variables have the same value. What am I missing?
Based on my test, there is no direct way to compare two variables in library->variable group.
So I suggest you set another variable 'compare' to previously compare the values of the two variables in the variable group.
Example:
In variablegroup 'MyVarGroup', I have var1 'SomeNumber' and var2 'samplevariable'. I now set var3 'compare', if var1=var2, I set var3 value '0', otherwise other different number.
You could also set it as true if the two variables are the same and false if they are different.
Here are my sample:
mytemplate.yml:
parameters:
name: my_param
type: string
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: echo "${{ parameters.my_param }}"][1]][1]
azure-pipelines.yml:
trigger:
- master
variables:
- group: MyVarGroup
- name: para
value: $[replace(replace('True',eq(variables['compare'], '0'), 'abc'),'True','xyz')]
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
echo "$(SomeNumber)"
echo "$(samplevariable)"
echo "$(compare)"
echo "$(para)"
- template: mytemplate.yml
parameters:
my_param: $(para)
If var3 compare is '0',then I will get 'abc' in the template output.
[![enter image description here][1]][1]
Otherwise, I will get 'xyz'
[![enter image description here][2]][2]
I hope it could do some help.
[1]: https://i.stack.imgur.com/1uYU6.png
[2]: https://i.stack.imgur.com/yeRy4.png

Find a string which has leading space and add line exactly below to add line

I have a file like below.
# Start OF Java_Out_Of_Memory
- displayName: "Java_Out_Of_Memory"
logDirectory: "/opt/xyz"
logName: "TextLog_*"
searchStrings:
- displayName: "Out_Of_Memory"
pattern: "java.lang.OutOfMemoryError"
matchExactString: false
caseSensitive: false
# End OF Java_Out_Of_Memory
I wanted to add line exactly below of caseSensitive: false with printMatchesString: false...
# Start OF Java_Out_Of_Memory
- displayName: "Java_Out_Of_Memory"
logDirectory: "/opt/xyz"
logName: "TextLog_*"
searchStrings:
#displayName Should be unique across the patterns including the case.
- displayName: "Out_Of_Memory"
pattern: "java.lang.OutOfMemoryError"
matchExactString: false
caseSensitive: false
printMatchedString: false
# End OF Java_Out_Of_Memory
I don't know how many lead space of matched string... Leading space may vary depends on file to file.
I tried this
sed -i '/^[^#]*caseSensitive:*/a \\ printMatchedString: false'
So my requirement is to add a line exactly below and even consider lead space
NOTE:- I have multi configuration like above and I need add PrinitMatched string on every configuration
Try this:
sed -E '/^[^#]*caseSensitive:/ {p; s/^([[:blank:]]*)[^[:blank:]].*$/\1printMatchesString: false/;}' myfile.txt
Explanation:
/^[^#]*caseSensitive:/ - on lines matching "caseSensitive:" (not preceded by "#"
{ - do the following block of commands
p - print the current line (in its original form)
s/^([[:blank:]]*)[^[:blank:]].*$/\1printMatchesString: false/ - parse the line as a bunch of blanks (in parens, so it's a capture group) and then whatever's left, and replace it with the blanks (\1 recalls the capture group) and "printMatchesString: false". Note that the result will be printed automatically.
} - marks the end of the group of commands

Azure DevOps pipelines : backslashes in parameters - do they need to be escaped and how?

I'm using a pipeline template and have a few string runtime parameters that will be file systems paths and they contain backslashes, I was echoing them out to test the template pipeline and have tried all possible approaches
no quotes
single quotes
double quotes escaping backslashes with another backslash
Echoing out all these different strings never displays backslashes and it doesn't seem to me to be a log display issue (accessing the raw log I never see a single backslash however I pass the paths).
This is my simple pipeline template to test what I'm doing
parameters:
- name: string1
type: string
default: C:\APPS\XYZ\
- name: string2
type: string
default: 'C:\APPS\XYZ\'
- name: string3
type: string
default: "C:\\APPS\\XYZ\\"
jobs:
- job: JOB
displayName: JOB
steps:
- checkout: none
- script: |
echo 1 ${{ parameters.string1 }}
echo 2 ${{ parameters.string2 }}
echo 3 ${{ parameters.string3 }}
In the end the disappearance of the backslashes was only in echoing out values. I don't fully understand why this is happening but clearly the backslash is a special character and gets interpreted. By putting the strings to be printed inside single quotes this does not occur and the parameter values get printed out correctly.
This is the correct code
parameters:
- name: string1
type: string
# no quotes
default: C:\APPS\XYZ\
- name: string2
type: string
# single quotes
default: 'C:\APPS\XYZ\'
- name: string3
type: string
# double quotes
default: "C:\\APPS\\XYZ\\"
steps:
- checkout: none
- script: |
echo '1 ${{ parameters.string1 }}'
echo '2 ${{ parameters.string2 }}'
echo '3 ${{ parameters.string3 }}'
First of all, you have a mistake in the second value - the single quote at the start is missing.
Second, you don't need quotes in paramaters value. If you set double quotes the rules for text are changed.
http://blogs.perl.org/users/tinita/2018/03/strings-in-yaml---to-quote-or-not-to-quote.html

Bamboo variable doesn't accept spaces?

I have a PowerShell script that I trigger from Bamboo, the script basically accepts 3 string parameters:
Param([string]$language, [string]$architecture ="x64", [string]$version)
$x64Path = "some path..."
$x86Path = "some path.."
$arguments = $language,$architecture,$version
If($architecture -eq "x64") {
Write-Host "Executing:" $x64Path" at x64 using" $language "and" $version
# & $x64Path $arguments
}
Else {
Write-Host "Executing:" $x86Path "at x86 using" $language "and" $version
# & $x86Path $arguments
}
if I run the PowerShell script from the ISE, for example:
.\MyScript.ps1 "JPN" "x64" "AB 6.0 R"
I will correctly get this output:
Executing: some path... at x64 using JPN and AB 6.0 R
I have created a plan in Bamboo with the following:
Variables
Task
I have tried adding the variables (argument) as:
"${bamboo.Language}" "${bamboo.Architecture}" ${bamboo.Version}
and aso as:
"${bamboo.Language}" "${bamboo.Architecture}" "${bamboo.Version}"
But I always get the same output:
Executing: some path... at x64 using JPN and AB
Notice that the last variable is cut out (I don't get the full AB 6.0 R)
Is this a bug in Bamboo, or something I'm doing wrong? or is there a workaround it?
PS. To achieve this run in Bamboo (by giving the variable values), I'm running the plan as follows:
I suspect the quotes get stripped after variable replacement in bamboo (ie. before the arguments are passed to powershell).
Add an inner set of single-quotes to the argument that has spaces:
"${bamboo.Language}" "${bamboo.Architecture}" "'${bamboo.Version}'"

Ansible time Conversion

I am comparing the last access time of a file with a file_date variable.
Vars:
file_date: '2013-12-12 14:35:02'
Playbook:
- name: set fact
set_fact:
time: "{{ (file_date|to_datetime).strftime('%s') }}"
Output shows as below:
ok: <server> {
"ansible_facts": {
"time": "1386876902"
},
"changed": false
}
But the last access time from win_stat output is
"lastwritetime": 1386858902.
There is slight difference because of the timezone. I need to subtract some value from time or change the timezone. Kindly advice how this can be done.
I would save that lastwritetime from win_stat to a register, and then use set_fact to perform the math operation on it.
- set_fact:
math_time: "{{ register_name.lastwritetime - 18000 }}"
Based on your output this should work, seeing as there are no quotes around the number. But if its getting returned as a string, just use a jinja filter to recast it as an integer and it should still work.
- set_fact:
math_time: "{{ register_name.lastwritetime|int - 18000 }}"