【问题标题】:How to deploy Github action pipeline with multiple branches in same YAML file using IF condition如何使用 IF 条件在同一 YAML 文件中部署具有多个分支的 Github 操作管道
【发布时间】:2022-07-25 16:03:35
【问题描述】:

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

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

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

因此,如果我推送到分支 1,它应该部署在 server1 中并且与所有服务器相​​同

为此,我使用 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 文件?

【问题讨论】:

  • 你应该仔细评估你的分支模型;您现在使用分支的方式是一种非常不好的做法。
  • 我知道它错了我不知道检查条件的确切条件语句。这就是为什么在流程中提出这个问题。仍在寻找合适的条件语句和语法来检查

标签: github yaml azure-pipelines github-actions cicd


【解决方案1】:

应该有效

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

【讨论】:

  • 语法正确但条件语句检查分支错误。一旦修改适当的条件语句将在评论中告知。感谢您的宝贵时间!
  • @jayaprakashR 可能您正在寻找github.ref。使用 Ctrl+F 找到这个“变量”。
【解决方案2】:

最后,我用正确的步骤构建了我的 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

【讨论】:

    【解决方案3】:

    这个 yaml 文件是否应该存在于每个分支中?或任何单个分支中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      相关资源
      最近更新 更多