简体   繁体   English

通过 github secrets 的环境变量

[英]Environtment variable via github secrets

I have a scala repo on github with has the scala work flow in scala.yml .我在 github 上有一个 scala scala.ymlscala.yml有 scala 工作流程。 One of my test requires a special key to be available.我的一项测试需要一个特殊的密钥才能使用。 But I can't hard code it into source code because of privacy reasons.但由于隐私原因,我无法将其硬编码为源代码。 So, I added the key to Secrets section in Settings .因此,我在Settings添加了Secrets部分的密钥。 The section states该部分指出

Secrets are environment variables that are encrypted and only exposed to selected actions. Anyone with collaborator access to this repository can use these secrets in a workflow.

Secrets are not passed to workflows that are triggered by a pull request from a fork.

But when I do try to get the key in my code(in the test) it comes out as blank.但是当我尝试在我的代码中(在测试中)获取密钥时,它显示为空白。

System.getenv("MY_KEY") //MY_KEY is added to the Secrets in Settings of the repo

Also, this workflow is NOT triggered by a pull request from a fork.此外,此工作流程不会由来自分叉的拉取请求触发。

You would need to set it in the set it in the environment variable your work flow as given here .您需要按照此处给出的工作流程将其设置在环境变量中的设置中。 If the variable name in Secrets is MY_KEY then in the work flow you can access it as {{ secrets.MY_KEY }}如果Secrets的变量名称是MY_KEY那么在工作流程中,您可以将其作为{{ secrets.MY_KEY }}

Example例子

    - name: Run tests
      env:
        MY_KEY: ${{ secrets.MY_KEY }}
      run: sbt test

This will set the environment variable MY_KEY and can be accessed via System.getenv("MY_KEY")这将设置环境变量MY_KEY并且可以通过System.getenv("MY_KEY")

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

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