I hope somebody can help me to solve this issue that is driving me crazy.
I have a basic testing infra on which I am learning.
I have a basic web app that runs a Microsoft (tutorial) website.
In my project I have a json file on which I used to update manually the version of the code to match always the tag on GitHub. Just to be clear, this is a basic version.json
file with a version:
{
"app": {
"name": "TestingInfra",
"service": "TestInfraConfiguration",
"version": "vX.X.X"
}
}
When I am ready to deploy, I create a tag in GitHub and manually update the version
number to match GitHub repo and I push.
At this stage it was a bit annoying to do this manually, so I wanted to automate the sed of that value in the json to automatically pick the tag from my repo.
In my pipeline yaml file as first step I have this configuration
- powershell: |
# Write your PowerShell commands here.
Write-Host "Update Build.BuildNumber"
cd $(System.DefaultWorkingDirectory)
$Latesttag = $(git describe --tags $(git rev-list --tags --max-count=1))
Write-Host "The latest git tag is $Latesttag "
Write-Host
"##vso[build.updatebuildNumber]$Latesttag"
- bash: |
version=$Latesttag
sed -i "s|$version|$version|g" ./version.json
cat ./version.json
In the PowerShell task, I am overriding the BuildNumber of the agent with the Tag from my GitHub. And this works just perfectly.
What I would like to do, is to use the same $Latesttag
from the Powershell, and replace the json file version
in the version.json
The pipeline does not fail like this, but also does not update the version placeholder.
Please can anyone help to understand how to achieve this?
And please if something is not clear, just ask for more informations, I will be glad to give as much details as I can
CodePudding user response:
You can use File Transform Azure.
I did something like the below in my pipelines.yml
variables
buildNumber: '$(Build.BuildNumber)'
... other code
steps:
- task: FileTransform@1
inputs:
folderPath: '$(System.DefaultWorkingDirectory)/drop'
fileType: 'json'
targetFiles: 'assets/json/version.json'
I have a file in assets/json/version.json
like below
{
"buildNumber" : ""
}
After steps from pipelines, It will replace the build number variable with correct number
After Build
{
"buildNumber" : "2022.4.13.2"
}
CodePudding user response:
See if this gets you closer to the desired behavior. Note comments in the code explaining the changes.
- powershell: |
Write-Host "Update Build.BuildNumber"
cd $(System.DefaultWorkingDirectory)
# Pretend the latest tag is v2.0.0
$Latesttag = "v2.0.0"
Write-Host "The latest git tag is $Latesttag "
# *** Note change in this command compared to original post
Write-Host "##vso[task.setvariable variable=LATEST_TAG]$Latesttag"
- bash: |
# Pretend that you want to update v1.0.0 in version.json
# to the latest tag (v2.0.0) derived in the powershell step
cat > ./version.json << EOF
{
"app": {
"name": "TestingInfra",
"service": "TestInfraConfiguration",
"version": "v1.0.0"
}
}
EOF
echo "version.json - BEFORE CHANGE:"
cat ./version.json
# Do replace
sed -i "s|v1.0.0|$LATEST_TAG|g" ./version.json
# Verify change
echo "version.json - AFTER CHANGE:"
cat ./version.json
Output:
Starting: Bash
==============================================================================
Task : Bash
Description : Run a Bash script on macOS, Linux, or Windows
Version : 3.201.1
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash /home/vsts/work/_temp/dheudhwj345-737-shej34.sh
version.json - BEFORE CHANGE:
{
"app": {
"name": "TestingInfra",
"service": "TestInfraConfiguration",
"version": "v1.0.0"
}
}
version.json - AFTER CHANGE:
{
"app": {
"name": "TestingInfra",
"service": "TestInfraConfiguration",
"version": "v2.0.0"
}
}
Finishing: Bash