gitazureazure-devopsazure-pipelinesazure-pipelines-yaml

Azure pipeline: can't push a file to an already-existing Azure repo branch


I'm trying to write an Azure pipeline that:

  • runs a python (myTestPythonFile.py) script that queries an API and returns a json file (myJson.json)
  • pushes this json file to a specific branch (DIN_grafana_json_archives) in my Azure repo.

I would like this pipeline to run periodically so as to create a series of archived versions of the json file.

The yaml below successfully created my target branch:


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'

At this stage, my branch DIN_grafana_json_archives is successfully created and contains the json file I pushed to it.

Now I'd like to run the pipeline again, that is, to retrieve a new version of the same json file and add it to the DIN_grafana_json_archives branch. Here's my edited yaml file:

# 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

Upon running, this bash step fails with the following error:

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

I've tried looking into

  • what "detached HEAD" means
  • the "pathspec did not match any file" error and experimented with pulling/fetching the repo to the Azure VM before the push, but I keep getting the same error message at some stage.

I'm out of my depth and I'd like someone to help solve this issue. It would also be amazing if you could provide some explanation so I can better understand what exactly is not working here. Thank you so much!


Solution

  • If the branch DIN_grafana_json_archives has been existing in the remote repository, and a myJson.json file also is existing in the remote DIN_grafana_json_archives branch.

    Now, if you want to update the content of myJson.json file and push the updates to the remote DIN_grafana_json_archives branch, you can do like as below:

    1. Go to Project Settings > Repositories > Security tab, ensure the following two identities at least have the Contribute permission set to Allow.

      • {project name} Build Service ({organization name})
      • Project Collection Build Service ({organization name})

      enter image description here

    2. Configure your pipeline like as below.

        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
    

    The command you are using,

    git checkout DIN_grafana_json_archives
    

    it just creates a new local branch with the name 'DIN_grafana_json_archives'. It will not automatically track with the existing remote 'DIN_grafana_json_archives' branch. This should be the reason of the error you get.

    The command I shared above,

    git checkout -b DIN_grafana_json_archives origin/DIN_grafana_json_archives
    

    OR

    git checkout -b DIN_grafana_json_archives --track origin/DIN_grafana_json_archives
    

    it will create the local branch (DIN_grafana_json_archives) and let it track with the existing remote branch (origin/DIN_grafana_json_archives).


    Related documentations: