Sam Jeston
History
44 lines·
2.0 KB
·Bourne Again Shell (bash)
| 1 | #!/usr/bin/env bash |
| 2 | # Builds openhub on a Linux machine and installs or updates it there as a |
| 3 | # systemd service (packaging/linux), over ssh. It builds on the target instead |
| 4 | # of cross-compiling, because libgit2 and SQLite are compiled from C. The |
| 5 | # target needs a C compiler, curl and git. If it has no cargo, the first run |
| 6 | # installs Rust there with rustup. The ssh user needs sudo. |
| 7 | # |
| 8 | # scripts/deploy-server.sh user@host |
| 9 | # |
| 10 | # Uncommitted changes are deployed too. The source and build output are kept on |
| 11 | # the target in ~/.cache/openhub-build, so later builds only recompile openhub. |
| 12 | set -euo pipefail |
| 13 | |
| 14 | host=${1:?usage: scripts/deploy-server.sh user@host} |
| 15 | cd "$(dirname "$0")/.." |
| 16 | version=$(git describe --tags --always --dirty 2>/dev/null || echo dev) |
| 17 | build=.cache/openhub-build # relative to the remote home directory |
| 18 | |
| 19 | echo "Uploading openhub $version to $host:~/$build/src" |
| 20 | # Tracked files plus untracked ones that aren't ignored, skipping deleted files. |
| 21 | # tar -m stamps them with the extraction time, so cargo always rebuilds openhub |
| 22 | # itself (whatever the clock skew between the machines) and reuses dependencies. |
| 23 | git ls-files -z --cached --others --exclude-standard | |
| 24 | while IFS= read -r -d '' f; do if [ -f "$f" ]; then printf '%s\0' "$f"; fi; done | |
| 25 | COPYFILE_DISABLE=1 tar --null -T - -czf - | |
| 26 | ssh "$host" "rm -rf $build/src && mkdir -p $build/src && tar -xzmf - -C $build/src" |
| 27 | |
| 28 | echo "Building on $host (the first build takes several minutes)" |
| 29 | ssh "$host" bash -s -- "$build" <<'EOF' |
| 30 | set -euo pipefail |
| 31 | build=$1 |
| 32 | export PATH=$HOME/.cargo/bin:$PATH |
| 33 | if ! command -v cargo >/dev/null; then |
| 34 | echo "Installing Rust with rustup" |
| 35 | curl -sSf --proto '=https' --tlsv1.2 https://sh.rustup.rs | sh -s -- -y --profile minimal --no-modify-path |
| 36 | fi |
| 37 | cd "$build/src" |
| 38 | CARGO_TARGET_DIR=$HOME/$build/target cargo build --release --locked |
| 39 | EOF |
| 40 | |
| 41 | # A terminal lets sudo ask for a password. |
| 42 | tty= |
| 43 | [ -t 0 ] && tty=-t |
| 44 | ssh $tty "$host" sudo bash "$build/src/packaging/linux/install.sh" "$build/target/release/openhub" "$build/src/packaging/linux/openhub.service" |