问题
During a GitHub action, I'd like to know the name of the branch:
- for a push action: name of the current branch
- for a pull_request action: name of the target branch
I need a string like develop
, master
or feature/xxx
(and not refs/pull/…).
The ${{ github.ref }}
var gives me refs/heads/develop
. How can I get only the develop
?
回答1:
You can create a step output of the last part of GITHUB_REF
like the following.
on: push
jobs:
example:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set output
id: vars
run: echo ::set-output name=short_ref::${GITHUB_REF#refs/*/}
- name: Check output
run: echo ${{ steps.vars.outputs.short_ref }}
回答2:
You can get it from github
object like ${{ github.head_ref }}
回答3:
You can get the branch name using the following syntax -
name: build
on: [push, pull_request]
jobs:
dummy-fake:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Verify run id
run : echo Run Id $GITHUB_RUN_ID
- name: Output the branch name
run: echo $(echo $GITHUB_REF | cut -d'/' -f 3)
来源:https://stackoverflow.com/questions/60300169/how-to-get-branch-name-on-github-action