docker-run 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. set -o errexit -o pipefail
  3. DIR=$(dirname "${0}")
  4. MAIN_DIR=$(readlink -f "${DIR}/..")
  5. if [ -L "${MAIN_DIR}/.git/config" ]; then
  6. # Support git-new-workdir
  7. GIT_DIR="$(dirname "$(realpath "${MAIN_DIR}/.git/config")")"
  8. else
  9. # Support git-worktree
  10. GIT_DIR="$(cd "${MAIN_DIR}" && git rev-parse --no-flags --git-common-dir)"
  11. fi
  12. if test -z "${IMAGE}" ; then
  13. # shellcheck disable=SC2016
  14. IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \
  15. sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g')
  16. fi
  17. declare -a docker_opts=(
  18. -i
  19. --rm
  20. --user "$(id -u):$(id -g)"
  21. --workdir "$(pwd)"
  22. --security-opt label=disable
  23. --network host
  24. )
  25. declare -a mountpoints=(
  26. "${MAIN_DIR}"
  27. "$(pwd)"
  28. )
  29. # curl lists (and recognises and uses) other types of *_proxy variables,
  30. # but only those make sense for Buildroot:
  31. for env in all_proxy http_proxy https_proxy ftp_proxy no_proxy; do
  32. if [ "${!env}" ]; then
  33. docker_opts+=( --env "${env}" )
  34. # The lower-case variant takes precedence on the upper-case one
  35. # (dixit curl)
  36. continue
  37. fi
  38. # http_proxy is only lower-case (dixit curl)
  39. if [ "${env}" = http_proxy ]; then
  40. continue
  41. fi
  42. # All the others also exist in the upper-case variant
  43. env="${env^^}"
  44. if [ "${!env}" ]; then
  45. docker_opts+=( --env "${env}" )
  46. fi
  47. done
  48. # Empty GIT_DIR means that we are not in a workdir, *and* git is too old
  49. # to know about worktrees, so we're not in a worktree either. So it means
  50. # we're in the main git working copy, and thus we don't need to mount the
  51. # .git directory.
  52. if [ "${GIT_DIR}" ]; then
  53. # GIT_DIR in the main working copy (when git supports worktrees) will
  54. # be just '.git', but 'docker run' needs an absolute path. If it is
  55. # not absolute, GIT_DIR is relative to MAIN_DIR. If it's an absolute
  56. # path already (in a wordir), then that's a noop.
  57. GIT_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}")"
  58. mountpoints+=( "${GIT_DIR}" )
  59. # 'repo' stores .git/objects separately.
  60. if [ -L "${GIT_DIR}/objects" ]; then
  61. # GITDIR is already an absolute path, but for symetry
  62. # with the above, keep the same cd+readlink construct.
  63. OBJECTS_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}/objects")"
  64. mountpoints+=( "${OBJECTS_DIR}" )
  65. fi
  66. fi
  67. if [ "${BR2_DL_DIR}" ]; then
  68. mountpoints+=( "${BR2_DL_DIR}" )
  69. docker_opts+=( --env BR2_DL_DIR )
  70. fi
  71. # shellcheck disable=SC2013 # can't use while-read because of the assignment
  72. for dir in $(printf '%s\n' "${mountpoints[@]}" |LC_ALL=C sort -u); do
  73. docker_opts+=( --mount "type=bind,src=${dir},dst=${dir}" )
  74. done
  75. if tty -s; then
  76. docker_opts+=( -t )
  77. fi
  78. exec docker run "${docker_opts[@]}" "${IMAGE}" "${@}"