diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 967e56d..2de885c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,26 +33,41 @@ jobs: run: dotnet test - name: Publish - run: dotnet publish -c Release -r linux-x64 -o deploy --no-self-contained + run: dotnet publish -c Release -r linux-x64 -o publish-output --no-self-contained ./Kattbot/ - name: Generate database migration script if: github.event.inputs.updateDatabase == 'auto' || github.event.inputs.updateDatabase == 'manual' run: | dotnet tool install --global dotnet-ef dotnet tool restore - dotnet ef migrations script --idempotent -p Kattbot.Data.Migrations -o deploy/database_migration.sql + dotnet ef migrations script --idempotent -p Kattbot.Data.Migrations -o publish-output/database_migration.sql env: DOTNET_ENVIRONMENT: Production - - - name: Copy files to remote dir + + - name: Copy publish output to staging folder + uses: appleboy/scp-action@master + with: + host: ${{secrets.VPS_HOST}} + port: ${{secrets.VPS_PORT}} + username: ${{secrets.KATTBOT_USER}} + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + source: "publish-output/" + target: "$HOME/kattbot-staging" + strip_components: 1 + rm: true + + - name: Copy deploy scripts to deploy folder uses: appleboy/scp-action@master with: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} - username: ${{secrets.VPS_USER}} - password: ${{secrets.VPS_PASSWORD}} - source: "/home/runner/work/Kattbot/Kattbot/deploy/" - target: "/home/kattbot-user/kattbot-deploy" + username: ${{secrets.KATTBOT_USER}} + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + source: "deploy/" + target: "$HOME/kattbot-deploy" + strip_components: 1 rm: true - name: Deploy - Stop service @@ -61,26 +76,29 @@ jobs: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} username: ${{secrets.KATTBOT_USER}} - password: ${{secrets.KATTBOT_PASSWORD}} - script: systemctl --user stop kattbot + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + script: systemctl --user stop --quiet kattbot || true - - name: Deploy - Run deploy script + - name: Deploy - Prepare deploy scripts uses: appleboy/ssh-action@master with: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} - username: ${{secrets.VPS_USER}} - password: ${{secrets.VPS_PASSWORD}} - script: sudo /home/kattbot-user/kattbot-deploy.sh - - - name: Deploy - Change Kattbot permission + username: ${{secrets.KATTBOT_USER}} + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + script: chmod +x $HOME/kattbot-deploy/*.sh + + - name: Deploy - Run deploy script uses: appleboy/ssh-action@master with: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} - username: ${{secrets.VPS_USER}} - password: ${{secrets.VPS_PASSWORD}} - script: sudo chmod 777 /home/kattbot-user/kattbot/Kattbot + username: ${{secrets.KATTBOT_USER}} + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + script: $HOME/kattbot-deploy/kattbot-deploy.sh - name: Deploy - Backup database if: github.event.inputs.updateDatabase == 'auto' || github.event.inputs.updateDatabase == 'manual' @@ -88,9 +106,10 @@ jobs: with: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} - username: ${{secrets.VPS_USER}} - password: ${{secrets.VPS_PASSWORD}} - script: sudo /home/kattbot-user/kattbot-backup-db.sh + username: ${{secrets.KATTBOT_USER}} + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + script: $HOME/kattbot-deploy/kattbot-backup-db.sh - name: Deploy - Run database migration script if: github.event.inputs.updateDatabase == 'auto' @@ -98,9 +117,10 @@ jobs: with: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} - username: ${{secrets.VPS_USER}} - password: ${{secrets.VPS_PASSWORD}} - script: sudo psql -U postgres -d kattbot -q -f /home/kattbot-user/kattbot/database_migration.sql + username: ${{secrets.KATTBOT_USER}} + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + script: psql -U postgres -d kattbot -q -f $HOME/kattbot/database_migration.sql - name: Deploy - Start service if: github.event.inputs.updateDatabase == 'no' || github.event.inputs.updateDatabase == 'auto' @@ -109,5 +129,6 @@ jobs: host: ${{secrets.VPS_HOST}} port: ${{secrets.VPS_PORT}} username: ${{secrets.KATTBOT_USER}} - password: ${{secrets.KATTBOT_PASSWORD}} - script: systemctl --user start kattbot + key: ${{secrets.KATTBOT_KEY}} + passphrase: ${{secrets.KATTBOT_PASSPHRASE}} + script: systemctl --user enable kattbot && systemctl --user start kattbot diff --git a/Kattbot.sln b/Kattbot.sln index ecc7849..9316651 100644 --- a/Kattbot.sln +++ b/Kattbot.sln @@ -15,9 +15,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .github\dependabot.yml = .github\dependabot.yml Directory.Build.props = Directory.Build.props Dotnet_cheatsheet.md = Dotnet_cheatsheet.md - kattbot-backup-db.sh = kattbot-backup-db.sh - kattbot-deploy.sh = kattbot-deploy.sh - kattbot.service = kattbot.service + deploy\kattbot-backup-db.sh = deploy\kattbot-backup-db.sh + deploy\kattbot-deploy.sh = deploy\kattbot-deploy.sh + deploy\kattbot.service = deploy\kattbot.service Readme.md = Readme.md .github\workflows\release.yml = .github\workflows\release.yml EndProjectSection diff --git a/Kattbot/Services/PuppeteerFactory.cs b/Kattbot/Services/PuppeteerFactory.cs index 030ce7e..2896ec2 100644 --- a/Kattbot/Services/PuppeteerFactory.cs +++ b/Kattbot/Services/PuppeteerFactory.cs @@ -1,6 +1,5 @@ using System.IO; using System.Linq; -using System.Reflection; using System.Runtime.InteropServices; using System.Threading.Tasks; using PuppeteerSharp; diff --git a/kattbot-backup-db.sh b/deploy/kattbot-backup-db.sh similarity index 55% rename from kattbot-backup-db.sh rename to deploy/kattbot-backup-db.sh index dc581a4..d4d4477 100644 --- a/kattbot-backup-db.sh +++ b/deploy/kattbot-backup-db.sh @@ -4,7 +4,7 @@ su - postgres -c pg_dump kattbot > /tmp/kattbot.bak # create folder if it doesnt'exit -mkdir -p /home/kattbot-user/kattbot-db-backups +mkdir -p $HOME/kattbot-db-backups # move backup file to backups folder -mv /tmp/kattbot.bak /home/kattbot-user/kattbot-db-backups/kattbot-$(date +%Y-%m-%d-%H:%M).bak \ No newline at end of file +mv /tmp/kattbot.bak $HOME/kattbot-db-backups/kattbot-$(date +%Y-%m-%d-%H:%M).bak \ No newline at end of file diff --git a/deploy/kattbot-deploy.sh b/deploy/kattbot-deploy.sh new file mode 100644 index 0000000..e2febbf --- /dev/null +++ b/deploy/kattbot-deploy.sh @@ -0,0 +1,12 @@ +#! /bin/bash + +# remove directory +rm -rf $HOME/kattbot +mkdir -p $HOME/kattbot + +# copy files from staging to app directory +cp -r $HOME/kattbot-staging/. $HOME/kattbot + +# copy service file and create path if it doesn't exist +mkdir -p $HOME/.config/systemd/user +cp $HOME/kattbot-deploy/kattbot.service $HOME/.config/systemd/user/ \ No newline at end of file diff --git a/kattbot.service b/deploy/kattbot.service similarity index 100% rename from kattbot.service rename to deploy/kattbot.service diff --git a/kattbot-deploy.sh b/kattbot-deploy.sh deleted file mode 100644 index e44482c..0000000 --- a/kattbot-deploy.sh +++ /dev/null @@ -1,10 +0,0 @@ -#! /bin/bash - -# empty directory -rm -r /home/kattbot-user/kattbot - -# recreate it -mkdir -p /home/kattbot-user/kattbot - -# copy files from deploy to app directory -cp -r /home/kattbot-user/kattbot-deploy/github/workspace/deploy/. /home/kattbot-user/kattbot \ No newline at end of file