Skip to content

Commit

Permalink
Deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadhabibi14 committed Aug 18, 2023
1 parent fffa197 commit bd672b0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ jobs:
port: ${{ secrets.SERVER_PORT }}
script: |
cd /root/wabot
go build main.go
go build main.go
systemctl restart whatsappbot.service
7 changes: 7 additions & 0 deletions .vscode/setting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"yaml.schemas": {
"https://json.schemastore.org/yamllint.json": "*.yaml"
},
"editor.semanticTokenColorCustomizations": {
}
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
26 changes: 26 additions & 0 deletions deploy/build-on-server.sh
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions deploy/whatsappbot.service
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bd672b0

Please sign in to comment.