Json file content version variable value Increment by 0.0.1 - json

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

Related

How to set Variables in Release pipeline in Azure DevOps pipeline

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

Terraform's external data source: STDOUT syntax unclear

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']
}

validating file name matches directory name

Have to write a ruby script which can validate that the key in s3 matches directory name.
I have a json file, path name- Apple/Employee/Background/test.tf
How can I validate that my directory path Apple/Employee/Background/ matches with the "key": "Apple/Employee/Background/" in json file.
I used jq and parsed json file to retrive the value of "key".
cat conf.json | jq '.terraform[] | .backend[] | .s3[] | .key'
"Apple/Employee/Background/terraform.tfstate"
I want to compare only Apple/Employee/Background with directory path and see if they are same or not.
json file looks like:
"terraform": [
{
"backend": [
{
"s3": [
{
"bucket": "terraform-dev",
"dynamodb": "terraform_files",
"encrypt": "true",
"key": "Apple/Employee/Background/terraform.tfstate"
}
]
}
],
"required_version": "~> 0.11.8"
}
]
the directory path looks like- Apple/Employee/Background/conf.tf
You can use File.dirname to get the directory path minus the base entry (in this case, 'terraform.tfstate')
# Assuming `key` contains the value from the JSON file
dir = File.dirname(key) # => "Apple/Employee/Background"
Note that the path is returned without a trailing slash.

How to read JSON data from Powershell script and iterate through it

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)" }

How to get the list of application names and uninstall strings from JSON file and pass them as parameters to uninstall an application using powershell

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"
}
]