github-actions - 在单个操作中使用两个不同的 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 上的一些开发人员建议在秘密名称中使用前缀,例如
- PROD_DB_PASS
- STG_DB_PASS
但是有没有更好更清洁的方法来做到这一点?
解决方案
就像您提到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 }}
同意有很多重复的代码,但我认为它比基于以前的工作动态传递环境更简单、更清晰、更可靠。
推荐阅读
- nattable - 支持撤消和重做操作
- python - 用 Python 打字
- javascript - 未捕获的类型错误:无法读取 javascript 上未定义的属性“样式”
- python - 使用 Pandas 将列复制到文件时遇到问题
- python - 使用 pyqtgraph 和线程的实时绘图
- php - Laravel 无法路由到 /admin
- php - 在 laravel 中使用多个翻译文件时出现 htmlspecialchars 错误
- html - 如何让一个 div 出现在另一个 div 内而不是在其下方?
- laravel-5 - Laravel 5.8 显示 404 Not Found 错误
- c++ - 运行时检查失败 #0。不知如何使用CDECL