Skip to content

Instantly share code, notes, and snippets.

@Jaskaranbir
Last active March 16, 2023 14:00
Show Gist options
  • Save Jaskaranbir/d5b065173b3a6f164e47a542472168c1 to your computer and use it in GitHub Desktop.
Save Jaskaranbir/d5b065173b3a6f164e47a542472168c1 to your computer and use it in GitHub Desktop.
Shell script to create GitHub releases with automatically generated changelogs (using github-changelog-generator).
#!/bin/bash
# ===> Set these variables first
branch="$GIT_BRANCH"
# Example: "Jaskaranbir/MyRepo"
repo_slug="$TRAVIS_REPO_SLUG"
token="$GITHUB_TOKEN"
version="$TRAVIS_TAG"
# An automatic changelog generator
gem install github_changelog_generator
LAST_REVISION=$(git rev-list --tags --skip=1 --max-count=1)
LAST_RELEASE_TAG=$(git describe --abbrev=0 --tags ${LAST_REVISION})
# Generate CHANGELOG.md
github_changelog_generator \
-u $(cut -d "/" -f1 <<< $repo_slug) \
-p $(cut -d "/" -f2 <<< $repo_slug) \
--token $token \
--since-tag ${LAST_RELEASE_TAG}
body="$(cat CHANGELOG.md)"
# Overwrite CHANGELOG.md with JSON data for GitHub API
jq -n \
--arg body "$body" \
--arg name "$version" \
--arg tag_name "$version" \
--arg target_commitish "$branch" \
'{
body: $body,
name: $name,
tag_name: $tag_name,
target_commitish: $target_commitish,
draft: false,
prerelease: false
}' > CHANGELOG.md
echo "Create release $version for repo: $repo_slug, branch: $branch"
curl -H "Authorization: token $token" --data @CHANGELOG.md "https://api.github.com/repos/$repo_slug/releases"
@simbo1905
Copy link

Hello,
This is script is really helpful. I noticed that it can be improved by getting LAST_RELEASE_TAG from https://api.github.com/repos/$TRAVIS_REPO_SLUG/releases/latest. Here is a link to our version which has that change https://github.com/sobolevn/git-secret/blob/8e426ceb99355740561f35c8a7398967ff9368e7/.ci/github_release_script.sh
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment