UNPKG

5.26 kBapplication/x-shView Raw
1#!/usr/bin/env bash
2#
3## Find the next semantic version
4
5set -e
6set -o pipefail
7
8current_branch=$(git rev-parse --abbrev-ref HEAD)
9if [[ "${current_branch}" != 'production' ]] \
10 && [[ "${current_branch}" != 'master' ]] \
11 && [[ "${current_branch}" != 'develop' ]]; then
12 echo 'Not on either production, master or develop branch. Exiting....'
13 exit 1
14fi
15
16## Get last tags
17last_production_tag=$(git describe --first-parent --abbrev=0 production)
18last_master_tag=$(git describe --first-parent --abbrev=0 master)
19last_develop_tag=$(git describe --first-parent --abbrev=0 develop)
20echo Last Production Tag: ${last_production_tag}
21echo Last Master Tag: ${last_master_tag}
22echo Last Develop Tag: ${last_develop_tag}
23
24## Parse semantic version from production tag
25last_production_ver=( ${last_production_tag//-/ } )
26last_production_mmp_ver=( ${last_production_ver[0]//./ } )
27last_production_pre_ver=${last_production_ver[1]}
28last_production_maj_ver=${last_production_mmp_ver[0]}
29last_production_min_ver=${last_production_mmp_ver[1]}
30last_production_pat_ver=${last_production_mmp_ver[2]}
31
32## Parse semantic version from master tag
33last_master_ver=( ${last_master_tag//-/ } )
34last_master_mmp_ver=( ${last_master_ver[0]//./ } )
35last_master_pre_ver=${last_master_ver[1]}
36last_master_maj_ver=${last_master_mmp_ver[0]}
37last_master_min_ver=${last_master_mmp_ver[1]}
38last_master_pat_ver=${last_master_mmp_ver[2]}
39
40## Parse semantic version from develop tag
41last_develop_ver=( ${last_develop_tag//-/ } )
42last_develop_mmp_ver=( ${last_develop_ver[0]//./ } )
43last_develop_pre_ver=${last_develop_ver[1]}
44last_develop_maj_ver=${last_develop_mmp_ver[0]}
45last_develop_min_ver=${last_develop_mmp_ver[1]}
46last_develop_pat_ver=${last_develop_mmp_ver[2]}
47
48## Get counts of breaking changes, features and fixes since last production
49break_count=$(git rev-list ${last_production_tag}..HEAD --grep='^...\*' --count)
50feature_count=$(git rev-list ${last_production_tag}..HEAD --grep='^fea:' --count)
51fix_count=$(git rev-list ${last_production_tag}..HEAD --grep='^fix:' --count)
52iac_count=$(git rev-list ${last_production_tag}..HEAD --grep='^iac:' --count)
53echo Breaks: ${break_count}
54echo Features: ${feature_count}
55echo Fixes: ${fix_count}
56echo IaC Changes: ${iac_count}
57
58unset next_maj_ver
59unset next_min_ver
60unset next_pat_ver
61unset next_pre_ver
62if [[ "${current_branch}" == 'production' ]]; then
63 echo 'Promoting latest beta version to production....'
64 next_maj_ver=${last_master_maj_ver}
65 next_min_ver=${last_master_min_ver}
66 next_pat_ver=${last_master_pat_ver}
67else
68 if [[ "${current_branch}" == 'master' ]]; then
69 next_maj_ver=${last_develop_maj_ver}
70 next_min_ver=${last_develop_min_ver}
71 next_pat_ver=${last_develop_pat_ver}
72
73 if (( "${next_maj_ver}" == "${last_master_maj_ver}" )) \
74 && (( "${next_min_ver}" == "${last_master_min_ver}" )) \
75 && (( "${next_pat_ver}" == "${last_master_pat_ver}" )); then
76 echo 'Incrementing beta version....'
77 last_master_pre_ver_parts=( ${last_master_pre_ver//./ } )
78 last_master_pre_ver_num=${last_master_pre_ver_parts[1]}
79 next_pre_ver="beta.$((last_master_pre_ver_num + 1))"
80 else
81 echo 'Promoting latest alpha version to beta.1....'
82 next_pre_ver='beta.1'
83 fi
84 else
85 if [[ "${current_branch}" == 'develop' ]]; then
86 ## Find next mmp version
87 if [[ "${break_count}" -gt 0 ]]; then
88 ## Increment major version if breaking changes added
89 next_maj_ver=$((last_production_maj_ver + 1))
90 next_min_ver='0'
91 next_pat_ver='0'
92 else
93 next_maj_ver=${last_production_maj_ver}
94 if [[ "${feature_count}" -gt 0 ]]; then
95 ## Increment minor version if feature added
96 next_min_ver=$((last_production_min_ver + 1))
97 next_pat_ver='0'
98 else
99 next_min_ver=${last_production_min_ver}
100 if [[ "${fix_count}" -gt 0 ]] || [[ "${iac_count}" -gt 0 ]]; then
101 ## Increment patch version if fix added or iac changed
102 next_pat_ver=$((last_production_pat_ver + 1))
103 else
104 next_pat_ver=${last_production_pat_ver}
105 fi
106 fi
107 fi
108 fi
109
110 if (( "${next_maj_ver}" == "${last_production_maj_ver}" )) \
111 && (( "${next_min_ver}" == "${last_production_min_ver}" )) \
112 && (( "${next_pat_ver}" == "${last_production_pat_ver}" )); then
113 echo 'No breaking change, feature, fix or IaC change was found.'
114 echo 'Version bump is unnecessary.'
115 exit 1
116 fi
117
118 if (( "${next_maj_ver}" == "${last_develop_maj_ver}" )) \
119 && (( "${next_min_ver}" == "${last_develop_min_ver}" )) \
120 && (( "${next_pat_ver}" == "${last_develop_pat_ver}" )); then
121 echo 'Incrementing alpha version....'
122 last_develop_pre_ver_parts=( ${last_develop_pre_ver//./ } )
123 last_develop_pre_ver_num=${last_develop_pre_ver_parts[1]}
124 next_pre_ver="alpha.$((last_develop_pre_ver_num + 1))"
125 else
126 echo 'Creating new alpha.1 version....'
127 next_pre_ver='alpha.1'
128 fi
129 fi
130fi
131
132if [[ -z "${next_pre_ver}" ]]; then
133 export NEXT_VERSION="${next_maj_ver}.${next_min_ver}.${next_pat_ver}"
134else
135 export NEXT_VERSION="${next_maj_ver}.${next_min_ver}.${next_pat_ver}-${next_pre_ver}"
136fi
137echo Next Version: ${NEXT_VERSION}