首页 > 解决方案 > 如何在 JS 文件中使用 Github 机密

问题描述

我有一个基本的 git repo 设置了 github 操作来构建和部署(主要是 HTML 和 TS 文件)。

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

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

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

标签: javascriptbashgithubyamlgithub-actions

解决方案


您可以将 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 }}

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


推荐阅读