git Azure管道:无法将文件推送到现有的Azure存储库分支

eyh26e7m  于 5个月前  发布在  Git
关注(0)|答案(1)|浏览(59)

我正在尝试编写一个Azure管道:

  • 运行python(myTestPythonFile.py)脚本,该脚本查询API并返回json文件(myJson.json)
  • 将此JSON文件推送到我的Azure存储库中的特定分支(DIN_grafana_json_archives)。

我希望这个管道定期运行,以便创建一系列json文件的存档版本。
下面的yaml成功创建了我的目标分支:

trigger: none

pool:
  vmImage: ubuntu-latest

steps:
- task: UsePythonVersion@0
  displayName: 'Use Python 3.8'
  inputs:
   versionSpec: 3.8

- 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'
    

- bash: |
   git config --global user.name {myUsername}
   git config --global user.email {[email protected]}
    
   git switch --orphan "DIN_grafana_json_archives"
   
   git add myJson.json
   git commit -m "add file myJson.json"
  

   git push --set-upstream https://{myPersonalAccessToken}@dev.azure.com/myOrganization/repo_path/_git/repo_name DIN_grafana_json_archives

  displayName: 'push json file to repo'

字符串
在这个阶段,我的分支DIN_grafana_json_archives成功创建,并包含我推送到它的json文件。
现在我想再次运行管道,也就是说,检索同一个json文件的新版本,并将其添加到DIN_grafana_json_archives分支。下面是我编辑的yaml文件:

# previous steps of the pipeline remain unchanged
- bash: |
   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 "add file myJson.json"

   git push https://{myPersonalAccessToken}@dev.azure.com/myOrganization/repo_path/_git/repo_name  DIN_grafana_json_archives


在运行时,这个bash步骤失败,并出现以下错误:

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.231.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash /home/vsts/work/_temp/a8c02273-cfdd-4f15-911b-377bd956ab7d.sh
error: pathspec 'DIN_grafana_json_archives' did not match any file(s) known to git
[detached HEAD fecac33] add file myJson.json
 1 file changed, 285 insertions(+)
 create mode 100644 myJson.json
error: src refspec DIN_grafana_json_archives does not match any
error: failed to push some refs to 'https://dev.azure.com/altendin-ifam/Poc%20FinOps%20MOPFI/_git/Poc%20FinOps%20MOPFI'
##[error]Bash exited with code '1'.
Finishing: Bash


我试过调查

  • “分离的头”是什么意思
  • “pathspec does not match any file”错误,并尝试在推送之前将存储库拉取到Azure VM,但在某个阶段我一直收到相同的错误消息。

我超出了我的深度,我想有人来帮助解决这个问题.这也将是惊人的,如果你能提供一些解释,所以我可以更好地了解到底是什么是不工作在这里.非常感谢!

pzfprimi

pzfprimi1#

如果分支**DIN_grafana_json_archives已存在于远程存储库中,并且远程DIN_grafana_json_archives分支中也存在一个myJson.json文件。
现在,如果您想更新
myJson.json文件的内容,并将更新推送到远程DIN_grafana_json_archives**分支,可以按照如下操作:

  • 进入【项目设置】>【存储库】>【安全】选项卡,确保以下两个身份至少有【Contribute】权限设置为【Allow】。
    *{project name} Build Service ({organization name})
    *Project Collection Build Service ({organization name})


的数据

  • 像下面这样配置你的管道。
steps:
    # Check out source files and persist the Git Credentials for use in next steps.
    - checkout: self
      persistCredentials: true

    # Check out 'DIN_grafana_json_archives' branch from remote.
    - task: Bash@3
      displayName: 'Check out branch DIN_grafana_json_archives'
      inputs:
        targetType: inline
        script: git checkout -b DIN_grafana_json_archives origin/DIN_grafana_json_archives

    # Generate a new 'myJson.json' file with the new content in the Python script. 
    - 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 {username}
          git config --global user.email {email}
          git add myJson.json
          git commit -m "Update file myJson.json"
          git push

字符串
您正在使用的命令,

git checkout DIN_grafana_json_archives


它只会创建一个名为“DIN_grafana_json_archives* x”的新本地分支。它不会自动跟踪现有的远程“DIN_grafana_json_archives**”分支。这应该是您遇到错误的原因。
我在上面分享的命令,

git checkout -b DIN_grafana_json_archives origin/DIN_grafana_json_archives


git checkout -b DIN_grafana_json_archives --track origin/DIN_grafana_json_archives


它将创建本地分支(DIN_grafana_json_archives),并让它跟踪现有的远程分支(origin/DIN_grafana_json_archives)。
相关文件:

相关问题