build_alpine_apk.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. set -e -o pipefail
  3. ARCHITECTURE="$1"
  4. VERSION="$2"
  5. BINARY_PATH="$3"
  6. OUTPUT_PATH="$4"
  7. if [ -z "$ARCHITECTURE" ] || [ -z "$VERSION" ] || [ -z "$BINARY_PATH" ] || [ -z "$OUTPUT_PATH" ]; then
  8. echo "Usage: $0 <architecture> <version> <binary_path> <output_path>"
  9. exit 1
  10. fi
  11. PROJECT=$(cd "$(dirname "$0")/.."; pwd)
  12. # Convert version to APK format:
  13. # 1.13.0-beta.8 -> 1.13.0_beta8-r0
  14. # 1.13.0-rc.3 -> 1.13.0_rc3-r0
  15. # 1.13.0 -> 1.13.0-r0
  16. APK_VERSION=$(echo "$VERSION" | sed -E 's/-([a-z]+)\.([0-9]+)/_\1\2/')
  17. APK_VERSION="${APK_VERSION}-r0"
  18. ROOT_DIR=$(mktemp -d)
  19. trap 'rm -rf "$ROOT_DIR"' EXIT
  20. # Binary
  21. install -Dm755 "$BINARY_PATH" "$ROOT_DIR/usr/bin/sing-box"
  22. # Config files
  23. install -Dm644 "$PROJECT/release/config/config.json" "$ROOT_DIR/etc/sing-box/config.json"
  24. install -Dm755 "$PROJECT/release/config/sing-box.initd" "$ROOT_DIR/etc/init.d/sing-box"
  25. install -Dm644 "$PROJECT/release/config/sing-box.confd" "$ROOT_DIR/etc/conf.d/sing-box"
  26. # Service files
  27. install -Dm644 "$PROJECT/release/config/sing-box.service" "$ROOT_DIR/usr/lib/systemd/system/sing-box.service"
  28. install -Dm644 "$PROJECT/release/config/[email protected]" "$ROOT_DIR/usr/lib/systemd/system/[email protected]"
  29. # Completions
  30. install -Dm644 "$PROJECT/release/completions/sing-box.bash" "$ROOT_DIR/usr/share/bash-completion/completions/sing-box.bash"
  31. install -Dm644 "$PROJECT/release/completions/sing-box.fish" "$ROOT_DIR/usr/share/fish/vendor_completions.d/sing-box.fish"
  32. install -Dm644 "$PROJECT/release/completions/sing-box.zsh" "$ROOT_DIR/usr/share/zsh/site-functions/_sing-box"
  33. # License
  34. install -Dm644 "$PROJECT/LICENSE" "$ROOT_DIR/usr/share/licenses/sing-box/LICENSE"
  35. # APK metadata
  36. PACKAGES_DIR="$ROOT_DIR/lib/apk/packages"
  37. mkdir -p "$PACKAGES_DIR"
  38. # .conffiles
  39. cat > "$PACKAGES_DIR/.conffiles" <<'EOF'
  40. /etc/conf.d/sing-box
  41. /etc/init.d/sing-box
  42. /etc/sing-box/config.json
  43. EOF
  44. # .conffiles_static (sha256 checksums)
  45. while IFS= read -r conffile; do
  46. sha256=$(sha256sum "$ROOT_DIR$conffile" | cut -d' ' -f1)
  47. echo "$conffile $sha256"
  48. done < "$PACKAGES_DIR/.conffiles" > "$PACKAGES_DIR/.conffiles_static"
  49. # .list (all files, excluding lib/apk/packages/ metadata)
  50. (cd "$ROOT_DIR" && find . -type f -o -type l) \
  51. | sed 's|^\./|/|' \
  52. | grep -v '^/lib/apk/packages/' \
  53. | sort > "$PACKAGES_DIR/.list"
  54. # Build APK
  55. apk mkpkg \
  56. --info "name:sing-box" \
  57. --info "version:${APK_VERSION}" \
  58. --info "description:The universal proxy platform." \
  59. --info "arch:${ARCHITECTURE}" \
  60. --info "license:GPL-3.0-or-later with name use or association addition" \
  61. --info "origin:sing-box" \
  62. --info "url:https://sing-box.sagernet.org/" \
  63. --info "maintainer:nekohasekai <[email protected]>" \
  64. --files "$ROOT_DIR" \
  65. --output "$OUTPUT_PATH"