Search code examples
githubyamlazure-pipelinesgithub-actionscicd

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


I am going to setup github action pipeline to deploy code into server via azure CLI and azure run command.

Here i have many branches in same repository and i need to deploy the code to corresponding server for each branch

eg. repo if push branch1 --> deploy in server 1
if push branch2 --> deploy in server 2

So if i push to branch1 that should deploy in server1 and same as for all servers

For this i created YAML file using if condition but i don't know whether it will work or not. I referred many document but cannot get the solution for this scenario

Here is my YAML file

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

Can anyone please guide me how to configure yaml file for this scenario?


Solution

  • Finally I build my YAML file with proper steps. "github.ref == 'value'" is the syntax for check the branch. Below i mentioned my simplified code for reference if anyone wants same logic.

    As per @David Slutsky syntax also works.

    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