Home > Article > Development Tools > how to get branch name in github actions
Branch names in GitHub Actions can be retrieved using the github.ref
context variable. This variable contains the full Git reference of the current workflow run, which includes the branch name.
To extract the branch name from the github.ref
, you can use the following pattern:
<code>branchName = github.ref.split("/").pop()</code>
This pattern will split the Git reference into individual components, and the last component is the branch name.
The best method to retrieve the branch name in GitHub Actions depends on your specific requirements. If you need the branch name in the workflow file itself, you should use the github.ref
context variable.
Alternatively, you can also use the gh
GitHub CLI tool, which is pre-installed in GitHub Actions runners. To retrieve the branch name using gh
, you can use the following command:
<code>gh branch --show-current</code>
Apart from the methods mentioned above, there are a few additional ways to obtain the branch name in GitHub Actions:
actions/checkout
action: The actions/checkout
action can be used to check out the repository code at a specific branch. The GITHUB_REF
environment variable set by this action contains the full Git reference, which includes the branch name.payload.ref
property: If a GitHub Actions workflow is triggered by a push event, the payload.ref
property of the github
context variable contains the full Git reference, including the branch name.GITHUB_HEAD_REF
environment variable: The GITHUB_HEAD_REF
environment variable is set by GitHub Actions when a workflow is triggered by a pull request. It contains the branch name of the pull request head.The above is the detailed content of how to get branch name in github actions. For more information, please follow other related articles on the PHP Chinese website!