samjeston / openhub Public
44 lines· 2.0 KB ·Bourne Again Shell (bash)
Raw
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.
12set -euo pipefail
13
14host=${1:?usage: scripts/deploy-server.sh user@host}
15cd "$(dirname "$0")/.."
16version=$(git describe --tags --always --dirty 2>/dev/null || echo dev)
17build=.cache/openhub-build # relative to the remote home directory
18
19echo "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.
23git 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
28echo "Building on $host (the first build takes several minutes)"
29ssh "$host" bash -s -- "$build" <<'EOF'
30set -euo pipefail
31build=$1
32export PATH=$HOME/.cargo/bin:$PATH
33if ! 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
36fi
37cd "$build/src"
38CARGO_TARGET_DIR=$HOME/$build/target cargo build --release --locked
39EOF
40
41# A terminal lets sudo ask for a password.
42tty=
43[ -t 0 ] && tty=-t
44ssh $tty "$host" sudo bash "$build/src/packaging/linux/install.sh" "$build/target/release/openhub" "$build/src/packaging/linux/openhub.service"