dockerized.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2008-2022 the Urho3D project.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22. #
  23. if [[ $# -eq 0 ]]; then echo "Usage: dockerized.sh linux|mingw|android|rpi|arm|web [command]"; exit 1; fi
  24. if [[ $(id -u) -eq 0 ]]; then echo "Should not run using sudo or root user"; exit 1; fi
  25. SCRIPT_DIR=$(cd "${0%/*}" || exit 1; pwd)
  26. PROJECT_DIR=$(cd "${0%/*}/.." || exit 1; pwd)
  27. # Determine which tool is available to use
  28. if ! docker --version >/dev/null 2>&1; then
  29. if podman --version >/dev/null 2>&1; then
  30. use_podman=1
  31. # Disable SELinux protection in order to mount Urho3D project root directory from host to container in unprivileged mode
  32. run_option="--security-opt label=disable"
  33. # Podman mount volume as 'noexec' by default but we need 'exec' for Android build (aapt2 permission denied otherwise)
  34. mount_option=,exec
  35. # Podman by default pull from other OS registries before trying 'docker.io', so we need to be more explicit to avoid the retries
  36. registry=docker.io/
  37. else
  38. echo "Could not find Docker client or podman"
  39. exit 1
  40. fi
  41. fi
  42. d () {
  43. if [[ $use_podman ]]; then
  44. podman "$@"
  45. else
  46. docker "$@"
  47. fi
  48. }
  49. if [[ ! $DBE_NAME ]]; then
  50. DBE_NAME=${registry}urho3d/dockerized
  51. fi
  52. if [[ ! $DBE_TAG ]]; then
  53. # If the command failed or not on a tag then use 'master' by default
  54. DBE_TAG=$(git describe --exact-match 2>/dev/null || echo master)
  55. fi
  56. platform=$1; shift
  57. if [[ "$DBE_NAMING_SCHEME" == "tag" ]]; then
  58. dbe_image=$DBE_NAME:$DBE_TAG-$platform
  59. else
  60. dbe_image=$DBE_NAME-$platform:$DBE_TAG
  61. fi
  62. if [[ $DBE_REFRESH == 1 ]]; then
  63. d pull $dbe_image
  64. fi
  65. if [[ $GITHUB_ACTIONS ]]; then
  66. mkdir -p $GITHUB_WORKSPACE/build/cache
  67. mount_home_dir="--mount type=bind,source=$GITHUB_WORKSPACE/build/cache,target=/home/urho3d$mount_option"
  68. else
  69. mount_home_dir="--mount type=volume,source=$(id -u).urho3d_home_dir,target=/home/urho3d$mount_option"
  70. interactive=-i
  71. fi
  72. if [[ $use_podman ]] || ( [[ $(d version -f '{{.Client.Version}}') =~ ^([0-9]+)\.0*([0-9]+)\. ]] && (( BASH_REMATCH[1] * 100 + BASH_REMATCH[2] >= 1809 )) ); then
  73. # podman or newer Docker client
  74. d run $interactive -t --rm -h fishtank $run_option \
  75. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  76. --env-file "$SCRIPT_DIR/.env-file" \
  77. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  78. $mount_home_dir \
  79. $dbe_image "$@"
  80. else
  81. # Fallback workaround on older Docker CLI version
  82. d run $interactive -t --rm -h fishtank \
  83. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  84. --env-file <(perl -ne 'chomp; print "$_\n" if defined $ENV{$_}' "$SCRIPT_DIR/.env-file") \
  85. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  86. $mount_home_dir \
  87. $dbe_image "$@"
  88. fi
  89. # vi: set ts=4 sw=4 expandtab: