首页 > 解决方案 > 在单个操作中使用两个不同的 GitHub 环境机密

问题描述

例如,我已经为登台和制作创建了 GitHub Environment Secrets

Enviroment > Production  > DB_PASS

Enviroment > Staging  > DB_PASS

在我的工作流程中

on:
 push:
    branches:
      - main
      - staging
jobs:
  deploy:
    environment: Production 
    name: Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Debug Secret 
      run: echo ${{ secrets.DB_PASS}}

当分支合并正在暂存时,我如何将环境更改为暂存,可能是通过使用 gihub.ref分支名称。我尝试environment使用从上一个作业动态传递值,needs.job1.outputs.output1但它不起作用。

我发现 Stackoverflow 上的一些开发人员建议在秘密名称中使用前缀,例如

但是有没有更好更清洁的方法来做到这一点?

标签: github-actions

解决方案


就像您提到github.ref的那样,这可能是最好的方法。

on:
 push:
    branches:
      - main
      - staging
jobs:
  deploy-production:
    environment: Production 
    name: Deploy production
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/main' }}
    steps:
      - name: Debug Secret 
        run: echo ${{ secrets.DB_PASS }}

  deploy-staging:
    environment: Staging 
    name: Deploy staging
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/staging' }}
    steps:
      - name: Debug Secret 
        run: echo ${{ secrets.DB_PASS }}

同意有很多重复的代码,但我认为它比基于以前的工作动态传递环境更简单、更清晰、更可靠。


推荐阅读