Search code examples
githubgithub-actions

How do I clone A repo in B repo github actions if two repo is in same organization, not using PAT?


In B repo, I have to clone other repos in same organization.

like, in B's action I want to clone A/C/D/E... and other repos dynamically. I don't want to add each other private repo's PAT, because all repos are in same organization so I just want to use global organization secret.

I tried this:

      - name: Use environment variables
        if: github.event.pull_request.merged
        env:
          SERVICE_NAME: ${{ env.SERVICE_NAME }}
          ENVIRONMENT: ${{ env.ENVIRONMENT }}
          VERSION: ${{ env.VERSION }}
        run: |
          echo "Service name: $SERVICE_NAME"
          echo "Environment: $ENVIRONMENT"
          echo "Version: $VERSION"

      - name: checkout deploy-metadata
        uses: actions/checkout@v3
        with:
          ref: 'refs/heads/master'

      - name: clone deploying server
        uses: actions/checkout@v3
        with:
          repository: private-repo/${{ env.SERVICE_NAME }}
          ref: v${{ env.VERSION }}
          token: ${{ secrets.GITHUB_TOKEN }}

but this doesn't work. How to clone other private repo in same organization without using each repo's PAT?


Solution

  • That was discussed before, and that thread references the documentation "Checkout multiple repos (private)"

    ${{ github.token }} is scoped to the current repository, so if you want to checkout a different repository that is private you will need to provide your own PAT.

    - name: Checkout
      uses: actions/checkout@v3
      with:
        path: main
    
    - name: Checkout private tools
      uses: actions/checkout@v3
      with:
        repository: my-org/my-private-tools
        token: ${{ secrets.GH_PAT }} # `GH_PAT` is a secret that contains your PAT
        path: my-tools
    

    So as long as you can provide a token of an account which has read access to each of those target repository, you should be able to clone them.