2019-04-18 15:18:20 -04:00
|
|
|
function retry() {
|
2020-05-25 11:07:58 -04:00
|
|
|
if eval "$@"; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
for i in 2 1; do
|
|
|
|
sleep 3s
|
|
|
|
echo "Retrying $i..."
|
2017-03-22 10:31:07 -04:00
|
|
|
if eval "$@"; then
|
2020-05-25 11:07:58 -04:00
|
|
|
return 0
|
2017-03-22 10:31:07 -04:00
|
|
|
fi
|
2020-05-25 11:07:58 -04:00
|
|
|
done
|
|
|
|
return 1
|
2017-03-22 10:31:07 -04:00
|
|
|
}
|
2018-05-11 11:06:08 -04:00
|
|
|
|
2021-06-10 05:10:04 -04:00
|
|
|
function test_url() {
|
2021-06-09 05:10:18 -04:00
|
|
|
local url="${1}"
|
2021-06-10 05:10:04 -04:00
|
|
|
local status
|
2021-06-09 05:10:18 -04:00
|
|
|
|
2022-04-21 11:10:09 -04:00
|
|
|
status=$(curl --output /dev/null -L -s -w ''%{http_code}'' "${url}")
|
2021-06-09 05:10:18 -04:00
|
|
|
|
2021-06-10 05:10:04 -04:00
|
|
|
if [[ $status == "200" ]]; then
|
|
|
|
return 0
|
2022-04-21 11:10:09 -04:00
|
|
|
else
|
|
|
|
# We display the error in the job to allow for better debugging
|
|
|
|
curl -L --fail --output /dev/null "${url}"
|
|
|
|
echo -e "\nExpected HTTP status 200: received ${status}\n"
|
|
|
|
return 1
|
2021-06-10 05:10:04 -04:00
|
|
|
fi
|
2021-06-09 05:10:18 -04:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:10:34 -04:00
|
|
|
function bundle_install_script() {
|
|
|
|
local extra_install_args="${1}"
|
|
|
|
|
|
|
|
if [[ "${extra_install_args}" =~ "--without" ]]; then
|
|
|
|
echoerr "The '--without' flag shouldn't be passed as it would replace the default \${BUNDLE_WITHOUT} (currently set to '${BUNDLE_WITHOUT}')."
|
|
|
|
echoerr "Set the 'BUNDLE_WITHOUT' variable instead, e.g. '- export BUNDLE_WITHOUT=\"\${BUNDLE_WITHOUT}:any:other:group:not:to:install\"'."
|
|
|
|
exit 1;
|
|
|
|
fi;
|
|
|
|
|
2022-09-13 14:12:21 -04:00
|
|
|
echo -e "section_start:`date +%s`:bundle-install[collapsed=true]\r\e[0KInstalling gems"
|
|
|
|
|
2022-06-22 08:08:59 -04:00
|
|
|
gem --version
|
2021-05-14 14:10:34 -04:00
|
|
|
bundle --version
|
2022-06-22 08:08:59 -04:00
|
|
|
gem install bundler --no-document --conservative --version 2.3.15
|
|
|
|
test -d jh && bundle config set --local gemfile 'jh/Gemfile'
|
2021-09-01 11:10:20 -04:00
|
|
|
bundle config set path "$(pwd)/vendor"
|
2021-05-14 14:10:34 -04:00
|
|
|
bundle config set clean 'true'
|
|
|
|
|
2021-07-15 08:09:01 -04:00
|
|
|
echo "${BUNDLE_WITHOUT}"
|
2021-05-14 14:10:34 -04:00
|
|
|
bundle config
|
|
|
|
|
2022-09-13 14:12:21 -04:00
|
|
|
run_timed_command "bundle install ${BUNDLE_INSTALL_FLAGS} ${extra_install_args}"
|
2021-05-14 14:10:34 -04:00
|
|
|
|
|
|
|
if [[ $(bundle info pg) ]]; then
|
|
|
|
# When we test multiple versions of PG in the same pipeline, we have a single `setup-test-env`
|
|
|
|
# job but the `pg` gem needs to be rebuilt since it includes extensions (https://guides.rubygems.org/gems-with-extensions).
|
|
|
|
# Uncomment the following line if multiple versions of PG are tested in the same pipeline.
|
|
|
|
run_timed_command "bundle pristine pg"
|
|
|
|
fi
|
2022-09-13 14:12:21 -04:00
|
|
|
|
|
|
|
echo -e "section_end:`date +%s`:bundle-install\r\e[0K"
|
2021-05-14 14:10:34 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:18:20 -04:00
|
|
|
function setup_db_user_only() {
|
2020-05-25 11:07:58 -04:00
|
|
|
source scripts/create_postgres_user.sh
|
2018-05-14 13:49:46 -04:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:09:17 -04:00
|
|
|
function setup_db_praefect() {
|
|
|
|
createdb -h postgres -U postgres --encoding=UTF8 --echo praefect_test
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:18:20 -04:00
|
|
|
function setup_db() {
|
2020-05-25 11:07:58 -04:00
|
|
|
run_timed_command "setup_db_user_only"
|
2022-06-02 23:09:20 -04:00
|
|
|
run_timed_command_with_metric "bundle exec rake db:drop db:create db:schema:load db:migrate" "setup_db"
|
2022-05-11 20:09:17 -04:00
|
|
|
run_timed_command "setup_db_praefect"
|
2019-07-22 11:11:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function install_gitlab_gem() {
|
2022-01-25 13:11:55 -05:00
|
|
|
run_timed_command "gem install httparty --no-document --version 0.18.1"
|
|
|
|
run_timed_command "gem install gitlab --no-document --version 4.17.0"
|
2019-07-22 11:11:50 -04:00
|
|
|
}
|
|
|
|
|
2020-08-25 11:10:17 -04:00
|
|
|
function install_tff_gem() {
|
2022-01-25 13:11:55 -05:00
|
|
|
run_timed_command "gem install test_file_finder --no-document --version 0.1.1"
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_junit_merge_gem() {
|
|
|
|
run_timed_command "gem install junit_merge --no-document --version 0.1.2"
|
2020-08-25 11:10:17 -04:00
|
|
|
}
|
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
function run_timed_command() {
|
|
|
|
local cmd="${1}"
|
2022-02-24 16:15:17 -05:00
|
|
|
local metric_name="${2:-no}"
|
2021-10-12 17:09:47 -04:00
|
|
|
local timed_metric_file
|
2020-05-14 11:08:14 -04:00
|
|
|
local start=$(date +%s)
|
2021-10-12 17:09:47 -04:00
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
echosuccess "\$ ${cmd}"
|
|
|
|
eval "${cmd}"
|
2021-10-12 17:09:47 -04:00
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
local ret=$?
|
|
|
|
local end=$(date +%s)
|
|
|
|
local runtime=$((end-start))
|
|
|
|
|
|
|
|
if [[ $ret -eq 0 ]]; then
|
|
|
|
echosuccess "==> '${cmd}' succeeded in ${runtime} seconds."
|
2021-10-12 17:09:47 -04:00
|
|
|
|
2022-02-24 16:15:17 -05:00
|
|
|
if [[ "${metric_name}" != "no" ]]; then
|
2021-10-12 17:09:47 -04:00
|
|
|
timed_metric_file=$(timed_metric_file $metric_name)
|
|
|
|
echo "# TYPE ${metric_name} gauge" > "${timed_metric_file}"
|
|
|
|
echo "# UNIT ${metric_name} seconds" >> "${timed_metric_file}"
|
|
|
|
echo "${metric_name} ${runtime}" >> "${timed_metric_file}"
|
|
|
|
fi
|
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
echoerr "==> '${cmd}' failed (${ret}) in ${runtime} seconds."
|
|
|
|
return $ret
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:09:47 -04:00
|
|
|
function run_timed_command_with_metric() {
|
|
|
|
local cmd="${1}"
|
|
|
|
local metric_name="${2}"
|
|
|
|
local metrics_file=${METRICS_FILE:-metrics.txt}
|
|
|
|
|
|
|
|
run_timed_command "${cmd}" "${metric_name}"
|
|
|
|
|
|
|
|
local ret=$?
|
|
|
|
|
|
|
|
cat $(timed_metric_file $metric_name) >> "${metrics_file}"
|
|
|
|
|
|
|
|
return $ret
|
|
|
|
}
|
|
|
|
|
|
|
|
function timed_metric_file() {
|
|
|
|
local metric_name="${1}"
|
|
|
|
|
|
|
|
echo "$(pwd)/tmp/duration_${metric_name}.txt"
|
|
|
|
}
|
|
|
|
|
2019-07-22 11:11:50 -04:00
|
|
|
function echoerr() {
|
2022-02-24 16:15:17 -05:00
|
|
|
local header="${2:-no}"
|
2019-07-22 11:11:50 -04:00
|
|
|
|
2022-02-24 16:15:17 -05:00
|
|
|
if [ "${header}" != "no" ]; then
|
2019-07-22 11:11:50 -04:00
|
|
|
printf "\n\033[0;31m** %s **\n\033[0m" "${1}" >&2;
|
|
|
|
else
|
|
|
|
printf "\033[0;31m%s\n\033[0m" "${1}" >&2;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function echoinfo() {
|
2022-02-24 16:15:17 -05:00
|
|
|
local header="${2:-no}"
|
2019-07-22 11:11:50 -04:00
|
|
|
|
2022-02-24 16:15:17 -05:00
|
|
|
if [ "${header}" != "no" ]; then
|
2019-07-22 11:11:50 -04:00
|
|
|
printf "\n\033[0;33m** %s **\n\033[0m" "${1}" >&2;
|
|
|
|
else
|
|
|
|
printf "\033[0;33m%s\n\033[0m" "${1}" >&2;
|
|
|
|
fi
|
|
|
|
}
|
2020-03-19 20:09:29 -04:00
|
|
|
|
2020-05-14 11:08:14 -04:00
|
|
|
function echosuccess() {
|
2022-02-24 16:15:17 -05:00
|
|
|
local header="${2:-no}"
|
2020-05-14 11:08:14 -04:00
|
|
|
|
2022-02-24 16:15:17 -05:00
|
|
|
if [ "${header}" != "no" ]; then
|
2020-05-14 11:08:14 -04:00
|
|
|
printf "\n\033[0;32m** %s **\n\033[0m" "${1}" >&2;
|
|
|
|
else
|
|
|
|
printf "\033[0;32m%s\n\033[0m" "${1}" >&2;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:09:28 -04:00
|
|
|
function fail_pipeline_early() {
|
|
|
|
local dont_interrupt_me_job_id
|
2021-01-12 01:10:31 -05:00
|
|
|
dont_interrupt_me_job_id=$(scripts/api/get_job_id.rb --job-query "scope=success" --job-name "dont-interrupt-me")
|
2020-09-14 11:09:28 -04:00
|
|
|
|
2020-09-16 05:09:43 -04:00
|
|
|
if [[ -n "${dont_interrupt_me_job_id}" ]]; then
|
2020-09-14 11:09:28 -04:00
|
|
|
echoinfo "This pipeline cannot be interrupted due to \`dont-interrupt-me\` job ${dont_interrupt_me_job_id}"
|
|
|
|
else
|
|
|
|
echoinfo "Failing pipeline early for fast feedback due to test failures in rspec fail-fast."
|
2021-01-12 01:10:31 -05:00
|
|
|
scripts/api/cancel_pipeline.rb
|
2020-09-14 11:09:28 -04:00
|
|
|
fi
|
|
|
|
}
|
2021-07-15 08:09:01 -04:00
|
|
|
|
|
|
|
function danger_as_local() {
|
|
|
|
# Force danger to skip CI source GitLab and fallback to "local only git repo".
|
|
|
|
unset GITLAB_CI
|
|
|
|
# We need to base SHA to help danger determine the base commit for this shallow clone.
|
2022-08-11 17:09:14 -04:00
|
|
|
bundle exec danger dry_run --fail-on-errors=true --verbose --base="${CI_MERGE_REQUEST_DIFF_BASE_SHA}" --head="${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA:-$CI_COMMIT_SHA}" --dangerfile="${DANGER_DANGERFILE:-Dangerfile}"
|
2021-07-15 08:09:01 -04:00
|
|
|
}
|