如何从github操作中的文件中分配环境变量?

发布于 2025-02-12 22:11:11 字数 944 浏览 0 评论 0原文

我目前正在从事一个Django项目。我创建了一个github操作,以运行python manage.py test命令每次将代码推到main分支时。

这里的问题是,我的项目中有许多环境变量。我无法将ENV变量设置为每个变量的GitHub秘密。

我的存储库中有一个env.dev文件。我需要做的是,每当我推动代码时,都需要通过从env.dev文件中读取环境变量来分配环境变量。

有什么办法做到这一点吗?

这是我的django.yml用于github操作的文件。

name: Django CI

on:
  pull_request:
    branches: [ "main"]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.9]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run Tests
      run: |
        python manage.py test

I'm currently working on a Django project. I have create a GitHub action to run python manage.py test command everytime I push the code to main branch.

The problem here is, I have many environment variables in my project. I can't set env variables as GitHub secrets for each variables.

I have a env.dev file in my repository. What I need to do is, everytime I push the code, it needs to assign environment variables by reading it from the env.dev file.

Is there any way to do this?

This is my django.yml file used for GitHub actions.

name: Django CI

on:
  pull_request:
    branches: [ "main"]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.9]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run Tests
      run: |
        python manage.py test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2025-02-19 22:11:11

如“ 我如何使用github操作? “ https://github.com/marketplace/actions/dotenv-action” rel =“ nofollow noreferrer”> dotenv动作确实从本仓的根部读取.env文件,并提供环境变量来构建步骤。

但是这些并不是Github的秘密。
有一个 api ,这意味着,如果您的.env内容不经常变化,则可以脚本脚本秘密创建步骤。

As noted in "How do I use an env file with GitHub Actions?", you do have actions like Dotenv Action which do read a .env file from the root of this repo and provides environment variables to build steps.

But those would not be GitHub secret though.
There is an API to create secrets, which means, if your .env content does not change too often, you can script the secret creation step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文