generate-gitlab-ci-yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bash
  2. set -e
  3. set -o pipefail
  4. main() {
  5. local template="${1}"
  6. preamble "${template}"
  7. gen_tests
  8. }
  9. preamble() {
  10. local template="${1}"
  11. cat - "${template}" <<-_EOF_
  12. # This file is generated; do not edit!
  13. # Builds appear on https://gitlab.com/buildroot.org/buildroot/pipelines
  14. image: ${CI_JOB_IMAGE}
  15. _EOF_
  16. }
  17. gen_tests() {
  18. local -a basics defconfigs runtimes
  19. local do_basics do_defconfigs do_runtime do_testpkg
  20. local defconfigs_ext cfg tst
  21. basics=( DEVELOPERS flake8 package )
  22. defconfigs=( $(cd configs; LC_ALL=C ls -1 *_defconfig) )
  23. runtimes=( $(./support/testing/run-tests -l 2>&1 \
  24. | sed -r -e '/^test_run \((.*)\).*/!d; s//\1/' \
  25. | LC_ALL=C sort)
  26. )
  27. if [ -n "${CI_COMMIT_TAG}" ]; then
  28. # When a tag is added to the Buildroot git tree, we want
  29. # to run the runtime tests and only test Qemu defconfigs.
  30. defconfigs=( $(cd configs; LC_ALL=C ls -1 qemu_*_defconfig) )
  31. do_basics=true
  32. do_defconfigs=base
  33. do_runtime=true
  34. elif [ "${CI_PIPELINE_SOURCE}" = "trigger" ]; then
  35. case "${BR_SCHEDULE_JOBS}" in
  36. (basic)
  37. do_basics=true
  38. do_defconfigs=check
  39. defconfigs_ext=_check
  40. ;;
  41. (defconfig)
  42. do_defconfigs=base
  43. ;;
  44. (runtime)
  45. do_runtime=true
  46. ;;
  47. esac
  48. else
  49. case "${CI_COMMIT_REF_NAME}" in
  50. (*-basics)
  51. do_basics=true
  52. do_defconfigs=check
  53. defconfigs_ext=_check
  54. ;;
  55. (*-defconfigs)
  56. do_defconfigs=base
  57. ;;
  58. (*-*_defconfig)
  59. defconfigs=( "${CI_COMMIT_REF_NAME##*-}" )
  60. do_defconfigs=base
  61. ;;
  62. (*-runtime-tests)
  63. do_runtime=true
  64. ;;
  65. (*-tests.*)
  66. runtimes=( $(./support/testing/run-tests -l 2>&1 \
  67. | sed -r -e '/^test_run \((.*)\).*/!d; s//\1/' \
  68. | LC_ALL=C sort \
  69. | grep "^${CI_COMMIT_REF_NAME##*-}")
  70. )
  71. do_runtime=true
  72. ;;
  73. esac
  74. fi
  75. # Retrieve defconfig for test-pkg from the git commit message (if any)
  76. if grep -q -E '^test-pkg config:$' <<<"${CI_COMMIT_DESCRIPTION}"; then
  77. sed -r -n -e '/^test-pkg config:$/{:a;n;p;ba;}' \
  78. <<<"${CI_COMMIT_DESCRIPTION}" \
  79. >defconfig.frag
  80. if [ ! -s defconfig.frag ]; then
  81. printf "Empty configuration fragment.\n" >&2; exit 1
  82. fi
  83. # Use --all since we expect the user having already pre-tested the
  84. # new package with the default subset of toolchains.
  85. ./utils/test-pkg \
  86. --all --prepare-only \
  87. --config-snippet defconfig.frag \
  88. --build-dir br-test-pkg >&2
  89. do_testpkg=( $(ls -1 br-test-pkg/*/.config 2>/dev/null |xargs -r dirname ) )
  90. if [ "${#do_testpkg[@]}" -eq 0 ]; then
  91. printf "Configuration fragment enables no test.\n" >&2; exit 1
  92. fi
  93. fi
  94. # If nothing else, at least do the basics to generate a valid pipeline
  95. if [ -z "${do_defconfigs}" \
  96. -a -z "${do_runtime}" \
  97. -a -z "${do_testpkg}" \
  98. ]
  99. then
  100. do_basics=true
  101. fi
  102. if ${do_basics:-false}; then
  103. for tst in "${basics[@]}"; do
  104. printf 'check-%s: { extends: .check-%s_base }\n' "${tst}" "${tst}"
  105. done
  106. fi
  107. if [ -n "${do_defconfigs}" ]; then
  108. for cfg in "${defconfigs[@]}"; do
  109. printf '%s%s: { extends: .defconfig_%s }\n' \
  110. "${cfg}" "${defconfigs_ext}" "${do_defconfigs}"
  111. done
  112. fi
  113. if ${do_runtime:-false}; then
  114. printf '%s: { extends: .runtime_test_base }\n' "${runtimes[@]}"
  115. fi
  116. if [ -n "${do_testpkg}" ]; then
  117. printf '%s: { extends: .test_pkg }\n' "${do_testpkg[@]}"
  118. fi
  119. }
  120. main "${@}"