如何在 JS 文件中使用 Github 机密

ant*_*vet 10 javascript bash yaml github github-actions

我有一个基本的 git 存储库,其中包含用于构建和部署的 github 操作(主要是 HTML 和 TS 文件)。

但是我必须在一些需要保密的 API 密钥中使用。

所以我想办法为他们使用 GITHUB SECRETS。

如何在我的 js(或 TS)文件中访问 GITHUB SECRETS,以便它可以正确地使用 github 操作进行构建?

sct*_*thi 13

您可以将 Secrets 作为 ENV 变量传入。

例子:

   ...
   steps:
      - name: Git checkout
        uses: actions/checkout@v2

      - name: Use Node 12.x
        uses: actions/setup-node@v1
        with:
          node-version: 12.x

      - name: Install Dependencies (prod)
        run: yarn install --frozen-lockfile --production

      - name: Run Tests (JEST)
        run: yarn test --ci --silent --testPathIgnorePatterns=experimental
        env:
          CI: true
          API_KEY: ${{ secrets.API_KEY }}
Run Code Online (Sandbox Code Playgroud)

在 Node.js 中,您可以通过process.env.API_KEY.