git 使用Azure DevOps Pipeline将文件推送到存储库

krcsximq  于 11个月前  发布在  Git
关注(0)|答案(2)|浏览(112)

我有一个Azure DevOps管道,它使用以下命令 checkout 两个存储库

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

字符串
注意essentialsself不同。
稍后它在essentials存储库中创建具有以下模板化流水线的文件,

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


我得到以下错误,

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


我可以证实

  • 存储库存在。分支main也存在
  • 文件肯定是在规定的位置创建的。
  • 代理My Project Build Service (My Org)作为贡献者访问存储库。

我仍然认为这与代理对存储库的访问有关。我是否忽略了一些明显的东西?

cs7cruho

cs7cruho1#

基于此document,您可以测试下面的示例。

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

字符串


希望这对你有帮助。

djmepvbi

djmepvbi2#

您可以使用一个小脚本来处理凭据:

- task: PowerShell@2
  displayName: 'Committing files and pushing'
  inputs:    
    targetType: 'inline'
    script: |      
      git config --global credential. Helper "!f() { echo \`"username=.`npassword=`${TOKEN}\`"; }; f"
      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
  env:
    TOKEN: $(System.AccessToken)

字符串

相关问题