Azure DevOps构建流水线推送代码遇非快进错误求助
Azure DevOps流水线脚本更新文件失败及Git Push报错排查
问题描述
使用Azure DevOps构建流水线触发仓库内的PowerShell脚本,预期每次运行更新4个文件的指定值,通过Git命令提交变更,但构建成功后文件未完成修改,执行git push时出现非快进(non-fast-forward)错误。
相关代码
PowerShell脚本
$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
流水线YAML配置
trigger: - demo variables: - template: variables.yml pool: demo-agent steps: - task: PowerShell@2 inputs: targetType: 'filePath' filePath: 'imageTag-version.ps1' arguments: '' pwsh: true
报错信息
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'.
排查与解决
一、文件未修改问题
- 校验路径与正则匹配:确认
$yamlFiles中的Path是流水线工作目录下的相对路径,验证Pattern正则表达式是否能精准匹配目标行(可本地执行脚本测试匹配逻辑)。 - 补全文件配置:若需更新4个文件,补充
$yamlFiles数组中第四个文件的路径与匹配规则。 - 调整提交逻辑:当前脚本处理单个文件后就执行
git add和git commit,会生成多份提交,可改为所有文件处理完成后统一提交。 - 指定文件编码:使用
Set-Content时添加-Encoding utf8参数,避免因编码问题导致内容未正确写入。
二、Git Push报错问题
- 拉取最新代码后推送:在
git push前添加拉取命令,解决本地分支落后于远程的问题:git config pull.rebase false git pull origin demo git push origin demo - 设置完整克隆:在流水线YAML中添加完整克隆步骤,避免浅克隆导致的分支信息不全:
steps: - checkout: self fetchDepth: 0 # 完整克隆仓库所有历史 - task: PowerShell@2 inputs: targetType: 'filePath' filePath: 'imageTag-version.ps1' arguments: '' pwsh: true - 配置Git身份:在PowerShell脚本开头添加身份配置,确保流水线有权限执行Git提交:
git config --global user.name "Azure DevOps Pipeline" git config --global user.email "pipeline@dev.azure.com"
内容的提问来源于stack exchange,提问作者Aadhi 17
相关产品推荐
相关产品推荐

