diff --git a/scripts/release.sh b/scripts/release.sh index 53d1379..fa21b6e 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -13,7 +13,7 @@ # $ ./scripts/release.sh patch # # NOTE: -# First time you have to create an annotated tag and commit the initial CHANGELOG, before creating issues or pull requests (if there these are not present it will fail) +# First time you have to create an annotated tag and commit the initial CHANGELOG, before creating issues or pull requests (if these are not present it will fail) # $ git tag --sign v0.0.0 --message 'Release v0.0.0' && git push --follow-tags # Bash strict mode @@ -44,7 +44,7 @@ REQUIRE_PULL_REQUEST="${REQUIRE_PULL_REQUEST:-false}" WAIT_FOR_CI_SUCCESS="${WAIT_FOR_CI_SUCCESS:-false}" ## [Boolean] Whether to create a GitHub release (defaults to `true`) ## requires GitHub Hub - https://github.com/github/hub) -PUBLISH_RELEASE="${PUBLISH_RELEASE:-true}" +PUBLISH_RELEASE="${PUBLISH_RELEASE:-$WRITE_CHANGELOG}" # Usage usage(){ @@ -101,8 +101,19 @@ get_semantic_version(){ PATCH="${semver[2]}" } +# Create a new branch for the changelog +create_release_branch(){ + if [[ "$REQUIRE_PULL_REQUEST" != 'true' ]]; then return; fi + + echo 'Create a new release branch' + RELEASE_BRANCH="release_${RELEASE//./_}" + git checkout -b "$RELEASE_BRANCH" +} + # Generate changelog generate_changelog(){ + if [[ "$WRITE_CHANGELOG" != 'true' ]]; then return; fi + if command -v docker >/dev/null 2>&1; then eval "docker run -it --rm -e CHANGELOG_GITHUB_TOKEN=${GITHUB_TOKEN} -v $(pwd):/usr/local/src/your-app ferrarimarco/github-changelog-generator --user ${GIT_REPO_OWNER} --project ${GIT_REPO}" "${@:-}" elif command -v github_changelog_generator >/dev/null 2>&1; then @@ -110,6 +121,57 @@ generate_changelog(){ fi } +# Commit CHANGELOG.md +commit_changelog(){ + if [[ "$WRITE_CHANGELOG" != 'true' ]]; then return; fi + if git diff --quiet HEAD; then return; fi + + echo 'Commit CHANGELOG' + git add CHANGELOG.md + git commit --gpg-sign --message "Update change log for ${RELEASE}" CHANGELOG.md +} + +# Wait for the CI job to return SUCCESS +wait_for_ci(){ + if [[ "$WAIT_FOR_CI_SUCCESS" != 'true' ]]; then return; fi + + echo 'Waiting for CI to finish' + while [[ $(hub ci-status "$RELEASE_BRANCH") != "success" ]]; do + sleep 5 + done +} + +# Merge release branch +merge_release_branch(){ + if [[ "$REQUIRE_PULL_REQUEST" != 'true' ]]; then return; fi + + echo 'Merge release branch' + git checkout "$INITIAL_BRANCH" + git merge --gpg-sign --no-ff --message "Release ${RELEASE}" "$RELEASE_BRANCH" +} + +# Tag release +tag_release(){ + echo "Tag ${RELEASE}" + git tag --sign "${RELEASE}" --message "Release ${RELEASE}" + git push --follow-tags +} + +# Publish GitHub release +publish_release(){ + if [[ "$PUBLISH_RELEASE" != 'true' ]]; then return; fi + + echo 'Publish release' + cat<