How to generate Release Notes / Changelog from Git Commits since last git tag?

Can be done with a simple Bash one-liner:

git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%h %s"

Do you want to expose this as an environment variable, so that you can send it to Slack / attach as release notes? Just add a Script step to your workflow (after the git clone step of course :wink: ) with the content:

#!/bin/bash
set -ex

# fetch tags, to be sure we have all the require info
git fetch --tags

# collect the commits since the last tag
export GIT_RELEASE_NOTES="$(git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%h %s")"

# expose as env var for subsequent build steps
envman add --key GIT_RELEASE_NOTES --value "$GIT_RELEASE_NOTES"

After this Script step you can access the generated git release in the $GIT_RELEASE_NOTES env var, which can be used in other steps’ input, e.g. in a Slack message or similar.

The generated release note will look something like this (commit hash + first line of the commit message):

4909e29 test output format fix
de07079 release notes test script
8bff3f3 gitignore .bitrise.secrets.yml

Happy Building! :slight_smile:

3 Likes

Note: if all you want to get is the last/current git commit message then you can use:

git log -1 --pretty=format:"%h %s"

instead of git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%h %s".

Example:

#!/bin/bash
set -ex

# get only the last commit
export GIT_RELEASE_NOTES="$(git log -1 --pretty=format:"%h %s")"

# expose as env var for subsequent build steps
envman add --key GIT_RELEASE_NOTES --value "$GIT_RELEASE_NOTES"

@viktorbenei Thank you for sharing this useful information.

Initially, I was using https://www.bitrise.io/integrations/steps/generate-changelog, but in one case, my commits were missing in the changelog.

Also, I had the same problem with https://www.bitrise.io/integrations/steps/git-commit-changelog.

Case was:

  1. bugfix/bug -> commit fix 1 -> push -> tag created -> 1.0.0.100 -> (changelog ok)

  2. development -> dev commit 1 -> git push (in this step, no tags have been created)

  3. development -> dev commit 2 -> git push (in this step, no tags have been created)

  4. bugfix/bug -> commit fix 2 -> push -> tag created -> 1.0.0.101 -> (changelog ok)

  5. development -> run bitrise manually -> tag created -> 1.0.0.102 -> (change log is empty)

I’m not sure if this is expected behavior in these steps I was using initially or its bug.

Thank you

1 Like