How to get branch name when deploying a release by pushing a tag

Problem to solve

I would like to somehow extract the branch name when I am running jobs when a tag is created.

A snippet from my CI/CD job:

build_job:
    image: xanderhendriks/stm32cubeide:7.0
    variables:
        GIT_SUBMODULE_STRATEGY: normal
    rules:
        - if: $CI_COMMIT_TAG # Run this job when a tag is created
    stage: build
    script:
        - mkdir binaries;
        - stm32cubeide --launcher.suppressErrors -nosplash -application org.eclipse.cdt.managedbuilder.core.headlessbuild -data /tmp/stm-workspace -import . -build my_project/Release

    after_script:
        - cp Release/${CI_PROJECT_NAME}.hex binaries/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}_${CI_COMMIT_REF_SLUG}.hex;
        - cd binaries
        - zip ${CI_PROJECT_NAME}-${CI_COMMIT_TAG}_${CI_COMMIT_REF_SLUG}_binaries.zip *.hex # zip all .hex files
        - cd ..
        - touch JOB_ID.txt
        - echo GE_JOB_ID1=$CI_JOB_ID >> JOB_ID.txt
        - cat JOB_ID.txt
        - ls -l
    artifacts:
        paths:
            - binaries/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}_${CI_COMMIT_REF_SLUG}_binaries.zip
        reports:
            dotenv: JOB_ID.txt

The job above is triggered when I create a tag. It supposed to build my project and zip the binaries and add them to the artifacts.

For example, when I am on my “main” branch and create/push 1.0.0 tag, the following zip is created:

my_project-1.0.0_1.0.0_binaries.zip

Which is not correct. Since I ran the pipeline from “main” branch, I want the zip to be named as following:

my_project-1.0.0_main_binaries.zip