From a2fafd7dbb493ba60cbde0e10f35dbc6a0432c84 Mon Sep 17 00:00:00 2001 From: apostolof Date: Sat, 13 Feb 2021 16:45:33 +0200 Subject: [PATCH] refactor: add helper scripts --- jenkins/check_package_changed.sh | 24 ++++++++++++++++++++++++ jenkins/hash_build_properties.sh | 18 ++++++++++++++++++ jenkins/map_to_thousand.sh | 5 +++++ 3 files changed, 47 insertions(+) create mode 100755 jenkins/check_package_changed.sh create mode 100755 jenkins/hash_build_properties.sh create mode 100755 jenkins/map_to_thousand.sh diff --git a/jenkins/check_package_changed.sh b/jenkins/check_package_changed.sh new file mode 100755 index 0000000..b915e37 --- /dev/null +++ b/jenkins/check_package_changed.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Based on this post: +# https://engineering.brigad.co/only-deploy-services-impacted-by-changes-in-a-mono-repository-18f54b8ac109 + +APP=$1 +# Jenkins should provide these in the environment by default +GIT_COMMIT=$2 +GIT_PREVIOUS_COMMIT=$3 +ROOT_FILES_AND_FOLDERS=${4:-"package.json" "yarn.lock" ".dockerignore" "docker" "jenkins"} + +function join_by { local IFS="$1"; shift; echo "$*"; } + +function package_changed { git diff --name-only "$COMMIT_RANGE" | grep -qE "^packages/$1/" && echo true || echo false; } + +COMMIT_RANGE="$GIT_PREVIOUS_COMMIT...$GIT_COMMIT" +ROOT_FILES_AND_FOLDERS_ARRAY=($ROOT_FILES_AND_FOLDERS) +ROOT_FILES_AND_FOLDERS_JOINED=$(join_by "|" ${ROOT_FILES_AND_FOLDERS_ARRAY[*]}) + +ROOT_FILES_CHANGED=$(git diff --name-only "$COMMIT_RANGE" | grep -qE "^($ROOT_FILES_AND_FOLDERS_JOINED)" && echo true || echo false) +IS_FORCE_BUILD=$(git log --oneline "$COMMIT_RANGE" | grep -qE "ci: force" && echo true || echo false) +APP_FILES_CHANGED=$(package_changed ${APP}) + +($IS_FORCE_BUILD || $ROOT_FILES_CHANGED || $APP_FILES_CHANGED) && echo 0 || echo 1 diff --git a/jenkins/hash_build_properties.sh b/jenkins/hash_build_properties.sh new file mode 100755 index 0000000..a30e81c --- /dev/null +++ b/jenkins/hash_build_properties.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Outputs to the stdout a deterministically generated integer in the range 0-4095. The integer is generated using the +# input strings and SHA1. +# Usage: hash_build_properties.sh +# Inputs: +# - branch: the branch being build +# - build_number the incrementing number of the build + +BRANCH=$1 +BUILD_NUMBER=$2 + +STRING_TO_HASH="$BRANCH-$BUILD_NUMBER" +SHA1_SUM_HEX=$(sha1sum <<<"$STRING_TO_HASH") +SHA1_TRUNCATED_HEX=$(cut -c1-3 <<<"$SHA1_SUM_HEX") +HASHED_STRING=$((0x${SHA1_TRUNCATED_HEX})) + +echo "$HASHED_STRING" diff --git a/jenkins/map_to_thousand.sh b/jenkins/map_to_thousand.sh new file mode 100755 index 0000000..d94d29d --- /dev/null +++ b/jenkins/map_to_thousand.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +INTEGER_TO_MAP=$1 + +echo $(( INTEGER_TO_MAP * 999 / 4095 ))