1

Edit: Question makes no sense, the branch which triggered the workflow always gets checked out per default.

Is there a way to get the name of the branch which started the workflow in Github Actions? For example, if I push to test/1, I'd like to use this name in my workflow.

Preferably before checking out the repo since I want to check out the branch which triggered the workflow. And I can't just specify the branch myself since I have multiple branches named test/* and I want my workflow to handle things dynamically in one workflow without creating a seperate workflow for each branch name.

Example:

name: Workflow

on:
  push:
    branches: [ test/* ]

jobs:
  build:
    runs-on: [ self-hosted ]
    steps:
      - name: Checkout the code
        uses: actions/checkout@v2
        with:
          ref: $branch_name <--- this is where I want to use the branch name for the branch which triggered the workflow on push
          fetch-depth: 0
Nigel-Lee
  • 145
  • 1
  • 13
  • 1
    I think if you don't specify any ref the default is the branch that triggered the workflow – Matteo Mar 29 '22 at 16:23
  • 1
    Unfortunately not, the default without any ref returns the default branch of the repo (i.e. main/master) – Nigel-Lee Mar 29 '22 at 16:27
  • try with `github.ref` – Matteo Mar 29 '22 at 16:30
  • 1
    BTW the [doc](https://github.com/actions/checkout#usage) says `The branch, tag or SHA to checkout. When checking out the repository that triggered a workflow, this defaults to the reference or SHA for that event.` – Matteo Mar 29 '22 at 16:38
  • 1
    `Unfortunately not, the default without any ref returns the default branch of the repo (i.e. main/master) – ` - no, that's not true. Using `actions/checkout` without any arguments checks out the current branch. If you think that's not true, please create a new workflow that checks out the repository and shows the branch. After that, open a new question to stackoverflow so we can see what's going on :) – rethab Mar 30 '22 at 06:28
  • I checked again yesterday and you are both right. The corresponding branch gets checked out per default. Thanks guys! – Nigel-Lee Mar 30 '22 at 06:42
  • this looks only correct if you have 2 workflows. when you chain 3 workflows, first and second checkouts the branch which triggered the workflows, but 3rd one checkouts to default branch (main or master) – aykcandem Nov 16 '22 at 10:18
  • this could help `github.event.push.ref` https://stackoverflow.com/questions/64781462/github-actions-default-branch-variable/72080588#72080588 – rofrol Jul 10 '23 at 09:04

0 Answers0