|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright (C) 2018 Pawel Krupa (@paulfantom) - All Rights Reserved |
| 4 | +# Permission to copy and modify is granted under the MIT license |
| 5 | +# |
| 6 | +# Script to automatically do a couple of things: |
| 7 | +# - generate a new tag according to semver (https://semver.org/) |
| 8 | +# - generate CHANGELOG.md by using https://github.com/skywinder/github-changelog-generator |
| 9 | +# - sync CHANGELOG with GitHub releases by using https://github.com/mattbrictson/chandler |
| 10 | +# |
| 11 | +# Tags are generated by searching for a keyword in last commit message. Keywords are: |
| 12 | +# - [patch] or [fix] to bump patch number |
| 13 | +# - [minor], [feature] or [feat] to bump minor number |
| 14 | +# - [major] or [breaking change] to bump major number |
| 15 | +# All keywords MUST be surrounded with square braces. |
| 16 | +# |
| 17 | +# Script uses git mechanisms for locking, so it can be used in parallel builds |
| 18 | +# |
| 19 | +# Requirements: |
| 20 | +# - GH_TOKEN variable set with GitHub token. Access level: repo.public_repo |
| 21 | +# - docker |
| 22 | +# - git-semver python package (pip install git-semver) |
| 23 | + |
| 24 | +# Exit when latest commit is tagged |
| 25 | +[[ $(git tag --points-at) ]] && exit 0 |
| 26 | + |
| 27 | +# Some basic variables |
| 28 | + |
| 29 | +GIT_USER="cloudalchemybot" |
| 30 | +ORGANIZATION=$(echo "$TRAVIS_REPO_SLUG" | awk -F '/' '{print $1}') |
| 31 | +PROJECT=$(echo "$TRAVIS_REPO_SLUG" | awk -F '/' '{print $2}') |
| 32 | +GALAXY_URL="https://galaxy.ansible.com/${ORGANIZATION}/${PROJECT#ansible-}" |
| 33 | + |
| 34 | +# Git config |
| 35 | +git config --global user.email "${GIT_MAIL}" |
| 36 | +git config --global user.name "${GIT_USER}" |
| 37 | +GIT_URL=$(git config --get remote.origin.url) |
| 38 | +GIT_URL=${GIT_URL#*//} |
| 39 | + |
| 40 | +# Generate TAG |
| 41 | +GIT_TAG=none |
| 42 | +echo "Last commit message: $TRAVIS_COMMIT_MESSAGE" |
| 43 | +case "${TRAVIS_COMMIT_MESSAGE}" in |
| 44 | + *"[patch]"*|*"[fix]"* ) GIT_TAG=$(git semver --next-patch) ;; |
| 45 | + *"[minor]"*|*"[feat]"*|*"[feature]"* ) GIT_TAG=$(git semver --next-minor) ;; |
| 46 | + *"[major]"*|*"[breaking change]"* ) GIT_TAG=$(git semver --next-major) ;; |
| 47 | + *) echo "Keyword not detected. Doing nothing" ;; |
| 48 | +esac |
| 49 | +if [ "$GIT_TAG" != "none" ]; then |
| 50 | + echo "Assigning new tag: $GIT_TAG" |
| 51 | + git tag "$GIT_TAG" -a -m "Automatic tag generation for travis build no. $TRAVIS_BUILD_NUMBER" |
| 52 | + git push "https://${GH_TOKEN}:@${GIT_URL}" --tags || exit 0 |
| 53 | +fi |
| 54 | + |
| 55 | +# Generate CHANGELOG.md |
| 56 | +git checkout master |
| 57 | +git pull |
| 58 | +docker run -it --rm -v "$(pwd)":/usr/local/src/your-app ferrarimarco/github-changelog-generator \ |
| 59 | + -u "${ORGANIZATION}" -p "${PROJECT}" --token "${GH_TOKEN}" \ |
| 60 | + --release-url "${GALAXY_URL}" \ |
| 61 | + --unreleased-label "**Next release**" --no-compare-link |
| 62 | + |
| 63 | +git add CHANGELOG.md |
| 64 | +git commit -m '[ci skip] Automatic changelog update' |
| 65 | + |
| 66 | +git push "https://${GH_TOKEN}:@${GIT_URL}" || exit 0 |
| 67 | + |
| 68 | +# Sync changelog to github releases |
| 69 | +if [ "$GIT_TAG" != "none" ]; then |
| 70 | + docker run -e CHANDLER_GITHUB_API_TOKEN="${GH_TOKEN}" -v "$(pwd)":/chandler -ti whizark/chandler push "${GIT_TAG}" |
| 71 | +fi |
0 commit comments