From bd672b092cd9496c2dc9387c9bf802da47a6e9d8 Mon Sep 17 00:00:00 2001 From: Ahmad Habibi Date: Fri, 18 Aug 2023 15:09:46 +0800 Subject: [PATCH] Deploy script --- .github/workflows/deploy.yaml | 3 ++- .vscode/setting.json | 7 +++++++ README.md | 14 ++++++++++++++ deploy/build-on-server.sh | 26 ++++++++++++++++++++++++++ deploy/deploy.sh | 25 +++++++++++++++++++++++++ deploy/whatsappbot.service | 12 ++++++++++++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 .vscode/setting.json create mode 100755 deploy/build-on-server.sh create mode 100755 deploy/deploy.sh create mode 100644 deploy/whatsappbot.service diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 21bee09..8cb852d 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -34,4 +34,5 @@ jobs: port: ${{ secrets.SERVER_PORT }} script: | cd /root/wabot - go build main.go \ No newline at end of file + go build main.go + systemctl restart whatsappbot.service \ No newline at end of file diff --git a/.vscode/setting.json b/.vscode/setting.json new file mode 100644 index 0000000..a627eaf --- /dev/null +++ b/.vscode/setting.json @@ -0,0 +1,7 @@ +{ + "yaml.schemas": { + "https://json.schemastore.org/yamllint.json": "*.yaml" + }, + "editor.semanticTokenColorCustomizations": { + } +} \ No newline at end of file diff --git a/README.md b/README.md index 0176113..7109c04 100644 --- a/README.md +++ b/README.md @@ -56,4 +56,18 @@ ExecStart=/root/wabot/main [Install] WantedBy=multi-user.target +``` + +Enable SystemD service: +```sh +sudo systemctl start whatsappbot.service +``` + +#### Deploy with existing script + +Go to `/deploy` directory, and run script + +```shell +# Run this script, note that your ssh private key is authorized by server +./deploy.sh ``` \ No newline at end of file diff --git a/deploy/build-on-server.sh b/deploy/build-on-server.sh new file mode 100755 index 0000000..85969c3 --- /dev/null +++ b/deploy/build-on-server.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -x + +SERVICE_FILE="whatsappbot.service" +SERVICE_DIR="/lib/systemd/system" + +# Go to project directory +cd /root/wabot + +# Install modules and build Golang +go get +go build main.go +# Check if there is a systemd service for golang program +if [ -f "${SERVICE_DIR}/${SERVICE_FILE}" ]; then + echo "The service file '${SERVICE_FILE}' exists in ${SERVICE_DIR}" + systemctl restart $SERVICE_FILE +else + echo -e \ + "\ + The file '${SERVICE_FILE}' does not exist in ${SERVICE_DIR} \n\ + SystemD service will be created now\ + " + cp ${SERVICE_FILE} ${SERVICE_DIR}/ + systemctl daemon-reload + systemctl start $SERVICE_FILE +fi \ No newline at end of file diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100755 index 0000000..e6045d7 --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -x + +# Remote server information +SERVER_USER="root" +SERVER_HOST="203.194.113.211" +SSH_PORT=22 + +# Define SSH private key +SSH_PRIVATE_KEY="~/.ssh/habi" + +# Project directory on the remote server +PROJECT_DIR="/root/wabot" + +# Send project to the remote server +rsync -avz \ + --exclude=".git" \ + --exclude=".github" \ + "ssh -p ${SSH_PORT} -i ${SSH_PRIVATE_KEY}" \ + ./ $SERVER_USER@$SERVER_HOST:$PROJECT_DIR + +# Run a remote script on a local machine over ssh +ssh -p $SSH_PORT -i $SSH_PRIVATE_KEY \ + $SERVER_USER@$SERVER_HOST \ + 'bash' $PROJECT_DIR/deploy/build-on-server.sh \ No newline at end of file diff --git a/deploy/whatsappbot.service b/deploy/whatsappbot.service new file mode 100644 index 0000000..e46e740 --- /dev/null +++ b/deploy/whatsappbot.service @@ -0,0 +1,12 @@ +[Unit] +Description=Habi-Bot (WhatsApp Bot) + +[Service] +User=root +Type=simple +Restart=on-failure +WorkingDirectory=/root/wabot +ExecStart=/root/wabot/main + +[Install] +WantedBy=multi-user.target \ No newline at end of file