Skip to content

Commit

Permalink
! improve ci: use and test jdk8/9/11/15 of sdkman
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jan 7, 2021
1 parent 93dca11 commit 69e7dfc
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 106 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
language: java
# Building a Java project
# https://docs.travis-ci.com/user/languages/java
#
Expand All @@ -9,27 +8,26 @@ language: java
# https://docs.travis-ci.com/user/reference/trusty/#JVM-(Clojure%2C-Groovy%2C-Java%2C-Scala)-images
# - Customizing the Build / Build Matrix
# https://docs.travis-ci.com/user/customizing-the-build/#Build-Matrix
language: java

# The Ubuntu Linux Build Environments
# https://docs.travis-ci.com/user/reference/linux/
dist: bionic

# JVM (Clojure, Groovy, Java, Scala) support
# https://docs.travis-ci.com/user/reference/bionic#jvm-clojure-groovy-java-scala-support
jdk:
- openjdk8
- openjdk10
- openjdk11

# Job Lifecycle
# https://docs.travis-ci.com/user/job-lifecycle/

before_install:
# output env info
- ls -la
- ls -l /usr/lib/jvm && ls /usr/lib/jvm/*
- echo -e "JAVA_HOME=$JAVA_HOME\nPATH=$PATH\nSHELL=$SHELL"
- echo -e "PATH=$PATH\nSHELL=$SHELL"

script:
- scripts/integration-test.sh
- scripts/multi-jdk-integration-test.sh

after_script:
- git status --ignored
Expand All @@ -38,10 +36,12 @@ before_cache:
# clear self maven install
- rm -rf $HOME/.m2/repository/com/alibaba/cola
- rm -rf $HOME/.m2/repository/com/alibaba/craftsman
- rm -rf $HOME/.m2/repository/com/alibaba/demo

# Caching Dependencies and Directories
# https://docs.travis-ci.com/user/caching/
cache:
directories:
- $HOME/.m2
- $HOME/.sdkman
- $HOME/.cache/pip
65 changes: 65 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash
[ -z "${_source_mark_of_common:+dummy}" ] || return 0
_source_mark_of_common=true

set -eEuo pipefail

################################################################################
# constants
################################################################################

# NOTE: $'foo' is the escape sequence syntax of bash
readonly nl=$'\n' # new line
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end

################################################################################
# common util functions
################################################################################

colorEcho() {
local color=$1
shift

# if stdout is the console, turn on color output.
[ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*"
}

redEcho() {
colorEcho 31 "$@"
}

yellowEcho() {
colorEcho 33 "$@"
}

blueEcho() {
colorEcho 36 "$@"
}

headInfo() {
colorEcho "0;34;46" ================================================================================
yellowEcho "$*"
colorEcho "0;34;46" ================================================================================
}

logAndRun() {
local simple_mode=false
[ "$1" = "-s" ] && {
simple_mode=true
shift
}

if $simple_mode; then
echo "Run under work directory $PWD : $*"
"$@"
else
blueEcho "Run under work directory $PWD :$nl$*"
time "$@"
fi
}

die() {
redEcho "Error: $*" 1>&2
exit 1
}
47 changes: 47 additions & 0 deletions scripts/common_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
[ -z "${_source_mark_of_common_build:+dummy}" ] || return 0
_source_mark_of_common_build=true

source "$(dirname "$(readlink -f "$BASH_SOURCE")")/common.sh"

################################################################################
# build util functions
################################################################################

readonly -a MVN_OPTIONS=(
-V --no-transfer-progress
)

MVN() {
local maven_wrapper_name="mvnw"

local d="$PWD"
while true; do
[ "/" = "$d" ] && die "Fail to find $maven_wrapper_name!"
[ -f "$d/$maven_wrapper_name" ] && break

d=$(dirname "$d")
done

logAndRun "$d/$maven_wrapper_name" "${MVN_OPTIONS[@]}" "$@"
}

# Where is Maven local repository?
# https://mkyong.com/maven/where-is-maven-local-repository/

# NOTE: DO NOT declare mvn_local_repository_dir var as readonly, its value is supplied by subshell.
mvn_local_repository_dir="$(
cd $(dirname "$(readlink -f "$BASH_SOURCE")")/../cola-components/ &&
./mvnw --no-transfer-progress help:evaluate -Dexpression=settings.localRepository |
grep '^/'
)"
[ -n "$mvn_local_repository_dir" ] || die "Fail to find find maven local repository directory: $mvn_local_repository_dir"

echo "find maven local repository directory: $mvn_local_repository_dir"

cleanMavenInstallOfColaInMavenLocalRepository() {
headInfo "clean maven build and install of COLA in maven local repository:"
logAndRun -s rm -rf "$mvn_local_repository_dir/com/alibaba/demo"
logAndRun -s rm -rf "$mvn_local_repository_dir/com/alibaba/cola"
logAndRun -s rm -rf "$mvn_local_repository_dir/com/alibaba/craftsman"
}
102 changes: 3 additions & 99 deletions scripts/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,112 +10,16 @@

set -eEuo pipefail

source "$(dirname "$(readlink -f "$BASH_SOURCE")")/common_build.sh"

# adjust current dir to project root dir
cd "$(dirname "$0")/.."

################################################################################
# constants
################################################################################

# NOTE: $'foo' is the escape sequence syntax of bash
readonly nl=$'\n' # new line
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end

################################################################################
# common util functions
################################################################################

colorEcho() {
local color=$1
shift

# if stdout is the console, turn on color output.
[ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*"
}

redEcho() {
colorEcho 31 "$@"
}

yellowEcho() {
colorEcho 33 "$@"
}

blueEcho() {
colorEcho 36 "$@"
}

headInfo() {
colorEcho "0;34;46" ================================================================================
yellowEcho "$*"
colorEcho "0;34;46" ================================================================================
}

logAndRun() {
local simple_mode=false
[ "$1" = "-s" ] && {
simple_mode=true
shift
}

if $simple_mode; then
echo "Run under work directory $PWD : $*"
"$@"
else
blueEcho "Run under work directory $PWD :$nl$*"
time "$@"
fi
}

die() {
redEcho "Error: $*" 1>&2
exit 1
}

################################################################################
# build util functions
################################################################################

readonly -a MVN_OPTIONS=(
-V --no-transfer-progress
)

MVN() {
local maven_wrapper_name="mvnw"

local d="$PWD"
while true; do
[ "/" = "$d" ] && die "Fail to find $maven_wrapper_name!"
[ -f "$d/$maven_wrapper_name" ] && break

d=$(dirname "$d")
done

logAndRun "$d/$maven_wrapper_name" "${MVN_OPTIONS[@]}" "$@"
}

# Where is Maven local repository?
# https://mkyong.com/maven/where-is-maven-local-repository/

# NOTE: DO NOT declare mvn_local_repository_dir var as readonly, its value is supplied by subshell.
mvn_local_repository_dir="$(
cd cola-components/ &&
./mvnw --no-transfer-progress help:evaluate -Dexpression=settings.localRepository |
grep '^/'
)"
[ -n "$mvn_local_repository_dir" ] || die "Fail to find find maven local repository directory: $mvn_local_repository_dir"

echo "find maven local repository directory: $mvn_local_repository_dir"

################################################################################
# CI operations
################################################################################

headInfo "clean maven build and install of COLA in maven local repository:"
logAndRun -s rm -rf "$mvn_local_repository_dir/com/alibaba/demo"
logAndRun -s rm -rf "$mvn_local_repository_dir/com/alibaba/cola"
logAndRun -s rm -rf "$mvn_local_repository_dir/com/alibaba/craftsman"
cleanMavenInstallOfColaInMavenLocalRepository

(
headInfo "CI: cola-components"
Expand Down
21 changes: 21 additions & 0 deletions scripts/multi-jdk-integration-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -eEuo pipefail
# adjust current dir to script dir
cd "$(dirname "$(readlink -f "$0")")"

source common.sh
source prepare-jdk.sh

################################################################################
# multi JDK CI operations
################################################################################

# shellcheck disable=SC2154
for jhm_var_name in "${java_home_var_names[@]}"; do
export JAVA_HOME="${!jhm_var_name}"

echo
headInfo "test with $jhm_var_name: $JAVA_HOME"
echo
logAndRun ./integration-test.sh
done
71 changes: 71 additions & 0 deletions scripts/prepare-jdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash
# SDKMAN! with Travis
# https://objectcomputing.com/news/2019/01/07/sdkman-travis
set -eEuo pipefail

[ -z "${_source_mark_of_prepare_jdk:+dummy}" ] || return 0
export _source_mark_of_prepare_jdk=true

source "$(dirname "$(readlink -f "$BASH_SOURCE")")/common.sh"

if [ ! -f "$HOME/.sdkman/bin/sdkman-init.sh" ]; then
[ -d "$HOME/.sdkman" ] && rm -rf "$HOME/.sdkman"
curl -s get.sdkman.io | bash || die "fail to install sdkman"
echo sdkman_auto_answer=true >>"$HOME/.sdkman/etc/config"

this_time_install_sdk_man=true
fi
set +u
# shellcheck disable=SC1090
source "$HOME/.sdkman/bin/sdkman-init.sh"
[ -n "$this_time_install_sdk_man" ] && logAndRun sdk ls java
set -u

jdks_install_by_sdkman=(
8.0.275-amzn

9.0.7-zulu
#10.0.2-zulu
11.0.9-zulu

#12.0.2-open
#13.0.5-zulu
#14.0.2-zulu
15.0.1-zulu
)
java_home_var_names=()

__setJdkHomeVarsAndInstallJdk() {
blueEcho "prepared jdks:"

local jdkNameOfSdkman
for jdkNameOfSdkman in "${jdks_install_by_sdkman[@]}"; do
local jdkVersion
jdkVersion=$(echo "$jdkNameOfSdkman" | awk -F'[.]' '{print $1}')

# jdkHomeVarName like JDK7_HOME, JDK11_HOME
local jdkHomeVarName="JDK${jdkVersion}_HOME"

if [ ! -d "${!jdkHomeVarName:-}" ]; then
local jdkHomePath="$SDKMAN_CANDIDATES_DIR/java/$jdkNameOfSdkman"

# set JDKx_HOME to global var java_home_var_names
eval "$jdkHomeVarName='${jdkHomePath}'"

# install jdk by sdkman
[ ! -d "$jdkHomePath" ] && {
set +u
logAndRun sdk install java "$jdkNameOfSdkman" || die "fail to install jdk $jdkNameOfSdkman by sdkman"
set -u
}
fi

java_home_var_names=(${java_home_var_names[@]:+"${java_home_var_names[@]}"} "$jdkHomeVarName")
printf '%s :\n\t%s\n\tspecified is %s\n' "$jdkHomeVarName" "${!jdkHomeVarName}" "$jdkNameOfSdkman"
done

echo
blueEcho "ls $SDKMAN_CANDIDATES_DIR/java/ :"
ls -la "$SDKMAN_CANDIDATES_DIR/java/"
}
__setJdkHomeVarsAndInstallJdk

0 comments on commit 69e7dfc

Please sign in to comment.