gpt4 book ai didi

yaml - Github 操作矩阵包括基于 GitHub 事件有条件的条目

转载 作者:行者123 更新时间:2023-12-05 05:49:10 27 4
gpt4 key购买 nike

我目前有一个工作流程,在合并到 master 时触发部署,并确保在部署 prod 之前开发作业通过。总共有 4 个环境(2 个开发环境和 2 个生产环境),如果可能的话,我希望能够使用 github 矩阵减少代码重复并将作业数量减少到 1,因为作业之间唯一变化的是值环境变量。

我遇到的问题是似乎没有办法根据 github 事件有条件地包含或排除条目。如果有人有意见或知道一个好的解决方法,我将非常感激。

文件和内容如下。

申请.yaml:

name: Terraform Apply

on:
deployment:
workflow_dispatch:
inputs:
environment:
description: "name of the environment"
required: true
default: "dev"

env:
AWS_ACCESS_KEY_ID: ${{ secrets.KEY_ID_EXAMPLE }}
AWS_SECRET_KEY: ${{ secrets.KEY_EXAMPLE }}

jobs:
deploy-dev:
name: TF Apply - Dev
runs-on: ubuntu-latest
if: ${{ github.event.deployment.environment == 'dev' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'dev' || github.event.inputs.environment == 'all' }}
env:
TF_VAR_aws_provider_role_arn: arn:example
TF_VAR_example_api_key: ${{ secrets.EXAMPLE_API_KEY }}
TF_VAR_example_app_key: ${{ secrets.EXAMPLE_APP_KEY }}
TF_WORKSPACE: dev
steps:
- name: Run Terraform Apply Composite
uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
with:
ssh_key: ${{ secrets.EXAMPLE_SSH_KEY }}

deploy-dev-midwest:
name: TF Apply - Dev Midwest
runs-on: ubuntu-latest
if: ${{ github.event.deployment.environment == 'dev-midwest' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'dev-midwest' || github.event.inputs.environment == 'all' }}
env:
TF_VAR_aws_provider_role_arn: arn:example
TF_VAR_example_api_key: ${{ secrets.DEV_MIDWEST_API_KEY }}
TF_VAR_example_app_key: ${{ secrets.DEV_MIDWEST_APP_KEY }}
TF_WORKSPACE: dev-midwest
steps:
- name: Run Terraform Apply Composite
uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
with:
ssh_key: ${{ secrets.GH_ACTIONS_SSH_PRIVATE_KEY }}

deploy-prod-east:
name: TF Apply - Prod East
needs: deploy-dev
runs-on: ubuntu-latest
if: ${{ github.event.deployment.environment == 'prod-east' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'prod-east' || github.event.inputs.environment == 'all' }}
env:
TF_VAR_aws_provider_role_arn: arn:example
TF_VAR_example_api_key: ${{ secrets.PROD_EAST_API_KEY }}
TF_VAR_example_app_key: ${{ secrets.PROD_EAST_APP_KEY }}
TF_WORKSPACE: prod-east
steps:
- name: Run Terraform Apply Composite
uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
with:
ssh_key: ${{ secrets.GH_ACTIONS_SSH_PRIVATE_KEY }}

deploy-prod-midwest:
name: TF Apply - Prod Midwest
needs: deploy-dev-midwest
runs-on: ubuntu-latest
if: ${{ github.event.deployment.environment == 'prod-midwest' || github.event.deployment.environment == 'all' || github.event.inputs.environment == 'prod-midwest' || github.event.inputs.environment == 'all' }}
env:
TF_VAR_aws_provider_role_arn: arn:example
TF_VAR_example_api_key: ${{ secrets.PROD_MIDWEST_API_KEY }}
TF_VAR_example_app_key: ${{ secrets.PROD_MIDWEST_APP_KEY }}
TF_WORKSPACE: prod-midwest
steps:
- name: Run Terraform Apply Composite
uses: GetTerminus/example/multi-workspace/terraform-apply@1.0
with:
ssh_key: ${{ secrets.GH_ACTIONS_SSH_PRIVATE_KEY }}

部署.yaml:

name: Master Deployments

on:
push:
branches: [ master ]

jobs:
deploy-all:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Create Sequential Deployments
uses: chrnorm/deployment-action@releases/v1
with:
token: ${{ secrets.GH_DEPLOYMENT_ACCESS_TOKEN_EXAMPLE }}
environment: all

最佳答案

对于您的示例,矩阵解决方案如下所示:

name: Terraform Apply
on:
push:
inputs:
environment:
description: "name of the environment"
required: true
default: "dev"

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [dev, dev-midwest, prod-east, prod-midwest]
steps:
- name: Deploy
if: ${{ github.event.inputs.environment == 'all' || (matrix.environment == github.event.inputs.environment) }}
shell: bash
run: |
echo "Hello World !!! ${{ matrix.environment }}"

关于yaml - Github 操作矩阵包括基于 GitHub 事件有条件的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70697950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com