3

I have a GitHub action workflow where I am doing a merge of various different source branches into target branch main.

Here is a test workflow to read the values of env variables:

name: Print Github Env Variables
on:
  push:
    branches:
      - main
      - test
  pull_request:
    branches:
      - main
      - test
  workflow_dispatch:
jobs:
  print-env-variabes:
    name: Print Github Variables
    runs-on: ubuntu-latest
    steps:
      - name: Print Github Variables
        run: |
          echo "GITHUB_WORKFLOW:$GITHUB_WORKFLOW"
          echo "GITHUB_EVENT_NAME:$GITHUB_EVENT_NAME"
          echo "GITHUB_EVENT_PATH:$GITHUB_EVENT_PATH"
          echo "GITHUB_WORKSPACE:$GITHUB_WORKSPACE"
          echo "GITHUB_SHA:$GITHUB_SHA"
          echo "GITHUB_REF:$GITHUB_REF"
          echo "GITHUB_HEAD_REF:$GITHUB_HEAD_REF"
      - name: Print complete webhook event payload
        run: cat $GITHUB_EVENT_PATH

Create test.txt file in a branch called test, create a pull request then merge into main. You will see what I am seeing then.

When I look at GITHUB_REF it contains main branch.

For pull_requests GITHUB_HEAD_REF contains ‘test’, but for push it is empty. I need the value when the files get merged, so when a push occurs.

The test workflow also prints out the entire event json object, on push event I didn’t see any entry with the branch ‘test’ in there either.

How do I get the name of the source branch during a push event?

vy218
  • 707
  • 1
  • 8
  • 15
  • Does this answer your question? [How to get the target branch of the GitHub pull request from an Actions?](https://stackoverflow.com/questions/62331829/how-to-get-the-target-branch-of-the-github-pull-request-from-an-actions) – Lucas Oct 20 '20 at 05:10
  • You can get the anchestors of the commut using `HEAD^1` and `HEAD^2`. – dan1st Oct 20 '20 at 05:12
  • 1
    Thanks @lucas for the link but I want the source branch name, not the target branch name. – vy218 Oct 20 '20 at 05:38
  • 1
    Pushes do not have a source branch name. They have only a commit hash ID. – torek Oct 20 '20 at 13:42
  • Thanks @torek - is there a way to determine the source branch from the hash ID? – vy218 Oct 25 '20 at 18:24
  • In the fully general case: no. There may not *be* a source branch name! If there is a source branch name, it's in some *other Git repository* anyway. Their names are not your names, so don't try to use them. – torek Oct 25 '20 at 18:34
  • @torek you can use -r to limit your list of branches only to the remote ones: `git branch -r --contains ${GITHUB_SHA}` – Valentin Genev Dec 30 '20 at 13:13
  • 1
    @ValentinGenev: that's true, but irrelevant to this question. Remote-tracking names exist in your own repository after they have been created by `git fetch`, and the question is about `git push` and github "actions" (with those "action" things being defined by GitHub, not by Git). If the repository in which the action runs has remote-tracking names, some of them might contain the SHA, but some might not, and the set that does contain the name might not be the right set, or might have too many names to be useful. – torek Dec 31 '20 at 00:20
  • 1
    @vy218 I was struggling with similar situation this week. Instead of using the push event I opted out to `pull_request -> closed` which triggers right after the push but retains all the PR's context (`GITHUB_BASE_REF`, `GITHUB_HEAD_REF`). – Valentin Genev Dec 31 '20 at 14:26

2 Answers2

1

This works for me after a merge request:

export LATESTLOG=$(git log -1 --pretty=%B)
export SOURCEBRANCH=$(echo $LATESTLOG | awk -F '\\\\n' '{print $1}' |  awk -F '/' '{print $2}' | awk '{print $1}')
  • I used this and it worked great in one repo, but when I tried to use it in another repo it no longer worked. I don't know what exactly is different between them. I added a line to echo $LATESTLOG (in the repo where it doesn't work) and just got the latest commit message. Do you know what would cause this? – Donagh Aug 17 '22 at 14:12
-2
name: Test closed branch

on:
  pull_request:
  types: [closed]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Get PR closed number
        env:
          PR_NUMBER: ${{ toJson(github.event.number) }}
        run: |
          echo "$PR_NUMBER"
utiq
  • 1,342
  • 2
  • 17
  • 33