Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token to create orgA/repo from orgB template #96

Closed
arcezd opened this issue Jan 22, 2024 · 2 comments
Closed

Token to create orgA/repo from orgB template #96

arcezd opened this issue Jan 22, 2024 · 2 comments

Comments

@arcezd
Copy link

arcezd commented Jan 22, 2024

I have been trying to enable a Github Workflow that should be able to create a new repo at orgA, using a template repo from orgB, I don't believe there is a chance to support this since the GitHub Apps tokens are scoped to an individual account or a specific org afaik, but it could be pretty useful to have a way to support this using GitHub Apps.

These are the steps, I'm trying to use:

      - name: Generate a token
        id: generate-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.ZCLOUD_BOT_APP_ID }}
          private-key: ${{ secrets.ZCLOUD_BOT_APP_PRIVATE_KEY }}
          owner: ${{ github.event.inputs.repo-org }}

      - name: Create the new repo
        id: create-repo
        env:
          GITHUB_TOKEN: ${{ steps.generate-token-template.outputs.token }}
        run: |
          gh repo create ${{ github.event.inputs.repo-org }}/${{ github.event.inputs.repo-name }} \
            --internal \
            --template ${{ github.repository }}

This is similar to Issue 45, but I can't use the matrix approach since permission to both orgs is required between the same command exec.

@gr2m
Copy link
Contributor

gr2m commented Jan 22, 2024

Unless the template repository is open source, I don't think this is possible with a GitHub App installation access token, because by design an installation access token only has access to the account that the installation belongs to. There are no cross-account installations

@arcezd
Copy link
Author

arcezd commented Jan 22, 2024

Thanks for the clarification @gr2m 👌🏼, my workaround for this was to git checkout the repo using the GitHub Workflow token, clean the git repo history, and then use the GitHub App access token to create the new repo.

      - name: Generate a token
        id: generate-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.BOT_APP_ID }}
          private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}
          owner: ${{ github.event.inputs.repo-org }}
      
      - name: Checkout template repo
        id: checkout
        uses: actions/checkout@v4

      - name: Initialize the new repo
        id: init-repo
        run: |
          ## clean git
          rm -rf .git

          ## set git config
          git config --global init.defaultBranch main

          ## commit initial files
          git init
          git config user.name "${{ vars.BOT_APP_NAME }}[bot]"
          git config user.email "${{ secrets.BOT_APP_ID }}+${{ vars.BOT_APP_NAME }}[bot]@users.noreply.github.com"
          git add .
          git commit -m "initial commit [skip ci]"

      - name: Create the new repo
        id: create-repo
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
        run: |
          gh repo create ${{ github.event.inputs.repo-org }}/${{ github.event.inputs.repo-name }} \
            --internal \
            --source=. \
            --remote=upstream \
            --push
        ##--template ${{ github.repository }}

@arcezd arcezd closed this as completed Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants