I have searched all the documents about how to set variables to pass the variable during the run time build pipeline and only told me how to set in .yaml. But how to use it in release pipeline runtime variables?
Suppose I have a Generate-manifest.ps1 file which I will run to generate manifest.json file which representing the latest release packages with version number inside it. Version: 1.0.0. This version value should passed as a variable during runtime. I need help to do this.
Manifest.json file looks like this.:
{
"version": "1.0.0",
"timeStamp": "2021-05-07T09:41:34+00:00",
"packages": [
{
"name": "data-service",
"type": "docker-image",
"version": "REL-1.0.5367"
},
{
"name": "feedback-service",
"type": "docker-image",
"version": "REL-1.0.6099"
},
]
}
Based on your explanation I understand that you need to read version variable during the build pipeline and pass this variable on the release pipeline in order to use it on BuildNumber for example.
First you will need to use a powershell task to read the version value from the .json file.
$deployment_config = Get-Content manifest.json -raw | ConvertFrom-Json
$versionNumber = $deployment_config.version
Then you can change the BuildNumber variable on build pipeline
$buildnumber = -join("v",$versionNumber ,"_","$(Build.BuildNumber)")
And also update
Write-Host "##vso[build.updatebuildnumber]$buildnumber"
You can then use the varialbe $(Build.BuildNumber) on release pipeline
Related
Before I do a release of my package and tag it, I would like to update the manifest.json file version Patch value to reflect the new version of the package every time I trigger it.
Json file:
{
"version": "0.0.0",
"timeStamp": "2022-02-14T09:41:34+00:00",
"packages": [
{
"name": "data-service",
"type": "docker-image",
"version": "REL-1.0.5"
},
{
"name": "application-service",
"type": "docker-image",
"version": "REL-1.0.6"
},
]
}
So here I am trying to increment by reading the content of the Json file from Powershell:
version = (Get-Content manifest.json | ConvertFrom-Json).version
Value for the variable "version" in the Json file is 0.0.0
But My question is - how to increase the only patch value? E.g. I need to have 0.0.1 output values after transform automatically in the manifest.json file.
Use the [version] type to parse the existing version, then increment the build number:
$data = Get-Content manifest.json | ConvertFrom-Json
# extract and increment version build number
$existingVersion = $data.version -as [version]
$nextVersion = [version]::new($existingVersion.Major, $existingVersion.Minor, $existingVersion.Build + 1)
# update original data
$data.version = "$nextVersion"
# write back to disk
$data |ConvertTo-Json |Set-Content updated-manifest.json
I would like to use terraform's external data source to identify certain AWS EC2 instances:
data "external" "monitoring_instances" {
program = ["bash", "${path.module}/../bash/tf_datasource_monitoring.sh"]
query = {
env = var.env_stage
}
}
The bash script is using AWS CLI to return a list of instance IDs.
But I keep receiving this Error: command "bash" produced invalid JSON: json: cannot unmarshal array into Go value of type string
I don't understand what the expected syntax of my script's STDOUT would be for terraform to understand the result.
So let's assume the script is supposed to return 3 instance IDs i-1, i-2 and i-3.
What would be the correct JSON syntax to be returned to terraform?
Examples, that do NOT work:
{
"instances": [
"i-1",
"i-2",
"i-3"
]
}
[
"i-1",
"i-2",
"i-3"
]
It is a known issue in Terraform for provider-external: https://github.com/hashicorp/terraform-provider-external/issues/2. It was opened a while ago, unfortunately is still present for latest version (Terraform v1.011).
You may want to avoid returning JSON objects which contain arrays.
I had the same issue, while executing a python script to generate a dynamic bigquery schema, which is per definition an array of JSONs.
I solved it, by implementing a wrapper JSON with the schema as a string value (see dummy code below).
# get_dynamic_bigquery_schema.py
import json
bigquery_schema = [
{"name": "int_field", "type": "INTEGER", "mode": "NULLABLE", "description": "int_field"},
{"name": "int_field_repeated", "type": "INTEGER", "mode": "REPEATED", "description": "int_field_repeated"}
]
wrapper_json = {'actual_output': str(json.dumps(bigquery_schema))}
print(json.dumps(wrapper_json))
which I can then access within terraform
# main.tf
data "external" "bigquery_schema" {
program = ["python", "${path.module}/get_dynamic_bigquery_schema.py"]
}
locals {
bigquery_schema= data.external.bigquery_schema.result['actual_output']
}
I'm building a JSON template that deploys a VM in Azure and executing a PowerShell script via Custom Script Extension (CSE). The JSON template was taken from here with some modifications for my company needs.
One of the parameters in the JSON template is adminPassword, that configures the password for the VM's local admin account.
The PowerShell script should deploy a domain controller on the VM. This is the important part of the PS script:
Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath C:\Windows\NTDS -DomainMode 7 -DomainName Domain.local -DomainNetbiosName Domain -ForestMode 7 -InstallDns:$true -LogPath C:\Windows\NTDS -SysvolPath C:\Windows\SYSVOL -NoRebootOnCompletion:$false -Force:$true
The Install-ADDSForest command requires the switch -SafeModeAdministratorPassword for the command to run.
Adding the password as plain text at the beginning of the PS script works, but plain text password is not an option. This is how I tested:
$SafePassPlain = 'Password'
$SafePass = ConvertTo-SecureString -string $SafePassPlain `
-AsPlainText -force
And entering this in the Install-ADDSForest line: -SafeModeAdministratorPassword $SafePass
This is the part in the JSON template where the script runs:
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.4",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"https://URLtoFile/DC-Domain.ps1
],
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File DC-Domain.ps1"
I would like to pass the adminPassword parameter from the JSON template to the PS script so it will use it for the -SafeModeAdministratorPassword switch.
Is it possible?
I read about ConvertFrom-Json and checked these: 1 2, but I'm not sure how to implement that on my end...
After checking this and this, seeing examples of passing parameters from a JSON template to a PS script, I tried implementing it like this, which didn't work:
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File DC-Domain1.ps1 -SafeModeAdministratorPassword ',parameters('adminPassword'))]"
Any help will be appreciated...
I'm starting to learn Powershell and I'm currently trying to read in a JSON file.
Here is my JSON file (named 'versions.json'):
{
"versions": {
"1.0.0": {
"Component1": "1.0.0",
"Component2": "1.0.0",
"Component3": "1.0.0",
},
"2.0.0": {
"Component1": "2.0.0",
"Component2": "2.0.0",
"Component3": "2.0.0"
}
}
}
I would like to read in this JSON file and print out the versions and what they consist of. For example, 1.0.0 consists of Component 1 at 1.0.0, Component 2 at 1.0.0, and Component 3 at 1.0.0.
I'm currently reading in the JSON file with this Powershell line:
$json = (Get-Content "versions.json" -Raw) | ConvertFrom-Json
Now, I want to iterate through $json and print out its data. I'm currently using this:
foreach($v in $json.versions) {
echo "Data: $v"
}
But, when I run my Powershell script, it prints:
Data: #{1.0.0=; 2.0.0=}
Is this the proper output? I was expecting to at least see two entries for 1.0.0 and 2.0.0. This feels like it may be a syntax issue but I am unsure. I am using Powershell version 5.
After using ConvertFrom-Json you have a PowerShell object which is a single item that has a versions property which has two sub-properties 1.0.0 and 2.0.0. Your ForEach is attempting to iterate them like a collection, but its just a single object.
However you can iterate over the properties as follows to get the result I think you wanted:
($Json.versions.psobject.properties) | foreach-object { "Data: $($_.name)" }
I would like to read JSON file to get the list of application names and their respective uninstall strings and pass them as arguments in power shell to uninstall the apps irrespective of whether the apps are Windows Installer(MSI) or setup. Can someone please suggest me how to do this. Thanks
Use the ConvertFrom-JSON.
This will create a PSObject out of the JSON
$TEST2 = $TEST | ConvertFrom-Json
Thank you for suggesting the answer, that worked. I used the following cmd-line
$J = Get-Content -Raw -Path $scriptDirectory\Files\XXX.json | ConvertFrom-Json
and passed the AppName property as given below to fetch the application names:
$AppName = $J |fl -Property AppName.
FYI.. my JSON file content looked as follows:
[
{
"AppName": "xxxxx",
"AppVersion": "aa.bb"
},
{
"AppName": "yyy",
"AppVersion": "aa.bb.cc"
}
]