【问题标题】:NodeJS/Heroku Application Error for reading env variables with Github Action使用 Github Action 读取环境变量的 NodeJS/Heroku 应用程序错误
【发布时间】:2021-01-24 12:56:39
【问题描述】:

最初,我有一个工作流设置的 git 存储库,以便在 git push 请求上上传一些 NodeJS 源代码,一切正常。但是我有一个 .env 文件中的 Steam API 密钥,我不想公开(完全删除 .env)所以我想使用 Github Secrets 来存储 STEAM_API_KEY (以及其他变量,例如作为 BASE_URL) 在工作流中使用的 yml 环境变量中,如下所示:

jobs:
  test:

    runs-on: ubuntu-latest
    
    env: 
      BASE_URL: ${{ secrets.BASE_URL }}
      STEAM_API_KEY: ${{ secrets.STEAM_API_KEY }}

    steps:
      - name: Checkout Repo v2
        uses: actions/checkout@v2
      
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
          
      - name: Running CI Installation
        run: npm ci
      - name: Running Application/Server Unit Tests
        run: npm test

然后我在我的代码中使用process.env.<variable_name>(在How can I use Github secrets in JS files 之后)访问了它们:

module.exports = new SteamAuth({
    realm: `${process.env.BASE_URL}/steam/user/auth`,
    returnUrl: `${process.env.BASE_URL}/steam/user/auth`,
    apiKey: process.env.STEAM_API_KEY
});

但是在 Heroku 上抛出了这个错误:

错误:缺少领域、returnURL 或 apiKey 参数。这些是必需的。

如果我只是将字符串直接硬编码到 realmreturnUrlapiKey 中,则不会发生这种情况。

在进一步排除故障时:

    var url1 = `${process.env.BASE_URL}/steam/user/auth`; // BASE_URL = "https://<app_name>.herokuapp.com"
    var url2 = "https://<app_name>.herokuapp.com/steam/user/auth";

    console.log(url1 === url2);
    console.log(url1);
    console.log(url2);

输出:

true
***/steam/user/auth
***/steam/user/auth

其中 url1 已加密 process.env.BASE_URL。但是 url2 也被加密了,因为它类似于 BASE_URL??这是 Github Actions 的缺陷吗?

在这一点上,我没有想法。我做错了什么,但不知道从这里去哪里。有人知道如何在 .js 代码中正确使用 Github 机密吗?

PS:Github 的秘密/工作流程对我来说非常陌生,请原谅我缺乏知识/理解。

【问题讨论】:

    标签: node.js git heroku github-actions steam


    【解决方案1】:

    我发现了问题: env 变量仅在运行 Github Action 时可用,NOT 在从 Heroku 执行时。

    仍然没有用url2解释最后一部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2021-09-24
      • 2021-10-08
      相关资源
      最近更新 更多