1 | #!/usr/bin/env bash
|
2 |
|
3 |
|
4 |
|
5 | set -e
|
6 | set -o pipefail
|
7 |
|
8 | current_branch=$(git rev-parse --abbrev-ref HEAD)
|
9 | if [[ "${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
|
14 | fi
|
15 |
|
16 |
|
17 | last_production_tag=$(git describe --first-parent --abbrev=0 production)
|
18 | last_master_tag=$(git describe --first-parent --abbrev=0 master)
|
19 | last_develop_tag=$(git describe --first-parent --abbrev=0 develop)
|
20 | echo Last Production Tag: ${last_production_tag}
|
21 | echo Last Master Tag: ${last_master_tag}
|
22 | echo Last Develop Tag: ${last_develop_tag}
|
23 |
|
24 |
|
25 | last_production_ver=( ${last_production_tag//-/ } )
|
26 | last_production_mmp_ver=( ${last_production_ver[0]//./ } )
|
27 | last_production_pre_ver=${last_production_ver[1]}
|
28 | last_production_maj_ver=${last_production_mmp_ver[0]}
|
29 | last_production_min_ver=${last_production_mmp_ver[1]}
|
30 | last_production_pat_ver=${last_production_mmp_ver[2]}
|
31 |
|
32 |
|
33 | last_master_ver=( ${last_master_tag//-/ } )
|
34 | last_master_mmp_ver=( ${last_master_ver[0]//./ } )
|
35 | last_master_pre_ver=${last_master_ver[1]}
|
36 | last_master_maj_ver=${last_master_mmp_ver[0]}
|
37 | last_master_min_ver=${last_master_mmp_ver[1]}
|
38 | last_master_pat_ver=${last_master_mmp_ver[2]}
|
39 |
|
40 |
|
41 | last_develop_ver=( ${last_develop_tag//-/ } )
|
42 | last_develop_mmp_ver=( ${last_develop_ver[0]//./ } )
|
43 | last_develop_pre_ver=${last_develop_ver[1]}
|
44 | last_develop_maj_ver=${last_develop_mmp_ver[0]}
|
45 | last_develop_min_ver=${last_develop_mmp_ver[1]}
|
46 | last_develop_pat_ver=${last_develop_mmp_ver[2]}
|
47 |
|
48 |
|
49 | break_count=$(git rev-list ${last_production_tag}..HEAD --grep='^...\*' --count)
|
50 | feature_count=$(git rev-list ${last_production_tag}..HEAD --grep='^fea:' --count)
|
51 | fix_count=$(git rev-list ${last_production_tag}..HEAD --grep='^fix:' --count)
|
52 | iac_count=$(git rev-list ${last_production_tag}..HEAD --grep='^iac:' --count)
|
53 | echo Breaks: ${break_count}
|
54 | echo Features: ${feature_count}
|
55 | echo Fixes: ${fix_count}
|
56 | echo IaC Changes: ${iac_count}
|
57 |
|
58 | unset next_maj_ver
|
59 | unset next_min_ver
|
60 | unset next_pat_ver
|
61 | unset next_pre_ver
|
62 | if [[ "${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}
|
67 | else
|
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 |
|
87 | if [[ "${break_count}" -gt 0 ]]; then
|
88 |
|
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 |
|
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 |
|
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
|
130 | fi
|
131 |
|
132 | if [[ -z "${next_pre_ver}" ]]; then
|
133 | export NEXT_VERSION="${next_maj_ver}.${next_min_ver}.${next_pat_ver}"
|
134 | else
|
135 | export NEXT_VERSION="${next_maj_ver}.${next_min_ver}.${next_pat_ver}-${next_pre_ver}"
|
136 | fi
|
137 | echo Next Version: ${NEXT_VERSION}
|