2
1

docker-run 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. )
  24. declare -a mountpoints=(
  25. "${MAIN_DIR}"
  26. "$(pwd)"
  27. )
  28. # Empty GIT_DIR means that we are not in a workdir, *and* git is too old
  29. # to know about worktrees, so we're not in a worktree either. So it means
  30. # we're in the main git working copy, and thus we don't need to mount the
  31. # .git directory.
  32. if [ "${GIT_DIR}" ]; then
  33. # GIT_DIR in the main working copy (when git supports worktrees) will
  34. # be just '.git', but 'docker run' needs an absolute path. If it is
  35. # not absolute, GIT_DIR is relative to MAIN_DIR. If it's an absolute
  36. # path already (in a wordir), then that's a noop.
  37. GIT_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}")"
  38. mountpoints+=( "${GIT_DIR}" )
  39. # 'repo' stores .git/objects separately.
  40. if [ -L "${GIT_DIR}/objects" ]; then
  41. # GITDIR is already an absolute path, but for symetry
  42. # with the above, keep the same cd+readlink construct.
  43. OBJECTS_DIR="$(cd "${MAIN_DIR}"; readlink -e "${GIT_DIR}/objects")"
  44. mountpoints+=( "${OBJECTS_DIR}" )
  45. fi
  46. fi
  47. if [ "${BR2_DL_DIR}" ]; then
  48. mountpoints+=( "${BR2_DL_DIR}" )
  49. docker_opts+=( --env BR2_DL_DIR )
  50. fi
  51. # shellcheck disable=SC2013 # can't use while-read because of the assignment
  52. for dir in $(printf '%s\n' "${mountpoints[@]}" |LC_ALL=C sort -u); do
  53. docker_opts+=( --mount "type=bind,src=${dir},dst=${dir}" )
  54. done
  55. if tty -s; then
  56. docker_opts+=( -t )
  57. fi
  58. exec docker run "${docker_opts[@]}" "${IMAGE}" "${@}"