Run a script only from a specific branch

I set my trigger event easily like this:

on:
  push:
    branches:
        - V2.0

But I don’t know how to select the branch to work with !

So far I tried :

jobs:
  build:
    runs-on: windows-2019
    env:
      github.ref == 'refs/heads/V2.0'

Any ideas ?

What exactly are you trying to do? If you want a whole workflow to run only when that branch is pushed, the branch filter already does that.

If you want to filter at the job or step level checking github.ref is correct, but you’ll need an if condition, jobs.<job_id>.if or jobs.<job_id>.steps.if, respectively. The env keyword is for setting environment variables.

I want my workflow to run on the secondary branch, not in the master.

Do you mean you want to check out the V2.0 branch? There’s nothing to do, if you use actions/checkout it will check out the last commit of the push that triggered the event by default, and with the branch filter that will only ever be a commit on that branch.

I think I uderstand what you mean:

This the behaviour of the actions/checkout ?

There are three parts to it:

  1. actions/checkout will check out the commit named by $GITHUB_SHA by default, that is if you don’t give it additional parameters telling it to check out something else.

  2. The push event sets GITHUB_SHA like this:

Commit pushed, unless deleting a branch (when it’s the default branch)

  1. If you set a branch filter on push events only events on that branch will be used for the workflow. So in your example the workflow will only run if the commit GITHUB_SHA points at is part of the V2.0 branch.

Hi @Cristishor201,

To trigger the workflow on V2.0 branch, you should put the workflow yaml on V2.0 branch.
Using actions/checkout@v2 will automatically checkout the V2.0 branch content.

on:
  push:
    branches:
        - V2.0    # branch filter is not neccessary but if you put it here, it doesn't matter.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: run script
        run: |
          ./test.sh

If you put the workflow yaml on master branch, you cannot trigger it on V2.0 branch. But in this case, you can trigger the workflow in master branch(via a commit), and checkout V2.0 branch to run the script.

on: [push]   # remove branch filter here otherwise cannot be triggerred on master branch.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
        with:
          ref: V2.0   # checkout V2.0 branch content
      - name: run script
        run: |
          ./test.sh

Hope it’s clear and helpful!