如何使用 IF 条件在同一 YAML 文件中部署具有多个分支的 Github 操作管道

How to deploy Github action pipeline with multiple branches in same YAML file using IF condition

我将设置 github 操作管道以通过 azure CLI 和 azure 运行 命令将代码部署到服务器。

这里我在同一个存储库中有很多分支,我需要为每个分支将代码部署到相应的服务器

例如。 repo if push branch1 --> 部署在服务器 1
如果推送 branch2 --> 在服务器 2 中部署

所以如果我推送到应该部署在 server1 中的 branch1 并且与所有服务器相​​同

为此,我使用 if 条件创建了 YAML 文件,但我不知道它是否有效。 我参考了很多文档,但无法找到这种情况的解决方案

这是我的 YAML 文件

name: deploy
on:
  push:
      branches: [ branch1, branch2, branch3 ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - name: Log in with Azure
        uses: azure/login@v1
        with:
          creds: '${{ secrets.AZURE_CREDENTIALS }}'
      if: ${{ push.branches == 'branch1' }}
      - name: 'Run az commands'
        run: |
           az list vm
      if: ${{ push.branches == 'branch2' }}
      - name: 'Run az commands'
        run: |
           az list vm
      if: ${{ push.branches == 'branch3' }}
      - name: 'Run az commands'
        run: |
           az list vm

任何人都可以指导我如何为这种情况配置 yaml 文件吗?

应该可以

name: deploy
on:
  push:
      branches: [ branch1, branch2, branch3 ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - name: Log in with Azure
        uses: azure/login@v1
        with:
          creds: '${{ secrets.AZURE_CREDENTIALS }}'
      - name: 'Run az commands on branch 1'
        if: ${{ github.ref == 'refs/heads/branch1' }}
        run: |
           az list vm
      - name: 'Run az commands on branch 2'
        if: ${{ github.ref == 'refs/heads/branch2' }}
        run: |
           az list vm
      - name: 'Run az commands on branch 3'
        if: ${{ github.ref == 'refs/heads/branch3' }}
        run: |
           az list vm

最后,我通过适当的步骤构建了我的 YAML 文件。 “github.ref == 'value'”是检查分支的语法。如果有人想要相同的逻辑,我在下面提到了我的简化代码以供参考。

根据@David Slutsky 语法也可以。

name: FFR-deploy
on:
  push:
      branches: [ Azure-pipeline, Azure-pipeline-devops ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
                
      - name: Log in with Azure
        uses: azure/login@v1
        with:
          creds: '${{ secrets.AZURE_CREDENTIALS }}'
      
      - name: 'Run on azure-pipeline branch'
        if: ${{ github.ref == 'refs/heads/Azure-pipeline' }}
        run: |
         az list vm
      
      - name: 'Run on azure-pipeline-devops branch'
        if: ${{ github.ref == 'refs/heads/Azure-pipeline-devops' }}
        run: |
         az list vm