Search code examples
gitazurepowershellazure-pipelinescheckout

Pushing file to a repository using from Azure DevOps Pipeline


I have an azure devops pipeline that checks out two repositories with the following command

resources:
  repositories:
    - repository: 'essentials'
      type : 'git'
      name : 'ic-essentials'
steps:
- checkout: self
  persistCredentials: true
- checkout: 'essentials'

note essentials is a different repository to self.

later on it creates a file in the essentials repository with the following templated pipeline,

parameters:  
  - name: SolutionName
    type: string
    displayName: 'Name of the Solution to be saved'
  - name: Version
    type: string
    displayName: 'Semver version of the solution'
  - name: CoreRepository
    type: string
    displayName: 'Name of the core repository'

steps:
- task: PowerShell@2
  displayName: 'Setting git default'
  inputs:    
    targetType: 'inline'
    script: |      
      git config --global user.email $(Git.UserEmail)
      git config --global user.name $(Git.UserName)     
           

- task: PowerShell@2
  displayName: 'Create manifest'
  inputs:
    targetType: 'inline'
    script: |
        Write-Host "Logic for manifest creation is loaded"
        # A logic that creates a file in the following location
        # $(Build.SourcesDirectory)/${{parameters.CoreRepository}}/Manifest

- task: PowerShell@2
  displayName: 'Committing files and pushing'
  inputs:    
    targetType: 'inline'
    script: |      
      Set-Location -Path $(Build.SourcesDirectory)/${{parameters.CoreRepository}}      
      git push --set-upstream origin main
      git add .
      git commit -m "Committing the manifest file"
      git push origin HEAD:main

i am getting the following error,

error: src refspec main does not match any
error: failed to push some refs to 'https://dev.azure.com/MyOrg/MyProj/_git/MyRepo'
[detached HEAD f327404] Committing the manifest file
 1 file changed, 8 insertions(+)
 create mode 100644 Manifest/THEFILE.manifest.xml
fatal: could not read Password for 'https://[email protected]': terminal prompts disabled
##[error]PowerShell exited with code '1'.

i can confirm

  • the repository exists. Also does the branch main
  • the file is definitely getting created in the stipulated location.
  • the agent My Project Build Service (My Org) as contributor access to the repository. enter image description here

I still think its to do with the access the agent has to the repository. am I overlooking something obvious?


Solution

  • Based on this document, you may test the sample below.

    resources:
      repositories:
      - repository: essentials
        type: git
        name: ic-essentials
    variables:
      essentialsRepoName: $[ resources.repositories.essentials.name ]
      Git.UserEmail: [email protected]
      Git.UserName: $(Build.DefinitionName)
    
    steps:
    - checkout: self
    - checkout: essentials
      persistCredentials: true #Use the System.AccessToken of the build service account to git push
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          Write-Host "1. Create a new file under $(Build.SourcesDirectory)/$(essentialsRepoName)/Manifest"
          New-Item -Path $(Build.SourcesDirectory)/$(essentialsRepoName)/Manifest/NewManifest-$(Build.BuildId).md -ItemType File
          
          Write-Host "2. Configure git and push new manifest file to repo resource - $(essentialsRepoName)"
          git config --global user.email $(Git.UserEmail)
          git config --global user.name $(Git.UserName)
    
          Set-Location -Path $(Build.SourcesDirectory)/$(essentialsRepoName)
          git checkout -b main
          git add .
          git commit -m "Committing the manifest file NewManifest-$(Build.BuildId).md"
          git push origin main
    

    enter image description here Hope this helps.