I am using azure DevOps for a build pipeline in which I am about to deploy my solution. In that i have scenario where a power shell code which is in the same repo will be triggered during the build pipeline and that power shell code is about to update a certain value modify 4 files upon each run. For this i have used some git commands to add and commit the changes to the repo. The build was successful but not the modification of the files. I will provide my powershell code and pipeline script here and if i try to push them i am facing an error during the build.
This is the powershell code
$yamlFiles = @(
@{
Path = "path to the file"
Pattern = "pattern of the code line to be changed"
},
@{
Path = "path to the file"
Pattern = "pattern of the code line to be changed"
},
@{
Path = "path to the file"
Pattern = "pattern of the code line to be changed"
}
)
foreach ($fileInfo in $yamlFiles) {
# Extract file path and pattern
$yamlFilePath = $fileInfo.Path
$pattern = $fileInfo.Pattern
# Read the content of the YAML file
$content = Get-Content $yamlFilePath
# Loop through each line and find the line with the specified pattern
for ($i = 0; $i -lt $content.Length; $i++) {
if ($content[$i] -match "$pattern(\d+)$") {
# Extract the last integer
$lastInteger = [int]($content[$i] -replace "^.*$pattern(\d+)$", '$1')
# Calculate the new integer (increment by 1)
$newInteger = $lastInteger + 1
# Form the new line with the updated last integer
$newLine = $content[$i] -replace "$pattern\d+$", "$pattern$newInteger"
# Print the changes to the console
Write-Host "File: $yamlFilePath"
Write-Host "Old Line: $($content[$i])"
Write-Host "New Line: $newLine"
# Replace the old line with the new line in the content array
$content[$i] = $newLine
# Update the variables YAML file
$variableYamlPath = "file path"
$variableYamlContent = Get-Content $variableYamlPath
for ($j = 0; $j -lt $variableYamlContent.Length; $j++) {
if ($variableYamlContent[$j] -match "^- name: incrementedValue$") {
# Update the corresponding value line
Write-Host "Updating incrementedValue in azure-finops-variables-pipelines.yml"
Write-Host "Old Value Line: $($variableYamlContent[$j + 1])"
$variableYamlContent[$j + 1] = " value: $newInteger"
Write-Host "New Value Line: $($variableYamlContent[$j + 1])"
break
}
}
# Write the updated content back to the variables YAML file
$variableYamlContent | Set-Content -Path $variableYamlPath
# Write the modified content back to the YAML file
$content | Set-Content $yamlFilePath
# Print a message indicating the file has been updated
Write-Host "Updated file: $yamlFilePath"
# Add and commit the changes
git add $yamlFilePath
git add $variableYamlPath
git commit -m "Updated the files"
# Exit the loop as we have already found and modified the required line
break
}
}
}
git push origin demo
this is the pipeline code
trigger:
- demo
variables:
- template: variables.yml
pool: demo-agent
steps:
- task: PowerShell@2
inputs:
targetType: 'filePath'
filePath: 'imageTag-version.ps1'
arguments: ''
pwsh: true
and this is the error during the build if we try to push
To https://dev.azure.com/_git/Demo
! [rejected] demo -> demo (non-fast-forward)
error: failed to push some refs to 'https://dev.azure.com/_git/Demo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Script execution completed. You can now push the changes manually using 'git push'.


git pullbeforegit push? What's the result?