git Azure Devops -未跟踪的文件,阻止推送文件的较新版本

yuvru6vn  于 2024-01-04  发布在  Git
关注(0)|答案(3)|浏览(158)

我的目标是运行一个DevOps管道(在我的repo的main分支中),它执行以下操作:

  • 运行一个Python脚本(在我的repo的main分支中),它返回一个名为myJson.json的JSON
  • myJson.json推送到同一个repo中名为DIN_grafana_json_archives的另一个分支。
  • 我希望能够定期运行完全相同的脚本,这样每次脚本运行时都会将myJson.json的更新版本推送到DIN_grafana_json_archives。这将允许我保留不同版本的json的存档。
  • 初始情况 *

这是我的main分支:x1c 0d1x
23 11 24 myTestPipeline.yml是我的管道。
myTestPythonFile.py是在管道中调用的脚本。
DIN_grafana_json_archives分支已存在且为空。
下面是我的pipeline:

  1. trigger: none
  2. pool:
  3. vmImage: ubuntu-latest
  4. steps:
  5. # Check out source files and persist the Git Credentials for use in next steps.
  6. - checkout: self
  7. persistCredentials: true
  8. # Generate a new 'myJson.json' file with the new content in the Python script.
  9. - script: python3 -m pip install --upgrade pip
  10. displayName: 'upgrade pip'
  11. - script: python3 -m pip install pandas
  12. displayName: 'install pandas'
  13. - script: python3 -m pip install requests
  14. displayName: 'install requests'
  15. - script: python3 -m pip install datetime
  16. displayName: 'install datetime'
  17. - task: PythonScript@0
  18. inputs:
  19. scriptSource: 'filePath'
  20. scriptPath: '$(System.DefaultWorkingDirectory)/myTestPythonFile.py'
  21. # Commit and push the updated 'myJson.json' file to remote 'DIN_grafana_json_archives' branch.
  22. - task: Bash@3
  23. displayName: 'Push updates to remote'
  24. inputs:
  25. targetType: inline
  26. script: |
  27. git config --global user.name {myUsername}
  28. git config --global user.email {[email protected]}
  29. git checkout DIN_grafana_json_archives
  30. git add myJson.json
  31. git commit -m "Update file myJson.json"
  32. git push

字符串
脚本运行正常,myJson.json被成功推送到DIN_grafana_json_archives

第二次运行脚本时出现问题

目的:我希望脚本将新版本的myJson.json(此版本可能与前一版本相同)推送到DIN_grafana_json_archives,即使它已经包含此文件(的旧版本)。
问题:脚本在最后阶段失败,并显示以下错误消息:

  1. error: The following untracked working tree files would be overwritten by checkout:
  2. myJson.json
  3. Please move or remove them before you switch branches.
  4. Aborting
  5. [detached HEAD db4eeee] Update file myJson.json
  6. 1 file changed, 285 insertions(+)
  7. create mode 100644 myJson.json
  8. fatal: You are not currently on a branch.
  9. To push the history leading to the current (detached HEAD)
  10. state now, use
  11. git push origin HEAD:<name-of-remote-branch>
  12. ##[error]Bash exited with code '128'.
  13. Finishing: Push updates to remote


如果我导航到DIN_grafana_json_archives分支并删除myJson.json,脚本运行正常。我需要做什么才能使我的脚本能够多次运行?谢谢。

编辑

更换

  1. git checkout DIN_grafana_json_archives


  1. git checkout -f DIN_grafana_json_archives


防止最后一步失败。此步骤的日志为:

  1. Previous HEAD position was bdf49df Update 23 11 24 myTestPipeline.yml for Azure Pipelines
  2. Switched to a new branch 'DIN_grafana_json_archives'
  3. branch 'DIN_grafana_json_archives' set up to track 'origin/DIN_grafana_json_archives'.
  4. On branch DIN_grafana_json_archives
  5. Your branch is up to date with 'origin/DIN_grafana_json_archives'.
  6. nothing to commit, working tree clean
  7. Everything up-to-date
  8. Finishing: Push updates to remote


然而,myJson.json的更新版本没有被推送到目标仓库。为什么会这样?为什么日志显示“nothing to commit”,即使接下来的行是git add / commit / push?

hk8txs48

hk8txs481#

而不是 checkout DIN_grafana_json_archives,这是导致错误,你可以直接推它使用

  1. git push origin HEAD:DIN_grafana_json_archives

字符串
只需在脚本中删除checkout步骤,并将git push替换为以下命令。

pgx2nnw8

