dockerized.sh 2.9 KB

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