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.
CodePudding user response:
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
CodePudding user response:
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."