Commit 0efc306b authored by Emeric Verschuur's avatar Emeric Verschuur

[ci-toolbox] Add CITBX_RUN_JOB_DEPENDENCIES job property (--with-dependencies...

[ci-toolbox] Add CITBX_RUN_JOB_DEPENDENCIES job property (--with-dependencies option) to run dependencies before

Implments #17
parent 06b6d9fb
......@@ -74,6 +74,16 @@ job-test-services-postgres:
printf " done!\n"
- psql -h db-postgres -U postgres -c 'select version();'
job-test-deps:
stage: test
tags:
- docker
script:
- echo "Simple job to test dependencies"
dependencies:
- job-with-before-after-script
- job-advanced
##################################################
# ### PACKAGE BUILD ### #
##################################################
......
......@@ -173,6 +173,8 @@ Exemple de script de module : [ci-scripts/modules/example.sh](ci-scripts/modules
* `citbx_job_list [prefix]` : Obtenir la liste des tâches (en option : avec le préfixe spécifié)
* `citbx_use <nom du module>` : Charger un module
* `citbx_export`: Exporte une variable à l'environnement de la tâche
* `citbx_subjob_export`: Exporte une variable à l'environnement des sous tâche (dans le contexte de l'option `--with-dependencies`)
* `print_critical <message>` : Affichage d'un message d'erreur et sortie (code de retour : 1)
* `print_error <message>` : Affichage un message d'erreur
* `print_warning <message>` : Affichage un message d'avertissement
......
......@@ -173,6 +173,8 @@ Example of module script: [ci-scripts/modules/example.sh](ci-scripts/modules/exa
* `citbx_job_list [prefix]`: Get the job list (optionally with the specified prefix)
* `citbx_use <module name>`: Load a module
* `citbx_export`: Export a variable to the job environment
* `citbx_subjob_export`: Export a variable to the sub jobs (in the context of the `--with-dependencies` option)
* `print_critical <message>`: Print an error message and exit (exit code: 1)
* `print_error <message>`: Print an error message
* `print_warning <message>`: Print a warning message
......
job_define() {
JOB_OPTION_EXPORT="dispatched value to subjobs"
citbx_subjob_export JOB_OPTION_EXPORT
}
......@@ -200,11 +200,54 @@ citbx_gitlab_ci_script() {
esac
}
# Run job dependencies
citbx_run_job_dependencies() {
local job_dependency_tree
# Build job dependency graph
citbx_fill_dependency_tree() {
local job_name=$1
if printf "%s\n" "${job_dependency_tree[@]}" | grep -q ^"$job_name"$; then
# Job already in the list: nothing to do
return
fi
job_dependency_tree+=("$job_name")
if [ $(citbx_gitlab_ci_query -r '."'"$job_name"'".dependencies | length') -eq 0 ]; then
# Job without dependencies: nothing to do
return
fi
while read -r j; do
citbx_fill_dependency_tree "$j"
done <<< "$(citbx_gitlab_ci_query -r '."'"$job_name"'".dependencies[]')"
}
citbx_fill_dependency_tree "$CI_JOB_NAME"
if [ ${#job_dependency_tree[@]} -eq 1 ]; then
print_note "No dependency found."
return
fi
local subjob_list
local env_args
while read -r j; do
subjob_list+=("$j")
done <<< "$(printf "%s\n" "${job_dependency_tree[@]:1}" | tac)"
for v in "${CITBX_SUBJOB_ENV_EXPORT_LIST[@]}"; do
env_args+=("$v=${!v}")
done
print_info "Number of dependencies: ${#subjob_list[@]}"
for j in "${subjob_list[@]}"; do
env "${env_args[@]}" $0 "$j"
done
print_info "All dependency jobs executed"
}
# DEPRECATED: Run an other job
citbx_run_ext_job() {
print_warning "The 'citbx_run_ext_job' function is deprecated since version 5.2.0 and will be removed in a future release." \
"=> Please remove all references to this function in the job_define/job_setup hook from the following file:" \
" * $CITBX_JOBS_DIR/$CITBX_JOB_FILE_NAME"
" * $CITBX_JOBS_DIR/$CITBX_JOB_FILE_NAME" \
"=> If you were using this function to run job's dependencies, then use the '--with-dependencies' option instead."
if [ "$CITBX_RUN_JOB_DEPENDENCIES" == "true" ]; then
print_critical "'--with-dependencies' option cannot be used with the 'citbx_run_ext_job' function."
fi
local env_args
local env
for env in "${bashopts_optlist[@]:1}"; do
......@@ -219,11 +262,16 @@ citbx_run_ext_job() {
env "${env_args[@]}" "$0" "$@"
}
# Export an variable to the job environment
# Export a variable to the job environment
citbx_export() {
CITBX_ENV_EXPORT_LIST+=("$@")
}
# Export a variable to the sub jobs
citbx_subjob_export() {
CITBX_SUBJOB_ENV_EXPORT_LIST+=("$@")
}
# Add docker run arguments
citbx_docker_run_add_args() {
CITBX_JOB_DOCKER_RUN_ARGS+=("$@")
......@@ -398,6 +446,10 @@ $(for j in "${CITBX_JOB_LIST[@]}"; do echo " $j"; done | sort -u)"
-d "Job executor type (only docker or shell is sypported yet)" -t enum \
-v "$(test -n "$CITBX_DEFAULT_DOCKER_IMAGE" && echo "docker" || echo "shell" )" \
-e 's|shell' -e 'd|docker'
bashopts_declare -n CITBX_RUN_JOB_DEPENDENCIES -l with-dependencies -t boolean \
-d "Run job dependencies"
bashopts_declare -n CITBX_SUBJOB_ENV_EXPORT_LIST -l export-to-subjob -o E -t string -m add \
-d "Export a property to a sub job (only applicable if CITBX_RUN_JOB_DEPENDENCIES property/export-to-subjob option is enabled)"
bashopts_declare -n CITBX_DOCKER_IMAGE -l docker-image -d "Docker image name" -t string \
-x "\"$CITBX_DEFAULT_DOCKER_IMAGE\""
bashopts_declare -n CITBX_DOCKER_ENTRYPOINT -l docker-entrypoint -d "Docker entrypoint" -t string -m add \
......@@ -466,6 +518,9 @@ if [ -n "$CITBX_BASHCOMP" ]; then
done <<< "$(docker image ls | tail -n +2 \
| awk '($1 != "<none>" && $2 != "<none>") {print $1":"$2}')"
;;
--export-to-subjob|-E)
printf '"%s"\n' "${bashopts_optlist[@]:1}"
;;
-*)
bashopts_get_valid_value_list $CITBX_BASHCOMP
;;
......@@ -490,6 +545,36 @@ case "$command" in
;;
esac
# Fetch git submodules
if [ "$GIT_SUBMODULE_STRATEGY" != "none" ]; then
GIT_SUBMODULE_ARGS=()
case "$GIT_SUBMODULE_STRATEGY" in
normal)
;;
recursive)
GIT_SUBMODULE_ARGS+=("--recursive")
;;
*)
print_critical "Invalid value for GIT_SUBMODULE_STRATEGY: $GIT_SUBMODULE_STRATEGY"
;;
esac
print_info "Fetching git submodules..."
git submodule --quiet sync "${GIT_SUBMODULE_ARGS[@]}"
git submodule update --init "${GIT_SUBMODULE_ARGS[@]}"
fi
if [ "$CITBX_GIT_CLEAN" == "true" ]; then
git clean -fdx
if [ "$GIT_SUBMODULE_STRATEGY" != "none" ]; then
git submodule --quiet foreach "${GIT_SUBMODULE_ARGS[@]}" git clean -fdx
fi
fi
# Run job dependencies first (if asked)
if [ "$CITBX_RUN_JOB_DEPENDENCIES" == "true" ]; then
citbx_run_job_dependencies
fi
if [ "$(citbx_gitlab_ci_query -r '."'"$CI_JOB_NAME"'".script | type')" == "null" ]; then
print_critical "Unable to find a valid job with tne name \"$CI_JOB_NAME\" in the .gitlab-ci.yml"
fi
......@@ -560,31 +645,6 @@ if citbx_gitlab_ci_script '."'"$CI_JOB_NAME"'"."after_script"' \
fi
CITBX_JOB_SCRIPT="'${CITBX_JOB_SCRIPT_PARTS[*]//\'/\'\\\'\'}'"
# Fetch git submodules
if [ "$GIT_SUBMODULE_STRATEGY" != "none" ]; then
GIT_SUBMODULE_ARGS=()
case "$GIT_SUBMODULE_STRATEGY" in
normal)
;;
recursive)
GIT_SUBMODULE_ARGS+=("--recursive")
;;
*)
print_critical "Invalid value for GIT_SUBMODULE_STRATEGY: $GIT_SUBMODULE_STRATEGY"
;;
esac
print_info "Fetching git submodules..."
git submodule --quiet sync "${GIT_SUBMODULE_ARGS[@]}"
git submodule update --init "${GIT_SUBMODULE_ARGS[@]}"
fi
if [ "$CITBX_GIT_CLEAN" == "true" ]; then
git clean -fdx
if [ "$GIT_SUBMODULE_STRATEGY" != "none" ]; then
git submodule --quiet foreach "${GIT_SUBMODULE_ARGS[@]}" git clean -fdx
fi
fi
# Git SHA1
CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME:-$(cd $CI_PROJECT_DIR && git rev-parse --abbrev-ref HEAD)}
CITBX_JOB_DOCKER_RUN_ARGS+=(-e CI_COMMIT_REF_NAME="$CI_COMMIT_REF_NAME")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment