首页 > 解决方案 > Environtment variable via github secrets

问题描述

I have a scala repo on github with has the scala work flow in scala.yml. 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. 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.

标签: githubgithub-actions

解决方案


您需要将它设置在环境变量中的设置中,您的工作流程如此给出。如果变量名称在Secrets工作MY_KEY流程中,您可以将其访问为{{ secrets.MY_KEY }}

例子

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

这将设置环境变量MY_KEY并且可以通过System.getenv("MY_KEY")


推荐阅读