Skip to content

Instantly share code, notes, and snippets.

@WesleyAC
Last active August 12, 2024 11:50
Show Gist options
  • Save WesleyAC/b3aaa0292579158ad566c140415c875d to your computer and use it in GitHub Desktop.
Save WesleyAC/b3aaa0292579158ad566c140415c875d to your computer and use it in GitHub Desktop.
Simple rust build and deploy script — https://blog.wesleyac.com/posts/simple-deploy-script
#!/usr/bin/env bash
cd $(dirname $0)
docker run --rm -it -v "$(pwd)":/home/rust/src -v cargo-git:/home/rust/.cargo/git -v cargo-registry:/home/rust/.cargo/registry -v "$(pwd)/target/":/home/rust/src/target ekidd/rust-musl-builder:nightly-2021-01-01 sudo chown -R rust:rust /home/rust/.cargo/git /home/rust/.cargo/registry /home/rust/src/target
docker run --rm -it -v "$(pwd)":/home/rust/src -v cargo-git:/home/rust/.cargo/git -v cargo-registry:/home/rust/.cargo/registry -v "$(pwd)/target/":/home/rust/src/target ekidd/rust-musl-builder:nightly-2021-01-01 cargo build --release
#!/usr/bin/env bash
set -e
cd $(dirname $0)
if [ "$#" -ne 2 ]; then
echo "usage: $0 user@server-address /path/to/remote/directory/"
exit 1
fi
SERVER_SSH=$1
SERVER_PATH=$2
BINARY_NAME="example"
SERVER_RESTART_COMMAND="systemctl restart $BINARY_NAME"
./build.sh
OUTFILE="./target/x86_64-unknown-linux-musl/release/$BINARY_NAME"
COMMIT_HASH=$(git rev-parse HEAD)
BUILD_TIMESTAMP=$(TZ=UTC date -u +"%s")
FILE_HASH=$(b2sum $OUTFILE | cut -f1 -d' ')
REMOTE_FILENAME="$BINARY_NAME-$BUILD_TIMESTAMP-$COMMIT_HASH-$FILE_HASH"
ssh $SERVER_SSH "mkdir -p $SERVER_PATH/versions/"
scp "$OUTFILE" "$SERVER_SSH:$SERVER_PATH/versions/$REMOTE_FILENAME"
ssh -q -T $SERVER_SSH <<EOL
nohup sh -c "\
rm "$SERVER_PATH/$BINARY_NAME" && \
ln -s "$SERVER_PATH/versions/$REMOTE_FILENAME" "$SERVER_PATH/$BINARY_NAME" && \
$SERVER_RESTART_COMMAND"
EOL
[Unit]
Description=Example server
After=network.target
[Service]
ExecStart=/home/example/example
User=example
Group=example
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment