|
| 1 | +#!/bin/bash |
| 2 | +set -exo pipefail |
| 3 | + |
| 4 | +### find previous sucessfull tag |
| 5 | +# shellcheck disable=SC2154 |
| 6 | +if [[ ${branch} == "master" ]]; then |
| 7 | + prev_build=$(git describe --tags --abbrev=0 --first-parent --exclude='????.??.??-*-*') |
| 8 | +else |
| 9 | + prev_build=$(git describe --tags --abbrev=0 --first-parent) |
| 10 | +fi |
| 11 | +# if we have multiple tags on same commit, pick latest |
| 12 | +prev_build=$(git tag --points-at "${prev_build}" | sort | tail -n 1) |
| 13 | +git_prev_build_sha=$(git rev-list -n1 "${prev_build}") |
| 14 | + |
| 15 | +function get_diff_count() { |
| 16 | + changed=$(git diff "${1}" --shortstat -- | awk '{print $1}') |
| 17 | + if [[ -z $changed ]]; then |
| 18 | + changed=0 |
| 19 | + fi |
| 20 | + echo -n $changed |
| 21 | +} |
| 22 | +LAST_COMPLETED_JOB_SHA1=${git_prev_build_sha} |
| 23 | + |
| 24 | + |
| 25 | +# this needed as we have tag only healthy builds. And probably don't want to compare with unhealthy build |
| 26 | +master_tag=$(git describe --tags --abbrev=0 --first-parent --exclude='????.??.??-*-*' origin/master) |
| 27 | +master_sha=$(git rev-list -n1 "${master_tag}") |
| 28 | +changes_master=$(get_diff_count "${master_sha}") |
| 29 | + |
| 30 | +changes_git_tag=$(get_diff_count "${git_prev_build_sha}") |
| 31 | + |
| 32 | +changes_current_candidate=$(get_diff_count "${LAST_COMPLETED_JOB_SHA1}") |
| 33 | + |
| 34 | +if [[ ${changes_current_candidate} -gt ${changes_master} ]]; then |
| 35 | + echo "as previuos will choose version from master ${master_tag}" |
| 36 | + LAST_COMPLETED_JOB_SHA1=${master_sha} |
| 37 | + changes_current_candidate=$(get_diff_count "${LAST_COMPLETED_JOB_SHA1}") |
| 38 | +elif [[ ${changes_current_candidate} -gt ${changes_git_tag} ]]; then |
| 39 | + echo "as previuos will choose version from git tag ${prev_build}" |
| 40 | + LAST_COMPLETED_JOB_SHA1=${git_prev_build_sha} |
| 41 | + changes_current_candidate=$(get_diff_count "${LAST_COMPLETED_JOB_SHA1}") |
| 42 | +fi |
| 43 | +echo "LAST_COMPLETED_JOB_SHA1=${LAST_COMPLETED_JOB_SHA1}" >> $GITHUB_ENV |
| 44 | + |
| 45 | + |
| 46 | +LATEST_CACHE_SHA1=${LAST_COMPLETED_JOB_SHA1} |
| 47 | + |
| 48 | + |
| 49 | +set +xo pipefail |
| 50 | +retrive_tag_num=30 |
| 51 | +last_builds=$(git tag -l --sort='-authordate' | head -n "${retrive_tag_num}" | uniq) |
| 52 | +set -o pipefail |
| 53 | +last_builds_array=($last_builds) |
| 54 | +tmp_dir='/tmp/' |
| 55 | +# changes_current_candidate |
| 56 | +(get_diff_count "${LATEST_CACHE_SHA1}"; echo -n " ${LATEST_CACHE_SHA1}") > "$tmp_dir/current.diff" & |
| 57 | +# git tags |
| 58 | +for i in $(seq 0 "$((retrive_tag_num-1))"); do |
| 59 | + (get_diff_count "${last_builds_array[$i]}"; echo " ${last_builds_array[$i]}") > "$tmp_dir/$i.diff" & |
| 60 | +done |
| 61 | +wait |
| 62 | +cache_candidate=$(cat "$tmp_dir"/*.diff | sort -n | head -n 1) |
| 63 | +tag_with_min_changes=$(echo "$cache_candidate" | awk '{print $2}') |
| 64 | +min_changes=$(echo "$cache_candidate" | awk '{print $1}') |
| 65 | + |
| 66 | +echo "as previous will choose version from commit ${tag_with_min_changes} with ${min_changes} changes" |
| 67 | +LATEST_CACHE_SHA1=$(git rev-list -n1 "${tag_with_min_changes}") |
| 68 | +changes_current_candidate=$(get_diff_count "${LATEST_CACHE_SHA1}") |
| 69 | + |
| 70 | +echo "LATEST_CACHE_SHA1=${LATEST_CACHE_SHA1}" >> $GITHUB_ENV |
0 commit comments