Search code examples
gitpowershellcontinuous-integrationazure-pipelinessbom

Can I send a generated file from Azure Pipeline to a second repository?


I’m new to the CI thing. I have an Azure Pipeline that generates a file based on my main repository. I want to know the best way to get this generated file and Commit it to another repository (without affecting the main repository).

Basically: [Pipeline of Main Repository] --> [Generate file] --> [Commit to secondary repository]

Important: The second repository doesn’t belong to the same organization.

I have not been able to find information on whether it is possible to clone and commit to a repository within a pipeline without affecting the main repository.

Thank you very much.


Solution

  • I can make it work with the following steps.

    Steps:

    1. Create an Azure Repos service connection for the Azure Repo in another organization with your Personal Access Token. enter image description here

    2. Check out multiple repositories in your pipeline. The source code is checked out into directories named after the repositories as a subfolder $(Build.SourcesDirectory). In my sample, the self repo path is $(Build.SourcesDirectory)/selfreponame, the second repo path is $(Build.SourcesDirectory)/testrepo.

    3. Use git command to push the generated file to the second repo in the bash task.

    Sample yaml:

    trigger:
    - none
    
    resources:
      repositories:
      - repository: MyAzureReposGitRepository # In a different organization
        endpoint: OtherReposServiceConnection
        type: git
        name: testproject/testrepo
        ref: refs/heads/main
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - checkout: self
    - checkout: MyAzureReposGitRepository
      persistCredentials: true
    
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          # create a new txt file
          echo "This is a text file." > myFile.txt      
          # copy the file to the folder of that repo. You can copy your generated file here.
          cp myFile.txt $(Build.SourcesDirectory)/testrepo
          
          cd $(Build.SourcesDirectory)/testrepo
          
          git config --global user.email "you@example.com"
          git config --global user.name "Your Name"
          git fetch
          git checkout main
          git add --all
          git commit -m "Add myFile.txt"
          git push
    
    

    Result :

    enter image description here