NodeJS/Heroku 使用 Github 操作读取环境变量的应用程序错误
NodeJS/Heroku Application Error for reading env variables with Github Action
最初,我有一个工作 git 回购和工作流程设置,以便在 git push
请求上上传一些 NodeJS 源代码,一切正常。但是我有一个 .env 文件中的 Steam API 密钥,我不想要 public(完全删除 .env)所以我想使用 Github Secrets 来存储 STEAM_API_KEY(以及其他变量,例如 BASE_URL)在工作流中使用的 yml env 变量中,如下所示:
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>
(在 之后)在我的代码中访问了它们:
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 上抛出这个错误:
Error: Missing realm, returnURL or apiKey parameter(s). These are required.
如果我只是将字符串直接硬编码到 realm、returnUrl 和 apiKey[,就不会发生这种情况=48=].
进一步排除故障后:
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 操作吗?
在这一点上我没主意了。我做错了什么,但不知道从这里去哪里。有人知道如何在 .js 代码中正确使用 Github 秘密吗?
PS: Github secrets/workflows 对我来说很陌生,请原谅我缺少 knowledge/understanding。
我解决了问题:
env 变量仅在 运行 Github 操作时可用,NOT 从 Heroku 执行时。
仍然没有用 url2
解释最后一部分
最初,我有一个工作 git 回购和工作流程设置,以便在 git push
请求上上传一些 NodeJS 源代码,一切正常。但是我有一个 .env 文件中的 Steam API 密钥,我不想要 public(完全删除 .env)所以我想使用 Github Secrets 来存储 STEAM_API_KEY(以及其他变量,例如 BASE_URL)在工作流中使用的 yml env 变量中,如下所示:
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>
(在
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 上抛出这个错误:
Error: Missing realm, returnURL or apiKey parameter(s). These are required.
如果我只是将字符串直接硬编码到 realm、returnUrl 和 apiKey[,就不会发生这种情况=48=].
进一步排除故障后:
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 操作吗?
在这一点上我没主意了。我做错了什么,但不知道从这里去哪里。有人知道如何在 .js 代码中正确使用 Github 秘密吗?
PS: Github secrets/workflows 对我来说很陌生,请原谅我缺少 knowledge/understanding。
我解决了问题: env 变量仅在 运行 Github 操作时可用,NOT 从 Heroku 执行时。
仍然没有用 url2