简体   繁体   English

如何在 Github Actions 中使用机密?

[英]How do I use secrets in Github Actions?

I feel like this is a really stupid question but can't seem to figure it out.我觉得这是一个非常愚蠢的问题,但似乎无法弄清楚。 I have set up a really simple node.js project with a API_KEY as a secret.我已经建立了一个非常简单的 node.js 项目,并将API_KEY作为秘密。

In the nodejs action yml I have the following:nodejs 操作 yml我有以下内容:

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
      env:  
        API_KEY: ${{ secrets.API_KEY }} 
    - run: export 
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

API_KEY doesn't show in export as I would expect it to nor does it show when I console.log(process.env) . API_KEY不会像我期望的那样export显示,也不会在我console.log(process.env)

According to the documentation , this should work as-is. 根据文档,这应该按原样工作。 I feel like I'm missing something really simple.我觉得我错过了一些非常简单的东西。

This is not a fork as suggested in this stackoverflow question .这不是这个 stackoverflow question 中建议的分叉。

What am I missing to get the API_KEY available in my node script?在我的节点脚本中获取 API_KEY 缺少什么?

Environment variables can be defined on three levels:环境变量可以定义在三个级别:

The preference is given to the most specific variable available.优先考虑可用的最具体的变量。 For example:例如:

env:
  VAR: I am global
jobs:
  job1:
    steps:
      - run: echo "$VAR"  # "I am global"
  job2:
    env:
      VAR: I am on the job level
    steps:
      - run: echo "$VAR"  # "I am on the job level"
      - env:
          VAR: I am on the step level
        run: echo "$VAR"  # "I am on the step level"
      - run: echo "$VAR"  # "I am on the job level"

To set an environment variable in a step, dynamically, and make it available for further steps, you have to use an environment file (this has changed recently from using workflow commands, which are now deprecated for environment variables):要在一个步骤中动态设置环境变量,并使其可用于后续步骤,您必须使用环境文件最近已从使用工作流命令更改为环境变量,现在已弃用环境变量):

steps:
  - name: Set the variable
    run: echo "foo=bar" >> "$GITHUB_ENV"
  - name: Print the variable
    run: echo "$foo"  # bar

The old, now deprecated way would set it like this:旧的,现在已弃用的方式将其设置为:

run: echo "::set-env name=foo::bar"

but this now triggers a deprecation warning.但这现在会触发弃用警告。

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

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