How to get branch name on GitHub action?

感情迁移 提交于 2020-12-31 06:20:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!