git rev-list not working inside github action
Asked Answered
M

2

6

I have a github action that is triggered when a release is created.

The goal is to:

  • Find the tag on which the release was based
  • Find the previous tag on that branch

So far, I have this:

CURRENT_TAG=${{ github.event.release.tag_name }}
PENULTIMATE_TAG=$(git describe --exact-match --tags `git rev-list --tags --max-count=1 --skip=1`)

These are then echoed out for testing. The first works correctly, but the second always returns empty. I have tried the exact same command locally, and it returns the second most recent tag, as expected.

I have no idea why it wouldn't work in a github action. For the sake of debugging, I removed the git describe component, confirming that it is in fact git rev-list that is returning nothing.

Merthiolate answered 6/8, 2020 at 12:53 Comment(2)
Hint: shallow clone. Increase its depth or disable shallow clone completely.Keir
@Keir Ah, found it, thank you!Merthiolate
D
12

Need to fetch all history by passing the fetch-depth option:

 - uses: actions/checkout@v2
      with:
        fetch-depth: 0
Decathlon answered 29/11, 2020 at 1:24 Comment(2)
It's really helped!.Reams
lol lifesaver! i spent hours trying to debug this xDLinsk
A
1

While using actions/checkout with fetch-depth: 0 will work, you might notice during the action run it grabs a bit more than you thought or need, from the checkout docs:

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags.

So in larger repositories "fetch all history for all branches and tags" could be a lot, and a bit of overkill for the usecase.

If you only need one branches' tags or commits, try just fetching that branch. I found the --unshallow git fetch option to be needed for my case well.

git fetch --unshallow origin release_branch

OP is using a release event type from github to trigger their workflow so I'm assuming their branch name would be static. If you were using a pull_request event, the base (target) branch of the pull request can be found with: ${{ github.base_ref }}

Assignation answered 8/10 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.