0

I am now designing the CICD pipeline and planned to trigger by tag and the source branch name would be needed as input.

The ${{ github.ref_name }} shows the source branch when it is a commit, but it shows the tag name when it is a tag creation. I know a tag is from a specific commit, and a commit is from a branch. I am wondering if there's any way to retrieve the source branch from tag directly, or commit from a tag then source branch from that commit.

Thanks.

Yuk Chan
  • 137
  • 2
  • 9
  • "I know a tag is from a specific commit, and a commit is from a branch."— no, this is wrong. Whatever led you to believe these things about Git commits needs fixing. – jthill Feb 03 '23 at 19:44
  • @jthill what is the correct concept? – Yuk Chan Feb 08 '23 at 19:20
  • https://stackoverflow.com/questions/58416014/how-can-i-find-the-first-commit-of-a-branch-with-jgit/58595799#58595799 – jthill Feb 08 '23 at 19:51

2 Answers2

2

Not with any env variable from GitHub, since the tag is only given and points to a commit, not a branch. You could look at the solutions given here: Identifying a tag belongs to which branch in git

Rob Bos
  • 1,320
  • 1
  • 10
  • 25
0

Finally I use this way to fetch the branch name.

run: |
  raw=$(git branch -r --contains ${{ github.ref }})
  branch=$(echo ${raw##*/} | tr [:upper:] [:lower:])
  echo "branch=$branch" >> $GITHUB_OUTPUT
  echo "Branch is $branch."
Yuk Chan
  • 137
  • 2
  • 9