1+ #! /bin/bash
2+
3+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the "License").
6+ # You may not use this file except in compliance with the License.
7+ # A copy of the License is located at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # or in the "license" file accompanying this file. This file is distributed
12+ # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+ # express or implied. See the License for the specific language governing
14+ # permissions and limitations under the License.
15+
16+ set -e
17+
18+ # #########################################
19+ # This script is used in CD workflow
20+ # to check to see what type of version
21+ # release is being done
22+ #
23+ # Env vars
24+ # 1. TARGET_VERSION
25+ # 2. REPO_NAME
26+ #
27+ # Step outputs
28+ # (All booleans: true/false)
29+ # 1. any-update
30+ # 2. major-update
31+ # 3. minor-update
32+ # 4. patch-update
33+ # #########################################
34+
35+ # splits the semver v{major}.{minor}.{patch}
36+ split_version () {
37+ if [[ $1 =~ ^v([0-9]+)\. ([0-9]+)\. ([0-9]+)$ ]]; then
38+ echo " ${BASH_REMATCH[1]} ${BASH_REMATCH[2]} ${BASH_REMATCH[3]} "
39+ fi
40+ }
41+
42+ # check environment vars
43+ if [ -z " ${TARGET_VERSION} " ]; then
44+ echo " Must have TARGET_VERSION set"
45+ exit 1
46+ fi
47+
48+ if [ -z " ${REPO_NAME} " ]; then
49+ REPO_NAME=" amazon/aws-otel-collector"
50+ fi
51+
52+ TOKEN=$( curl -s " https://auth.docker.io/token?service=registry.docker.io&scope=repository:${REPO_NAME} :pull" | docker run --rm -i stedolan/jq -c " .token" | sed ' s/"//g' )
53+ # retrieves all the available tags and sorts them in reverse version order
54+ TAGS=$( curl -s " https://registry.hub.docker.com/v2/${REPO_NAME} /tags/list" -H " Authorization: Bearer ${TOKEN} " | docker run --rm -i stedolan/jq -c " .tags[]" | sed ' s/"//g' | sort -V -r)
55+ # get the first tag in the reversed list
56+ LATEST_VERSION=$( echo " ${TAGS} " | sed -n ' 1p' )
57+
58+ echo " Comparing ${LATEST_VERSION} (latest) / ${TARGET_VERSION} (target)"
59+
60+ LATEST_PARTS=($( split_version " ${LATEST_VERSION} " ) )
61+ TARGET_PARTS=($( split_version " ${TARGET_VERSION} " ) )
62+
63+ if [ -z " ${LATEST_PARTS} " ] || [ -z " ${TARGET_PARTS} " ]; then
64+ echo " Unable to split versions: ${LATEST_VERSION} (latest) / ${TARGET_VERSION} (target)"
65+ exit 1
66+ fi
67+
68+ LATEST_MAJOR=${LATEST_PARTS[0]}
69+ LATEST_MINOR=${LATEST_PARTS[1]}
70+ LATEST_PATCH=${LATEST_PARTS[2]}
71+
72+ TARGET_MAJOR=${TARGET_PARTS[0]}
73+ TARGET_MINOR=${TARGET_PARTS[1]}
74+ TARGET_PATCH=${TARGET_PARTS[2]}
75+
76+ MAJOR_UPDATE=false
77+ MINOR_UPDATE=false
78+ PATCH_UPDATE=false
79+
80+ if [ " ${TARGET_MAJOR} " -gt " ${LATEST_MAJOR} " ]; then
81+ MAJOR_UPDATE=true
82+ elif [ " ${TARGET_MAJOR} " -eq " ${LATEST_MAJOR} " ]; then
83+ if [ " ${TARGET_MINOR} " -gt " ${LATEST_MINOR} " ]; then
84+ MINOR_UPDATE=true
85+ elif [ " ${TARGET_MINOR} " -eq " ${LATEST_MINOR} " ] && [ " ${TARGET_PATCH} " -gt " ${LATEST_PATCH} " ]; then
86+ PATCH_UPDATE=true
87+ fi
88+ fi
89+
90+ [ ${MAJOR_UPDATE} == " true" ] || [ ${MINOR_UPDATE} == " true" ] || [ ${PATCH_UPDATE} == " true" ] && ANY_UPDATE=true || ANY_UPDATE=false
91+
92+ echo " ::set-output name=major-update::${MAJOR_UPDATE} "
93+ echo " ::set-output name=minor-update::${MINOR_UPDATE} "
94+ echo " ::set-output name=patch-update::${PATCH_UPDATE} "
95+ echo " ::set-output name=any-update::${ANY_UPDATE} "
0 commit comments