5

I have been working on GitHub Actions from quite some time now, and we have a designated workflow in place. The workflow has been written in yaml.

I am trying to put some if condition for the github.ref and that block of the yaml is being skipped on the run. It means - if the pull request is raised against X branch - that block of code should run.

Something like this:

 
      - name: Branch name check - Running only for DEV branch.. 
        if: ${{contains(github.ref, 'DEV*')}}
        uses: mathrix-education/sonar-scanner@master

        with:
          version: 4.2.0.1873 # required
          typescript: false
          scan: true

Can anyone help me out on this ?

riQQ
  • 9,878
  • 7
  • 49
  • 66
Rahul Ravikumar
  • 51
  • 1
  • 1
  • 2
  • How are your branches actually named? Probably you should just leave off the * and only use `if: ${{contains(github.ref, 'DEV')}}` to only runn the step if the branch name contains `DEV`. – riQQ Sep 09 '20 at 18:03
  • I am trying to make a generic yaml where it should be independent of the branch name. But when a PR is raised against that X branch - the checks should happen. ``` branches: - master - UAT - DEV* ``` Anything after DEV - it should pick it up. Eg: DEV2020. Something like this. – Rahul Ravikumar Sep 09 '20 at 18:09
  • You didn't really answer my question and have you tried my proposal (leaving off the `*`)? – riQQ Sep 09 '20 at 18:10
  • @riQQ: Yes - I did try. It is still getting skipped. – Rahul Ravikumar Sep 09 '20 at 18:31
  • Does my post answer your question? If yes, please accept it: https://stackoverflow.com/help/someone-answers – riQQ Sep 22 '20 at 07:02

2 Answers2

9

We ran into a similar thing when constructing a github workflow. The conditions we are working under:

  • We want step 1 to only run on pushes OR pull requests to develop.
  • We want step 2 to only run on pull requests to master.
  • Steps must be in a single workflow.yaml.

After some research, below is what we came up with. I found Github Actions to be very flexible, with a lot of options that Gitlab and Bitbucket don't have. But that is also the downside and makes it more complex to understand sometimes.

---
name: my-workflow
on:
  push:
    branches:
      - develop
  pull_request:
    branches:
      - develop
      - main
    types:
      - closed

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: checkout repo
        uses: actions/checkout@v1
      - name: step 1
        if: github.ref == 'refs/heads/develop' || github.event.pull_request.base.ref == 'develop'
        run: echo "Hello develop"
      - name: step 2
        if: github.event.pull_request.base.ref == 'master'
        run: echo "Hello master"

I would recommend not using if-statements on branches too often. This introduces inconsistencies between the deployments of different branches. For some use cases you may be better of using github environments (Enterprise only). For example, if you need to set different environment variables or secrets for different branches. Below is a simple example where you could have two github environments (develop and main) both containing different values for the secret MY_SECRET.

name: my-workflow
on:
  push:
    branches:
      - develop
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    name: Do something
    steps:
      - run: echo "Example using environments corresponding branch names"
    environment: ${{ github.ref == 'refs/heads/main' && 'main' || github.ref == 'refs/heads/develop' && 'develop' }}
    env:
      MY_SECRET: ${{ secrets.MY_SECRET }}
Cloudkollektiv
  • 11,852
  • 3
  • 44
  • 71
1

Use the following to check for the base of the pull request:

if: ${{contains(github.base_ref, 'DEV')}}

github.ref will contain something like refs/pull/1/merge for pull requests.

riQQ
  • 9,878
  • 7
  • 49
  • 66