Last active
August 12, 2024 11:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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