0

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'.
1
  • Have you tried to add git pull before git push? What's the result? Commented Jun 24, 2024 at 8:29

2 Answers 2

0

Updates were rejected because the tip of your current branch is behind its remote counterpart. If you want to integrate the remote changes, use 'git pull' before pushing again.

This error generally is caused by that the local repository is not up-to-date with the remote repository. In other words, there are conflicts between the local repository and the remote repository.

You can consider one of the following methods to resolve the issue:

  1. Use the "git pull" command to sync the latest code version from remote repository to local repository.

    • Before making changes in the local repository, execute the "git pull" command so that the local repository can be up-to-date with the remote.
    • Make changes in local repository.
    • Push changes from local to the remote repository.
  2. Push the changes via temp branch.

    • Run the "git pull" command before make any changes in the local repository.
    • Create a temp branch based on the target branch that you want to make changes in the local repository.
    • Switch to the temp branch and make changes on it in the local repository.
    • Push the temp branch to the remote repository.
    • On the remote repository, create a PR (Pull Request) to merge changes from the temp branch to the target branch. If you get some conflicts on the PR, manually resolve the conflicts.
    • After successfully completing the PR merge, you can delete the temp branch.

If you can ensure that no other people will push changes to the target branch in the remote repository during you are making changes on the same branch in the local repository, you can select method #1. Otherwise, it is recommended using method #2.

Sign up to request clarification or add additional context in comments.

Comments

0

I can reproduce your issue. The reason of the issue is that someone makes new changes to your Azure repo between the checkout step and git push in your PowerShell@2 step. When you change your files in PowerShell task, the version of the repo is still the old one.

enter image description here

As @Bright Ran-MSFT suggested, you can create a temporary branch locally, then push changes through this branch and eventually merge it into your target branch.

  • Advantage: You will not get the same error in the pipeline, since the branch is created locally.
  • Disadvantage: Each time you run the pipeline, a new branch will be created and you need to delete it before the next build. Otherwise, if other users make changes to your temporary branch next time, you may encounter the same issue again in the temporary branch.

If you want to use a temporary branch, you can refer to the following scripts:

# Create a local branch "temp" based on the checkout branch
git checkout -b temp

# Make changes to your files
...

git add $file
git commit -m "Change files"

# Push to "temp" branch, you can see this branch in Azure repo then
git push origin temp

To avoid this situation as much as possible, you can create a separate branch for your pipeline, and run the pipeline based on this branch. If necessary, you can merge it to the original branch through PR. Then use branch security to limit others' access to and modification of your branch.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.