Change trigger branch without a code change

Context
We try to use git flow.
During our sprint we work on the develop branch and towards the end of the sprint we cut to release branch and make any last min changes into release branch. Meanwhile the rest of the the team keeps merging their PRs into develop. The GH action workflow watches the specified branch and deploys the code when we have a merge (into develop for example).

Problem
We want to change our trigger branch when it comes towards the end of the sprint to be triggered from release branch. We can do this by changing the branch specified in the yaml file but feels like an overkill. We would ideally like to do it from somehwere else like the workflow_dispatch GUI or using a label or something that requires minimal effort for the team.

Here is our stripped down yaml:

name: Build & Deploy
on:
  workflow_dispatch:
  push:
    branches:
      - develop # I want to change this dynamically
jobs:
  build:
    runs-on: ubuntu-latest
    - name: Install & build
      run: |
        echo "Deploy code..."

You can do what you want using:

Input works fine for manual workflow but I need something that will persist. i.e once the trigger branch has been changed I want it to stay like that for subsequent workflows triggered automatically.
For e.g if I was to use secrets as a config it would look something like this:

name: Build & Deploy
on:
  workflow_dispatch:
  push:
    branches:
      - ${{ secrets.BRANCH_TO_WATCH }} # I want to change this dynamically
....

I’d probably delete the branches constraint and then have a step that runs if it’s a push and checks to see if the branch is master or a specific file exists and sets an output. And then have the rest of the workflow run only if that output is set.

Then you can have the input workflow create that file on that branch and push.

Ahh that is exactly what I need. I added an if check in the build step and it works exactly as I need -
Thank you so much!
Here was my final solution for others that may come across this in the future.

name: Build & Deploy
on:
  workflow_dispatch:
  push:
jobs:
  build:
    env: ${{secrets.BRANCH_TO_WATCH}}
    runs-on: ubuntu-latest
    steps:
      - name: install & build
        if: ${{ contains(github.ref, env.BRANCH_TO_DEPLOY) }} # <-----
        run: ....
1 Like