From 05e6a03da848ecc0081a23d934b9c35d34564859 Mon Sep 17 00:00:00 2001 From: Mustapha Zorgati Date: Mon, 10 Sep 2018 13:52:00 +0200 Subject: [PATCH] TSK-657: now incrementing and pushing pom files to repo after releasing --- .travis.yml | 10 +++-- ci/change_version.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++ ci/commitPoms.sh | 79 +++++++++++++++++++++++++++++++++++++ ci/release.sh | 8 ++-- 4 files changed, 182 insertions(+), 8 deletions(-) create mode 100755 ci/change_version.sh create mode 100755 ci/commitPoms.sh diff --git a/.travis.yml b/.travis.yml index 8175eb5..f5d40af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,18 +4,20 @@ jdk: stages: - Build - - "Release / Deploy" + - Release env: global: - - DEPLOY_REPO=MiguelMartinRojas/TaskanaSimpleHistory + - DEPLOY_REPO=Taskana/TaskanaSimpleHistory jobs: include: - stage: Build script: - mvn clean install -q -f lib -DskipTests -Dmaven.javadoc.skip=true - - stage: "Release / Deploy" + - stage: Release script: - ci/release.sh lib/taskana-history-plugin $TRAVIS_TAG - if: repo = env(DEPLOY_REPO) AND (tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ OR branch = TSK-657) AND type != pull_request + && ci/change_version.sh -i -m "lib/" + && ci/commitPoms.sh + if: repo = env(DEPLOY_REPO) AND (tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ OR branch = master) AND type != pull_request diff --git a/ci/change_version.sh b/ci/change_version.sh new file mode 100755 index 0000000..7631519 --- /dev/null +++ b/ci/change_version.sh @@ -0,0 +1,93 @@ +#!/bin/bash +set -e #fail fast + +#H Usage: +#H change_version.sh -h | change_version.sh --help +#H +#H prints this help and exits +#H +#H change_version.sh <-m modules...> [-i] +#H +#H if a release version exists (extracted from TRAVIS_TAG) +#H the maven versions of all modules will be changed to the given release version. +#H +#H module: +#H directory of a maven project +#H i: +#H increments version +#H +#H Environment variables: +#H - TRAVIS_TAG +#H if this is a tagged build then TRAVIS_TAG contains the version number. +#H pattern: v[DIGIT].[DIGIT].[DIGIT] +# Arguments: +# $1: exitcode +function helpAndExit { + cat "$0" | grep "^#H" | cut -c4- + exit "$1" +} + +# takes a version (without leading v) and increments its +# last number by one. +# Arguments: +# $1: version (without leading v) which will be patched +# Return: +# version with last number incremented +function increment_version() { + if [[ ! "$1" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then + echo "'$1' does not match tag pattern." >&2 + exit 1; + fi + echo "${1%\.*}.`expr ${1##*\.*\.} + 1`" +} + +# changing version in pom and all its children +# Arguments: +# $1: directory of pom +# $2: new version +function change_version { + mvn versions:set -f "$1" -DnewVersion="$2" -DartifactId=* -DgroupId=* versions:commit +} + +function main { + [[ $# -eq 0 || "$1" == '-h' || "$1" == '--help' ]] && helpAndExit 0 + + while [[ $# -gt 0 ]]; do + case $1 in + -i) + INCREMENT="true" + shift # past argument + ;; + -m|--modules) + if [[ -z "$2" || "$2" == -* ]]; then + echo "missing parameter for argument '-m|--modules'" >&2 + exit 1 + fi + MODULES=($2) + shift # past argument + shift # past value + ;; + *) # unknown option + echo "unknown parameter $1" >&2 + exit 1 + ;; + esac + done + + if [[ ${#MODULES[@]} -eq 0 ]]; then + echo "Can not perform deployment without any modules" >&2 + helpAndExit 1 + fi + + if [[ "$TRAVIS_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + version=`[[ -n "$INCREMENT" ]] && echo $(increment_version "${TRAVIS_TAG##v}")-SNAPSHOT || echo "${TRAVIS_TAG##v}"` + for dir in ${MODULES[@]}; do + change_version "$dir" "$version" + done + + else + echo "skipped version change because this is not a release build" + fi +} + +main "$@" diff --git a/ci/commitPoms.sh b/ci/commitPoms.sh new file mode 100755 index 0000000..f269c5b --- /dev/null +++ b/ci/commitPoms.sh @@ -0,0 +1,79 @@ +#!/bin/bash +set -e # fail fast + +#H Usage: +#H commitPoms.sh -h | commitPoms.sh --help +#H +#H prints this help and exits +#H +#H commitPoms.sh [additional files...] +#H +#H commits and pushes all *.pom files (+ additional files) +#H +#H Requirements: +#H current commit is a HEAD commit +#H GH_TOKEN - github access token +#H GH_USER - username for the github access token +#H TRAVIS_TAG (format v[0-9]+\.[0-9]+\.[0-9]+) +#H TRAVIS_REPO_SLUG - repo name (in form: owner_name/repo_name) +# Arguments: +# $1: exit code +function helpAndExit { + cat "$0" | grep "^#H" | cut -c4- + exit "$1" +} + +# takes a version (without leading v) and increments its +# last number by one. +# Arguments: +# $1: version (without leading v) which will be patched +# Return: +# version with last number incremented +function increment_version() { + if [[ ! "$1" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then + echo "'$1' does not match tag pattern." >&2 + exit 1; + fi + echo "${1%\.*}.`expr ${1##*\.*\.} + 1`" +} + +function main { + [[ "$1" == '-h' || "$1" == '--help' ]] && helpAndExit 0 + [[ -z "$GH_USER" || -z "$GH_TOKEN" || -z "$TRAVIS_REPO_SLUG" ]] && helpAndExit 1 + if [[ "$TRAVIS_TAG" =~ v[0-9]+\.[0-9]+\.[0-9]+ ]]; then + #check if tagged commit is a head commit of any branch + commit=`git ls-remote -q -t origin | grep "$TRAVIS_TAG" | cut -c1-40` + branch=`git ls-remote -q -h origin | grep "$commit" | sed "s/$commit.*refs\/heads\///"` + + if [[ -z "$commit" || -z "$branch" ]]; then + echo "the commit '$commit' of tag '$TRAVIS_TAG' is not a head commit. Can not release" >&2 + exit 1 + fi + + if [[ `echo "$branch" | wc -l` != '1' ]]; then + echo "can not match commit '$commit' to a unique branch." >&2 + echo "Please make sure, that the tag '$TRAVIS_TAG' is the head of a unique branch" >&2 + echo "Branches detected: $branch" + exit 1 + fi + + #commit all poms + git checkout -b "$branch" + #to compensate new updates + git pull + git add "./*pom.xml" + for file in "$@"; do + [[ -n "$file" ]] && git add "$file" + done + git commit -m "Updated poms to version `increment_version ${TRAVIS_TAG##v}`-SNAPSHOT" + + #push poms (authentication via GH_TOKEN) + git remote add deployment "https://$GH_USER:$GH_TOKEN@github.com/$TRAVIS_REPO_SLUG.git" + git push --quiet --set-upstream deployment "$branch" + else + echo "Nothing to push - this is not a release!" + fi + +} + +main "$@" diff --git a/ci/release.sh b/ci/release.sh index 230c5bf..1f9f36f 100755 --- a/ci/release.sh +++ b/ci/release.sh @@ -28,8 +28,8 @@ set -e # fail fast #H * codesigning.asc.enc #H have to exist in the same folder as this script #H - the environment variables -#H * encrypted_3a2bac31b42b_key -#H * encrypted_3a2bac31b42b_iv +#H * encrypted_a4551539cb8c_key +#H * encrypted_a4551539cb8c_iv #H have to exist (in order to decode codesigning.asc.enc) # Arguments: # $1: exit code @@ -40,8 +40,8 @@ function helpAndExit { # decripting gpg keys and importing them (needed to sign artifacts) # Global: -# $encrypted_3a2bac31b42b_key: decription key -# $encrypted_3a2bac31b42b_iv: initialisation vector +# $encrypted_a4551539cb8c__key: decription key +# $encrypted_a4551539cb8c__iv: initialisation vector # Arguments: # $1: basedir function decodeAndImportKeys {