繁体   English   中英

在 Github 工作流中的作业之间传递秘密作为 output

[英]Passing secrets as output between jobs in a Github workflow

我试图在作业之间传递一个 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}}"

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

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="

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

echo -n "API_TOKEN=test" >> $GITHUB_OUTPUT

在日志中我看到了它:

--env-var "auth_token=test"

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

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=***"

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

找到了实现这一目标的技巧。 包括暂时“混淆” Github 眼睛的秘密。

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

API_TOKEN_BASE64=`echo -n <my_secret> | base64 -w 0`
echo -n "API_TOKEN=$API_TOKEN_BASE64" >> $GITHUB_OUTPUT

在我需要秘密的工作中,我将其解码(并在需要的地方使用):

API_TOKEN=`echo -n ${{needs.get-auth-token.outputs.API_TOKEN}} | base64 --decode`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM