如何在删除事件Github操作上提取分支名称

时间:2020-07-07 16:18:31

标签: github-actions

我正在尝试在删除事件中提取分支名称。原来它不在GITHUB_REF对象中,因为它将是default branch

通常我会跑步

- name: Extract branch name
  shell: bash
  run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  id: extract_branch

但是显然,对于删除事件,我需要通过${{ github.event.ref }}

提取分支
- name: Extract branch name
  shell: bash
  run: echo "##[set-output name=branch;]$(echo ${{ github.event.ref }})" # how to drop refs/heads/?
  id: extract_branch

现在我不知道如何删除分支名称的refs / heads。

编辑:由于在删除事件中,github.event.ref已经包含简单的分支名称,例如feature-1-my-branch而不是refs/heads/feature-1-my-branch我上面的示例代码有效。

如果要在此上下文中以其他事件类型进行一些后处理,其中github.event.ref返回refs/heads/feature-1-my-branch,在这种情况下我将如何丢弃refs/heads? / p>

1 个答案:

答案 0 :(得分:1)

您可以仅使用${{ github.event.ref }}来引用分支名称,GitHub API docs中记录了完整的delete事件有效负载。

我也自己做了一个测试。使用here中定义的工作流程。

    steps:
      - uses: actions/checkout@v2
      - name: run build
        run: |
          echo "GITHUB_SHA is ${{ github.sha }}"
          echo "GITHUB_REF is ${{ github.ref }}"
          echo "${{ github.event.ref }} - ${{ github.event.ref_type }}"

我可以通过推和删除branch来触发跑步(它也适用于tag)。这样会导致运行类似this

GITHUB_SHA is feb56d132c8142995b8fea6fd67bdd914e5e0d68
GITHUB_REF is refs/heads/master
so-62779643-test-delete-event-test2 - branch

[更新]

要去除GITHUB_REF中的前缀,请执行以下操作:

      - uses: actions/checkout@v2
      - name: run build
        run: |
          echo "::set-env name=GITHUB_REF::${{ github.ref }}"
          echo "old GITHUB_REF is $GITHUB_REF"
          GITHUB_REF=$(echo $GITHUB_REF | sed -e "s#refs/heads/##g")
          echo "new GITHUB_REF is $GITHUB_REF"

run log reference

old GITHUB_REF is refs/heads/master
new GITHUB_REF is master