dockerized.sh 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2008-2020 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. PROJECT_DIR=$(cd "${0%/*}/.." || exit 1; pwd)
  25. if [[ ! $DBE_TAG ]]; then
  26. # If the command failed or not on a tag then use 'master' by default
  27. DBE_TAG=$(git describe --exact-match 2>/dev/null || echo master)
  28. fi
  29. BuildEnvironment=-$1; shift
  30. BuildEnvironment=${BuildEnvironment/-base}
  31. # Determine which tool is available to use
  32. if ! docker --version >/dev/null 2>&1; then
  33. if podman --version >/dev/null 2>&1; then
  34. use_podman=1
  35. # Disable SELinux protection in order to mount Urho3D project root directory from host to container in unprivileged mode
  36. run_option="--security-opt label=disable"
  37. # Podman mount volume as 'noexec' by default but we need 'exec' for Android build (aapt2 permission denied otherwise)
  38. mount_option=,exec
  39. # Podman by default pull from other OS registries before trying 'docker.io', so we need to be more explicit to avoid the retries
  40. registry=docker.io/
  41. else
  42. echo "Could not find Docker client or podman"
  43. exit 1
  44. fi
  45. fi
  46. d () {
  47. if [[ $use_podman ]]; then
  48. podman "$@"
  49. else
  50. docker "$@"
  51. fi
  52. }
  53. if [[ $DBE_REFRESH == 1 ]]; then
  54. d pull "${registry}urho3d/dockerized$BuildEnvironment:$DBE_TAG"
  55. fi
  56. if [[ $GITHUB_ACTIONS ]]; then
  57. mkdir -p $GITHUB_WORKSPACE/build/cache
  58. mount_home_dir="--mount type=bind,source=$GITHUB_WORKSPACE/build/cache,target=/home/urho3d$mount_option"
  59. else
  60. mount_home_dir="--mount type=volume,source=$(id -u).urho3d_home_dir,target=/home/urho3d$mount_option"
  61. interactive=-i
  62. fi
  63. if [[ $use_podman ]] || ( [[ $(d version -f '{{.Client.Version}}') =~ ^([0-9]+)\.0*([0-9]+)\. ]] && (( BASH_REMATCH[1] * 100 + BASH_REMATCH[2] >= 1809 )) ); then
  64. # podman or newer Docker client
  65. d run $interactive -t --rm -h fishtank $run_option \
  66. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  67. --env-file "$PROJECT_DIR/script/.env-file" \
  68. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  69. $mount_home_dir \
  70. "${registry}urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  71. else
  72. # Fallback workaround on older Docker CLI version
  73. d run $interactive -t --rm -h fishtank \
  74. -e HOST_UID="$(id -u)" -e HOST_GID="$(id -g)" -e PROJECT_DIR="$PROJECT_DIR" \
  75. --env-file <(perl -ne 'chomp; print "$_\n" if defined $ENV{$_}' "$PROJECT_DIR/script/.env-file") \
  76. --mount type=bind,source="$PROJECT_DIR",target="$PROJECT_DIR" \
  77. $mount_home_dir \
  78. "urho3d/dockerized$BuildEnvironment:$DBE_TAG" "$@"
  79. fi
  80. # vi: set ts=4 sw=4 expandtab: