我的目标是运行一个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 0d1x23 11 24 myTestPipeline.yml
是我的管道。myTestPythonFile.py
是在管道中调用的脚本。DIN_grafana_json_archives
分支已存在且为空。
下面是我的pipeline:
trigger: none
pool:
vmImage: ubuntu-latest
steps:
# Check out source files and persist the Git Credentials for use in next steps.
- checkout: self
persistCredentials: true
# Generate a new 'myJson.json' file with the new content in the Python script.
- script: python3 -m pip install --upgrade pip
displayName: 'upgrade pip'
- script: python3 -m pip install pandas
displayName: 'install pandas'
- script: python3 -m pip install requests
displayName: 'install requests'
- script: python3 -m pip install datetime
displayName: 'install datetime'
- task: PythonScript@0
inputs:
scriptSource: 'filePath'
scriptPath: '$(System.DefaultWorkingDirectory)/myTestPythonFile.py'
# Commit and push the updated 'myJson.json' file to remote 'DIN_grafana_json_archives' branch.
- task: Bash@3
displayName: 'Push updates to remote'
inputs:
targetType: inline
script: |
git config --global user.name {myUsername}
git config --global user.email {[email protected]}
git checkout DIN_grafana_json_archives
git add myJson.json
git commit -m "Update file myJson.json"
git push
字符串
脚本运行正常,myJson.json
被成功推送到DIN_grafana_json_archives
。
第二次运行脚本时出现问题
目的:我希望脚本将新版本的myJson.json(此版本可能与前一版本相同)推送到DIN_grafana_json_archives
,即使它已经包含此文件(的旧版本)。
问题:脚本在最后阶段失败,并显示以下错误消息:
error: The following untracked working tree files would be overwritten by checkout:
myJson.json
Please move or remove them before you switch branches.
Aborting
[detached HEAD db4eeee] Update file myJson.json
1 file changed, 285 insertions(+)
create mode 100644 myJson.json
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use
git push origin HEAD:<name-of-remote-branch>
##[error]Bash exited with code '128'.
Finishing: Push updates to remote
型
如果我导航到DIN_grafana_json_archives
分支并删除myJson.json
,脚本运行正常。我需要做什么才能使我的脚本能够多次运行?谢谢。
编辑
更换
git checkout DIN_grafana_json_archives
型
与
git checkout -f DIN_grafana_json_archives
型
防止最后一步失败。此步骤的日志为:
Previous HEAD position was bdf49df Update 23 11 24 myTestPipeline.yml for Azure Pipelines
Switched to a new branch 'DIN_grafana_json_archives'
branch 'DIN_grafana_json_archives' set up to track 'origin/DIN_grafana_json_archives'.
On branch DIN_grafana_json_archives
Your branch is up to date with 'origin/DIN_grafana_json_archives'.
nothing to commit, working tree clean
Everything up-to-date
Finishing: Push updates to remote
型
然而,myJson.json的更新版本没有被推送到目标仓库。为什么会这样?为什么日志显示“nothing to commit”,即使接下来的行是git add / commit / push?
3条答案
按热度按时间hk8txs481#
而不是 checkout DIN_grafana_json_archives,这是导致错误,你可以直接推它使用
字符串
只需在脚本中删除checkout步骤,并将git push替换为以下命令。
pgx2nnw82#
这是因为
checkout
命令使用的是管道排队时的 commit,而不是分支中的最新值。这使得管道可以从特定的时间点确定性地“重新运行”。在后台,git将这种情况称为 detached head。如果您查看
checkout
命令的输出,您将看到类似于以下内容:的数据
请记住,分支可能已经从管道最初排队的时间发生了变化,因此您可能需要获取最新的更改,以防止您的推送被拒绝。如果您的管道作为批量触发器自动运行,则不太可能发生这种情况,因为您应该始终拥有最新的更改。
字符串
第二件需要考虑的事情是,你应该在修改仓库内容之前 checkout 并更新 *,因为git会阻止你使用未跟踪的更改进行切换。正如你所发现的,这就是为什么你必须添加
--force
参数。最后,作为一种优化,你应该只在有修改的情况下提交和推送。例如,假设你的脚本产生的结果与你的文件的当前副本相同。
假设您只修改现有文件,以下操作应该可以正常工作:
型
wribegjk3#
然而,myJson.json的更新版本没有被推送到目标仓库。为什么会这样?为什么日志显示“nothing to commit”,即使接下来的行是git add / commit / push?
当你用
git checkout -f DIN_grafana_json_archives
checkout 时,它会用分支中的旧文件替换新的myJson.json
文件,这就是为什么它说“没有提交”。解决方法一
您可以复制文件并使用新的唯一名称对其进行重命名。然后您可以将文件推送到分支,而不会出现此问题。
My yaml sample:
字符串
测试结果:
x1c 0d1x的数据
解决方法二
复制以使用新文件名备份json文件。 checkout 分支后,删除旧的json文件并重命名新的json文件。然后提交并将更新的json文件推送到远程“DIN_grafana_json_archives”分支。
Yaml:
型
测试结果:
的