8000 Create token for multiple organization/user accounts · Issue #45 · actions/create-github-app-token · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Create token for multiple organization/user accounts #45

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

Closed
gr2m opened this issue Sep 10, 2023 · 2 comments · Fixed by #63
Closed

Create token for multiple organization/user accounts #45

gr2m opened this issue Sep 10, 2023 · 2 comments · Fixed by #63
Labels

Comments

@gr2m
Copy link
Contributor
gr2m commented Sep 10, 2023

This is a follow up to #4 (comment)

@kmaehashi had posted the following request here

Under the GitHub Enterprise Cloud setup, we are often required to access repositories under multiple organizations. It would be great if you consider covering the checkout use case like these:

# Checkout orgA/repoA and orgB/repoB

- uses: actions/create-github-app-token@v1
  id: app-token
  with:
    app_id: ${{ vars.APP_ID }}
    private_key: ${{ secrets.PRIVATE_KEY }}
    repositories: orgA/repoA, orgB/repoB

- uses: actions/checkout@v4
  with:
    repository: 'orgA/repoA'
    token: ${{ steps.app-token.outputs.token }}

- uses: actions/checkout@v4
  with:
    repository: 'orgB/repoB'
    token: ${{ steps.app-token.outputs.token }}
# Checkout the current repository which has orgA/repoA and orgB/repoB as submodule

- uses: actions/create-github-app-token@v1
  id: app-token
  with:
    app_id: ${{ vars.APP_ID }}
    private_key: ${{ secrets.PRIVATE_KEY }}
    repositories: ${{ github.repository }}, orgA/repoA, orgB/repoB

- uses: actions/checkout@v4
  with:
    submodules: true
    token: ${{ steps.app-token.outputs.token }}

Unfortunately, an installation access token can by design only access a single account (GitHub user or organization account). There cannot be a single token that has access across multiple organizations.

But I've run into this requirement before and I see a possible workaround that would require an additional action and the user of matrix.

  1. Say there was an action like actions/get-app-installation-ids, it would take app_id and private_key as arguments, and optionally a list of logins to filter down the installations. The action would have installation_ids and installation_logins outputs.
  2. That output could be used to dynamically set strategy.matrix in a second job, so all steps would be run for each of the installation IDs
  3. In the second job, actions/create-github-app-token could be used to create an installation access token for that particular installation

I'm however not sure how we could filter down to specific repositories across multiple organizations. I'd need to experiment myself to see what's possible. Maybe the actions/get-app-installation-ids could take an argument like you suggest (say repositories: orgA/repoA, orgA/repoB, orgB/repoC) and then have a nested output like this: [["orgA", "repoA, repoB"],["orgB", "repoC"]] which we could could iterate through using the matrix and then split out the items like ["orgA", "repoA, repoB"] into owner: orgA, repositories: repoA, Repo B

@gr2m
Copy link
Contributor Author
gr2m commented Sep 29, 2023

I paired with @parkerbxyz on this problem today, and we figured out an approach for this, here is what our action workflow looks like:

name: Debug
"on":
  workflow_dispatch: {}
jobs:
  set-matrix:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{steps.set.outputs.matrix }}
    steps:
      - id: set
        run: echo 'matrix=[{"owner":"gr2m"},{"owner":"gr2m-sandbox","repos":["test12"]}]' >>"$GITHUB_OUTPUT"

  use-matrix:
    name: '@${{ matrix.ownersAndRepos.owner }} installation'
    needs: [set-matrix]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ownersAndRepos: ${{ fromJson(needs.set-matrix.outputs.matrix) }}

    steps:
      - run: echo owner - ${{ matrix.ownersAndRepos.owner }}
      - run: echo repos - ${{ join(matrix.ownersAndRepos.repos) }}
      - uses: gr2m/create-github-app-token@main
        id: app-token
        with:
          app_id: ${{ vars.GR2M_GITHUB_APP_ID }}
          private_key: ${{ secrets.GR2M_GITHUB_APP_PRIVATE_KEY }}
          owner: ${{ matrix.ownersAndRepos.owner }}
          repositories: ${{ join(matrix.ownersAndRepos.repos) }}
      - uses: octokit/request-action@v2.x
        id: get-installation-repositories
        with:
          route: GET /installation/repositories
        env:
          GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
      - run: echo $DATA
        env:
          DATA: ${{ steps.get-installation-repositories.outputs.data }}

Note that this only works with my fork at gr2m/create-github-app-token right now. But once #46 is merged and released this approach will work, and it doesn't even require a separate github action 🎉

I think once #46 lands we should document that approach in the README as a usage example for future reference

@parkerbxyz parkerbxyz linked a pull request Oct 12, 2023 that will close this issue
parkerbxyz added a commit that referenced this issue Oct 16, 2023
Adds an example workflow to the README that shows how a matrix strategy can be used to create tokens for multiple user or organization accounts. Resolves #45.
@create-app-token-action-releaser

🎉 This issue has been resolved in version 1.5.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant
0