|
| 1 | +# taken from OpenZipkin |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | +set -x |
| 5 | + |
| 6 | +build_started_by_tag() { |
| 7 | + if [ "${TRAVIS_TAG}" == "" ]; then |
| 8 | + echo "[Publishing] This build was not started by a tag, publishing snapshot" |
| 9 | + return 1 |
| 10 | + else |
| 11 | + echo "[Publishing] This build was started by the tag ${TRAVIS_TAG}, publishing release" |
| 12 | + return 0 |
| 13 | + fi |
| 14 | +} |
| 15 | + |
| 16 | +is_pull_request() { |
| 17 | + if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then |
| 18 | + echo "[Not Publishing] This is a Pull Request" |
| 19 | + return 0 |
| 20 | + else |
| 21 | + echo "[Publishing] This is not a Pull Request" |
| 22 | + return 1 |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +is_travis_branch_master() { |
| 27 | + if [ "${TRAVIS_BRANCH}" = master ]; then |
| 28 | + echo "[Publishing] Travis branch is master" |
| 29 | + return 0 |
| 30 | + else |
| 31 | + echo "[Not Publishing] Travis branch is not master" |
| 32 | + return 1 |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +check_travis_branch_equals_travis_tag() { |
| 37 | + #Weird comparison comparing branch to tag because when you 'git push --tags' |
| 38 | + #the branch somehow becomes the tag value |
| 39 | + #github issue: https://github.com/travis-ci/travis-ci/issues/1675 |
| 40 | + if [ "${TRAVIS_BRANCH}" != "${TRAVIS_TAG}" ]; then |
| 41 | + echo "Travis branch does not equal Travis tag, which it should, bailing out." |
| 42 | + echo " github issue: https://github.com/travis-ci/travis-ci/issues/1675" |
| 43 | + exit 1 |
| 44 | + else |
| 45 | + echo "[Publishing] Branch (${TRAVIS_BRANCH}) same as Tag (${TRAVIS_TAG})" |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +check_release_tag() { |
| 50 | + tag="${TRAVIS_TAG}" |
| 51 | + if [[ "$tag" =~ ^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]]; then |
| 52 | + echo "Build started by version tag $tag. During the release process tags like this" |
| 53 | + echo "are created by the 'release' Maven plugin. Nothing to do here." |
| 54 | + exit 0 |
| 55 | + elif [[ ! "$tag" =~ ^release-[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]]; then |
| 56 | + echo "You must specify a tag of the format 'release-0.0.0' to release this project." |
| 57 | + echo "The provided tag ${tag} doesn't match that. Aborting." |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +is_release_commit() { |
| 63 | + project_version=$(./mvnw help:evaluate -N -Dexpression=project.version|grep -v '\[') |
| 64 | + if [[ "$project_version" =~ ^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]]; then |
| 65 | + echo "Build started by release commit $project_version. Will synchronize to maven central." |
| 66 | + return 0 |
| 67 | + else |
| 68 | + return 1 |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +release_version() { |
| 73 | + echo "${TRAVIS_TAG}" | sed 's/^release-//' |
| 74 | +} |
| 75 | + |
| 76 | +safe_checkout_master() { |
| 77 | + # We need to be on a branch for release:perform to be able to create commits, and we want that branch to be master. |
| 78 | + # But we also want to make sure that we build and release exactly the tagged version, so we verify that the remote |
| 79 | + # master is where our tag is. |
| 80 | + git checkout -B master |
| 81 | + git fetch origin master:origin/master |
| 82 | + commit_local_master="$(git show --pretty='format:%H' master)" |
| 83 | + commit_remote_master="$(git show --pretty='format:%H' origin/master)" |
| 84 | + if [ "$commit_local_master" != "$commit_remote_master" ]; then |
| 85 | + echo "Master on remote 'origin' has commits since the version under release, aborting" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +#---------------------- |
| 91 | +# MAIN |
| 92 | +#---------------------- |
| 93 | + |
| 94 | +if ! is_pull_request && build_started_by_tag; then |
| 95 | + check_travis_branch_equals_travis_tag |
| 96 | + check_release_tag |
| 97 | +fi |
| 98 | + |
| 99 | +./mvnw install -nsu |
| 100 | + |
| 101 | +# If we are on a pull request, our only job is to run tests, which happened above via ./mvnw install |
| 102 | +if is_pull_request; then |
| 103 | + true |
| 104 | +# If we are on master, we will deploy the latest snapshot or release version |
| 105 | +# - If a release commit fails to deploy for a transient reason, delete the broken version from bintray and click rebuild |
| 106 | +elif is_travis_branch_master; then |
| 107 | + ./mvnw --batch-mode -s ./.settings.xml -Prelease -nsu -DskipTests deploy |
| 108 | + |
| 109 | + # If the deployment succeeded, sync it to Maven Central. Note: this needs to be done once per project, not module, hence -N |
| 110 | + if is_release_commit; then |
| 111 | + ./mvnw --batch-mode -s ./.settings.xml -nsu -N io.zipkin.centralsync-maven-plugin:centralsync-maven-plugin:sync |
| 112 | + fi |
| 113 | + |
| 114 | +# If we are on a release tag, the following will update any version references and push a version tag for deployment. |
| 115 | +elif build_started_by_tag; then |
| 116 | + safe_checkout_master |
| 117 | + ./mvnw --batch-mode -s ./.settings.xml -Prelease -nsu -DreleaseVersion="$(release_version)" -Darguments="-DskipTests" release:prepare |
| 118 | +fi |
| 119 | + |
0 commit comments