在 Github 工作流程中的作业之间传递机密作为输出

Tar*_*rta 2 bash github jwt github-actions

我试图在作业之间传递 JWT 令牌,但有些东西阻止它正确传递。根据文档,如果我想在作业之间传递变量,我需要outputs按照此处的说明使用。我正在做的事情如下:

name: CI
on:
  pull_request:
    branches:
      - main
jobs:
  get-service-url:
    ...does something not interesting to us...
  get-auth-token:
    runs-on: ubuntu-latest
    outputs:
      API_TOKEN: ${{ steps.getauthtoken.outputs.API_TOKEN }}
    steps:
      - name: Get Token
        id: getauthtoken
        run: |
          API_TOKEN:<there is a full JWT token here>
          echo -n "API_TOKEN=$API_TOKEN" >> $GITHUB_OUTPUT
  use-token:
    runs-on: ubuntu-latest
    needs: [get-service-url,get-auth-token]
    name: Run Tests
    steps:
      - uses: actions/checkout@v3
      - name: Run tests
        run: |
          newman run ${{ github.workspace }}/tests/collections/my_collection.json --env-var "service_url=${{needs.get-service-url.outputs.service_URL}}" --env-var "auth_token=${{needs.get-auth-token.outputs.API_TOKEN}}"
Run Code Online (Sandbox Code Playgroud)

因此,在运行期间,在我的输出中我看到:

Run newman run /home/runner/work/my-repo/my-repo/tests/collections/my_collection.json  --env-var "service_url=https://test.net" --env-var "auth_token="
Run Code Online (Sandbox Code Playgroud)

起初我认为在作业之间传递令牌本身有问题。因此,我尝试放置一个虚拟令牌并将其导出到输出中。在我的get-auth-token工作中,对输出的调用变成了:

echo -n "API_TOKEN=test" >> $GITHUB_OUTPUT
Run Code Online (Sandbox Code Playgroud)

在日志中我看到了它:

--env-var "auth_token=test"
Run Code Online (Sandbox Code Playgroud)

所以我在工作中传递它的方式很好。此外,令牌存在并且是正确的,因为我硬编码了一个令牌以简化我的测试。事实上,如果在我的get-auth-token工作中我尝试echo $API_TOKEN在日志中看到***这让我明白 Github 正确地混淆了它。然后我试着不要在工作之间传递它。因此,我在命令之前创建了相同的硬编码令牌,并在直接和 tada!newman run中引用它!newman run现在的日志是:

Run newman run /home/runner/work/my-repo/my-repo/tests/collections/my_collection.json  --env-var "service_url=https://test.net" --env-var "auth_token=***"
Run Code Online (Sandbox Code Playgroud)

所以令牌就在那里!但我需要它来自另一份工作。有一些东西阻止令牌在作业之间传递,我不知道如何实现这一点。

Tar*_*rta 5

找到了一个技巧来实现这一点。包括暂时“混淆”Github 眼中的秘密。

在我检索秘密的工作中,我对其进行编码并将其导出到GITHUB_OUTPUT

API_TOKEN_BASE64=`echo -n <my_secret> | base64 -w 0`
echo -n "API_TOKEN=$API_TOKEN_BASE64" >> $GITHUB_OUTPUT
Run Code Online (Sandbox Code Playgroud)

在我需要秘密的工作中,我对其进行解码(并在需要时使用):

API_TOKEN=`echo -n ${{needs.get-auth-token.outputs.API_TOKEN}} | base64 --decode`
Run Code Online (Sandbox Code Playgroud)

  • 注意:这将在日志中打印您的编码秘密,任何人都可以对其进行解码! (2认同)