pgx2nnw82#

这是因为checkout命令使用的是管道排队时的 commit,而不是分支中的最新值。这使得管道可以从特定的时间点确定性地“重新运行”。在后台,git将这种情况称为 detached head
如果您查看checkout命令的输出,您将看到类似于以下内容:


的数据
请记住,分支可能已经从管道最初排队的时间发生了变化,因此您可能需要获取最新的更改,以防止您的推送被拒绝。如果您的管道作为批量触发器自动运行,则不太可能发生这种情况,因为您应该始终拥有最新的更改。

  1. git fetch
  2. git checkout $branch
  3. git pull

字符串
第二件需要考虑的事情是,你应该在修改仓库内容之前 checkout 并更新 *,因为git会阻止你使用未跟踪的更改进行切换。正如你所发现的,这就是为什么你必须添加--force参数。
最后,作为一种优化,你应该只在有修改的情况下提交和推送。例如,假设你的脚本产生的结果与你的文件的当前副本相同。
假设您只修改现有文件,以下操作应该可以正常工作:

  1. changes=$(git ls-files --modified --deleted)
  2. if [ -n "$changes" ]; then
  3. # stage and add all changes
  4. git commit --all -m"Updated from pipeline [skip ci]"
  5. git push
  6. fi

展开查看全部
wribegjk

wribegjk3#

然而,myJson.json的更新版本没有被推送到目标仓库。为什么会这样?为什么日志显示“nothing to commit”,即使接下来的行是git add / commit / push?
当你用git checkout -f DIN_grafana_json_archives checkout 时,它会用分支中的旧文件替换新的myJson.json文件,这就是为什么它说“没有提交”。

解决方法一

您可以复制文件并使用新的唯一名称对其进行重命名。然后您可以将文件推送到分支,而不会出现此问题。
My yaml sample:

  1. trigger: none
  2. pool:
  3. vmImage: ubuntu-latest
  4. steps:
  5. # Check out source files and persist the Git Credentials for use in next steps.
  6. - checkout: self
  7. persistCredentials: true
  8. # Just to create a new myJson.json file.
  9. - task: PowerShell@2
  10. inputs:
  11. targetType: 'inline'
  12. script: 'Write-Output "Backups time $(Get-Date)" >>myJson.json'
  13. # Copy to backup the json file with a new filename.
  14. # Commit and push the updated the new filename json file to remote 'DIN_grafana_json_archives' branch.
  15. - task: Bash@3
  16. displayName: 'Push updates to remote'
  17. inputs:
  18. targetType: inline
  19. script: |
  20. cp myJson.json $(Build.BuildNumber)myJson.json
  21. git config --global user.name {myUsername}
  22. git config --global user.email {[email protected]}
  23. git fetch
  24. git checkout DIN_grafana_json_archives
  25. git add $(Build.BuildNumber)myJson.json
  26. git commit -m "Upload $(Build.BuildNumber)myJson.json for BuildId $(Build.BuildId)"
  27. git push

字符串
测试结果:
x1c 0d1x的数据

解决方法二

复制以使用新文件名备份json文件。 checkout 分支后,删除旧的json文件并重命名新的json文件。然后提交并将更新的json文件推送到远程“DIN_grafana_json_archives”分支。
Yaml:

  1. trigger: none
  2. pool:
  3. vmImage: ubuntu-latest
  4. steps:
  5. # Check out source files and persist the Git Credentials for use in next steps.
  6. - checkout: self
  7. persistCredentials: true
  8. # Just to create a new myJson.json file.
  9. - task: PowerShell@2
  10. inputs:
  11. targetType: 'inline'
  12. script: 'Write-Output "Backups time $(Get-Date)" >>myJson.json'
  13. # Copy to backup the json file with a new name.
  14. # After check out the branch, delete the old json file and rename back the new json file.
  15. # Then commit and push the updated the json file to remote 'DIN_grafana_json_archives' branch.
  16. - task: Bash@3
  17. displayName: 'Push updates to remote'
  18. inputs:
  19. targetType: inline
  20. script: |
  21. cp myJson.json $(Build.BuildNumber)myJson.json
  22. git config --global user.name {myUsername}
  23. git config --global user.email {[email protected]}
  24. git fetch
  25. git checkout DIN_grafana_json_archives -f
  26. rm myJson.json
  27. cp $(Build.BuildNumber)myJson.json myJson.json
  28. git add myJson.json
  29. git commit -m "$(Build.BuildNumber) Upload myJson.json "
  30. git push


测试结果:


展开查看全部

相关问题