get-full-sources.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. #
  3. # Short script that is run as part of the CI environment
  4. # in .gitlab-ci.yml
  5. #
  6. # Sets up submodules in the ESP-IDF source tree
  7. # - Ideally, this just means doing a "git submodule update"
  8. # - But if something goes wrong we re-clone the repo from scratch
  9. #
  10. # This is a "best of both worlds" for GIT_STRATEGY: fetch & GIT_STRATEGY: clone
  11. #
  12. # -----------------------------------------------------------------------------
  13. # Common bash
  14. if [[ ! -z ${DEBUG_SHELL} ]]; then
  15. set -x # Activate the expand mode if DEBUG is anything but empty.
  16. fi
  17. set -o errexit # Exit if command failed.
  18. set -o pipefail # Exit if pipe failed.
  19. set -o nounset # Exit if variable not set.
  20. die() {
  21. echo "${1:-"Unknown Error"}" 1>&2
  22. exit ${2:-1}
  23. }
  24. # -----------------------------------------------------------------------------
  25. [ -z ${CI_PROJECT_DIR} ] && die "This internal script should only be run by a Gitlab CI runner."
  26. [ -z ${GITLAB_SSH_SERVER} ] && die "GITLAB_SSH_SERVER should be defined to run mirror-submodule-update.sh"
  27. [ -z ${CI_REPOSITORY_URL} ] && die "CI_REPOSITORY_URL should be defined to run mirror-submodule-update.sh"
  28. [ -z ${CI_COMMIT_SHA} ] && die "CI_COMMIT_SHA should be defined to run mirror-submodule-update.sh"
  29. DONT_USE_MIRROR=${DONT_USE_MIRROR:-"0"}
  30. ERR_CANNOT_UPDATE=13
  31. SCRIPT_DIR=$(dirname -- "${0}")
  32. update_submodules() {
  33. if [ "${DONT_USE_MIRROR}" = "1" ]; then
  34. git submodule update --init --recursive
  35. else
  36. ${SCRIPT_DIR}/mirror-submodule-update.sh || return $?
  37. fi
  38. }
  39. del_files() {
  40. DELETED_FILES=$(mktemp --tmpdir -d tmp_XXXX)
  41. # if non-empty
  42. [ "$(ls -A .)" ] && ( shopt -s dotglob; mv * "${DELETED_FILES}/" )
  43. trap 'del_files_rollback' ERR
  44. }
  45. del_files_confirm() {
  46. [ -d "${DELETED_FILES}" ] && rm -rf "${DELETED_FILES}"
  47. trap ERR
  48. }
  49. del_files_rollback() {
  50. [ "$(ls -A .)" ] && [ "$(ls -A ${DELETED_FILES}/)" ] && ( shopt -s dotglob; rm -rf * )
  51. [ "$(ls -A ${DELETED_FILES}/)" ] && ( shopt -s dotglob; mv "${DELETED_FILES}/"* . )
  52. [ -d "${DELETED_FILES}" ] && rmdir "${DELETED_FILES}"
  53. trap ERR
  54. }
  55. RETRIES=10
  56. # we're in gitlab-ci's build phase, so GET_SOURCES_ATTEMPTS doesn't apply here...
  57. # For the first time, we try the fastest way.
  58. for try in `seq $RETRIES`; do
  59. echo "Trying to add submodules to existing repo..."
  60. update_submodules &&
  61. echo "Fetch strategy submodules succeeded" &&
  62. exit 0
  63. git submodule foreach --recursive "git reset --hard HEAD && git submodule deinit --force -- . || true"
  64. git reset --hard HEAD && git submodule deinit --force -- . || true
  65. done
  66. # Then we use the clean way.
  67. for try in `seq $RETRIES`; do
  68. cd ${CI_PROJECT_DIR} # we are probably already here but pays to be certain
  69. echo "Trying a clean clone of IDF..."
  70. del_files
  71. git clone ${CI_REPOSITORY_URL} . &&
  72. git checkout ${CI_COMMIT_SHA} &&
  73. update_submodules &&
  74. echo "Clone strategy succeeded" &&
  75. del_files_confirm &&
  76. exit 0
  77. ERR_RES=$?
  78. del_files_rollback
  79. echo "Clean clone failed..."
  80. if [ $ERR_RES -eq $ERR_CANNOT_UPDATE ]; then
  81. echo "###"
  82. echo "### If you have updated one of the submodules,"
  83. echo "### you have to synchronize the local mirrors manually"
  84. echo "###"
  85. echo "### https://gitlab.espressif.cn:6688/idf/esp-idf/wikis/ci-use-guide#submodule-mirroring-for-private-branches"
  86. echo "###"
  87. die "Failed to clone repo & submodules together" $ERR_RES
  88. fi
  89. done
  90. die "Failed to clone repo & submodules together"