spc-alpine-docker 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env sh
  2. # This file is using docker to run commands
  3. # Detect docker can run
  4. if ! which docker >/dev/null; then
  5. echo "Docker is not installed, please install docker first !"
  6. exit 1
  7. fi
  8. DOCKER_EXECUTABLE="docker"
  9. # shellcheck disable=SC2046
  10. if [ $(id -u) -ne 0 ]; then
  11. if ! docker info > /dev/null 2>&1; then
  12. if [ "$SPC_USE_SUDO" != "yes" ]; then
  13. echo "Docker command requires sudo"
  14. # shellcheck disable=SC2039
  15. echo -n 'To use sudo to run docker, run "export SPC_USE_SUDO=yes" and run command again'
  16. exit 1
  17. fi
  18. DOCKER_EXECUTABLE="sudo docker"
  19. fi
  20. fi
  21. # to check if qemu-docker run
  22. if [ "$SPC_USE_ARCH" = "" ]; then
  23. SPC_USE_ARCH=x86_64
  24. fi
  25. case $SPC_USE_ARCH in
  26. x86_64)
  27. ALPINE_FROM=alpine:edge
  28. ;;
  29. aarch64)
  30. ALPINE_FROM=multiarch/alpine:aarch64-edge
  31. # shellcheck disable=SC2039
  32. echo -e "\e[033m* Using different arch needs to setup qemu-static for docker !\e[0m"
  33. $DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null
  34. ;;
  35. *)
  36. echo "Current arch is not supported to run in docker: $SPC_USE_ARCH"
  37. exit 1
  38. ;;
  39. esac
  40. if [ "$SPC_USE_MIRROR" = "yes" ]; then
  41. SPC_USE_MIRROR="RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories"
  42. else
  43. SPC_USE_MIRROR="RUN echo 'Using original repo'"
  44. fi
  45. # Detect docker env is setup
  46. if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-$SPC_USE_ARCH; then
  47. echo "Docker container does not exist. Building docker image ..."
  48. ALPINE_DOCKERFILE=$(cat << EOF
  49. FROM $ALPINE_FROM
  50. $SPC_USE_MIRROR
  51. RUN apk update
  52. RUN apk add bash file wget cmake gcc g++ jq autoconf git libstdc++ linux-headers make m4 libgcc binutils bison flex pkgconfig automake curl
  53. RUN apk add build-base xz php81 php81-common php81-pcntl php81-tokenizer php81-phar php81-posix php81-xml composer
  54. RUN mkdir /app
  55. WORKDIR /app
  56. ADD ./src /app/src
  57. ADD ./composer.json /app/composer.json
  58. ADD ./bin /app/bin
  59. RUN composer update --no-dev
  60. EOF
  61. )
  62. echo "$ALPINE_DOCKERFILE" > "$(pwd)"/Dockerfile
  63. $DOCKER_EXECUTABLE build -t cwcc-spc-$SPC_USE_ARCH .
  64. rm "$(pwd)"/Dockerfile
  65. fi
  66. # Check if in ci (local terminal can execute with -it)
  67. if [ -t 0 ]; then
  68. INTERACT=-it
  69. else
  70. INTERACT=''
  71. fi
  72. # Run docker
  73. # shellcheck disable=SC2068
  74. $DOCKER_EXECUTABLE run --rm "$INTERACT" -e SPC_FIX_DEPLOY_ROOT="$(pwd)" -v "$(pwd)"/config:/app/config -v "$(pwd)"/src:/app/src -v "$(pwd)"/buildroot:/app/buildroot -v "$(pwd)"/source:/app/source -v "$(pwd)"/downloads:/app/downloads cwcc-spc-$SPC_USE_ARCH bin/spc $@