通过不同 git 存储库中的提交触发 GitHub 操作,无需个人访问令牌

问题描述 投票:0回答:2

我想在 https://github.com/linux-test-project/ltp 中提交来触发 https://github.com/linux-test-project/linux-test-project 中的 GitHub Action .github.com。这可能吗?

我相信它可以通过 gitlab 管道工作。如果可能的话,它将帮助我避免使用个人访问令牌(避免安全问题)。

但也许我错了,example中的最新注释提到https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-工作流程,其中指出我需要使用个人访问令牌:(.

github continuous-integration github-actions
2个回答
0
投票

个人访问令牌旨在访问 GitHub 上的资源 代表你自己。要代表组织访问资源, 或者为了长期集成,您应该使用 GitHub 应用程序。 (来源

=> 看起来在 GitHub Actions 中,如果没有个人访问令牌,就无法跨存储库创建工作流程。

我需要这个来更新发布 github 页面的存储库。最后,我将在自定义存储库中添加网页内容https://github.com/linux-test-project/linux-test-project.github.com我将添加到源存储库中https:// /github.com/linux-test-project/ltp 脚本,动态生成源并创建(在 https://github.com/linux-test-project/ltp自定义 GitHub Actions 工作流程将公布来源。 https://github.com/linux-test-project/linux-test-project.github.com 将不再需要,将被删除。


0
投票

这是一种不需要使用 PAT 的方法。相反,使用安装在两个存储库中的 Github 应用程序。这就是权限的处理方式。该应用程序有权触发工作流程。

  1. 您首先需要通过github.com创建一个Github应用程序,并将其私钥获取到repo Secret中。

    GITHUB_APP_PRIVATE_KEY

  2. 然后获取应用程序的id并将其保密。

    GITHUB_APP_ID

以下操作的作用是:

  1. 使用私钥和应用程序 ID 生成 JWT
  2. 此 JWT 可用于调用 Github API 来获取我们的应用程序的安装位置(哪个存储库)
  3. 我们可以使用安装url来生成访问令牌
  4. 访问令牌可用于授权从 repo1 到 repo2 的工作流调度事件
jobs:
  trigger-wfs:
    runs-on: ubuntu-latest
    steps:
      - id: checkout-repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Set up python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install jwt

      - name: Write private key for multi repo app to pem
        run: |
          echo "$PRIVATE_KEY" > private-key.pem
        env:
          PRIVATE_KEY: ${{ secrets.GITHUB_APP_PRIVATE_KEY }}

      - name: Generate JWT
        id: generate-jwt
        env:
          APP_ID: ${{ secrets.GITHUB_APP_ID }}
        run: |
          JWT=$(python -c "
          import jwt
          import time
          import os

          pem_file = 'private-key.pem'
          app_id = os.environ['APP_ID']

          with open(pem_file, 'rb') as pem_file:
              signing_key = jwt.jwk_from_pem(pem_file.read())

          payload = {
              'iat': int(time.time()),
              'exp': int(time.time()) + 600,
              'iss': app_id
          }

          jwt_instance = jwt.JWT()
          encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')

          print(encoded_jwt)
          ")
          echo "JWT=$JWT" >> $GITHUB_ENV

      - name: Extract access_tokens_url # where the app is installed / which repos we added it to
        run: |
            INSTALLATIONS=$(curl -X GET \
                -H "Authorization: Bearer ${{ env.JWT }}" \
                -H "Accept: application/vnd.github.v3+json" \
                https://api.github.com/app/installations)
            
            echo "app installations: $INSTALLATIONS"

            ACCESS_TOKENS_URL=$(echo $INSTALLATIONS | jq -r --arg APP_ID "${{ secrets.GITHUB_APP_ID }}" '.[] | select(.app_id == (${{ secrets.GITHUB_APP_ID }} | tonumber)).access_tokens_url')
            echo "access tokens url for getting installation token from gh api post request: $ACCESS_TOKENS_URL"
            echo "ACCESS_TOKENS_URL=$ACCESS_TOKENS_URL" >> $GITHUB_ENV

      - name: Get installation token for triggering workflow
        id: get-installation-token-for-triggering-workflow
        run: |
            INSTALLATION_TOKEN_RESPONSE=$(curl -X POST \
                -H "Authorization: Bearer ${{ env.JWT }}" \
                -H "Accept: application/vnd.github.v3+json" \
                ${{ env.ACCESS_TOKENS_URL }})

                INSTALLATION_TOKEN=$(echo $INSTALLATION_TOKEN_RESPONSE | jq -r '.token')
                echo "INSTALLATION_TOKEN=$INSTALLATION_TOKEN" >> $GITHUB_ENV
                echo "installation token response: $INSTALLATION_TOKEN_RESPONSE"

      - name: Trigger workflow
        id: trigger-workflow
        run: |
            curl -X POST \
                -H "Accept: application/vnd.github.v3+json" \
                -H "Authorization: token ${{ env.INSTALLATION_TOKEN }}" \
                https://api.github.com/repos/ghaccount/testing-multi-repo-wf/actions/workflows/i_want_this_wf_to_be_triggered.yml/dispatches \
                -d '{"ref":"main"}'
© www.soinside.com 2019 - 2024. All rights reserved.