Browse Source

refactor: add helper scripts

develop
Apostolos Fanakis 4 years ago
parent
commit
a2fafd7dbb
  1. 24
      jenkins/check_package_changed.sh
  2. 18
      jenkins/hash_build_properties.sh
  3. 5
      jenkins/map_to_thousand.sh

24
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

18
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 <branch> <build_number>
# 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"

5
jenkins/map_to_thousand.sh

@ -0,0 +1,5 @@
#!/bin/bash
INTEGER_TO_MAP=$1
echo $(( INTEGER_TO_MAP * 999 / 4095 ))
Loading…
Cancel
Save