Build, Test and Deploy Discord Bot to VPS #2
This file contains hidden or 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
name: Build, Test and Deploy Discord Bot to VPS | |
on: | |
workflow_dispatch: # Manual trigger only | |
# Uncomment below when VPS is ready: | |
# push: | |
# branches: [ "main" ] | |
# paths-ignore: | |
# - 'docs/**' | |
# - '.gitignore' | |
# - 'LICENSE' | |
jobs: | |
build-test-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version-file: .nvmrc | |
- name: Install dependencies | |
run: | | |
if [ -f package-lock.json ]; then | |
npm ci --no-audit --no-fund | |
else | |
npm install --no-audit --no-fund | |
fi | |
- name: Lint | |
run: npm run lint | |
- name: Build bot | |
run: npm run build:ci | |
- name: Run tests | |
run: npm test:ci | |
- name: Package build output | |
run: tar czf bot-build.tar.gz ./dist | |
- name: Copy build artifact to VPS | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.VPS_HOST }} | |
username: ${{ secrets.VPS_USER }} | |
key: ${{ secrets.VPS_SSH_KEY }} | |
source: "bot-build.tar.gz" | |
target: "/home/${{ secrets.VPS_USER }}/discord-bot/" | |
- name: Create .env file on VPS | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.VPS_HOST }} | |
username: ${{ secrets.VPS_USER }} | |
key: ${{ secrets.VPS_SSH_KEY }} | |
script: | | |
echo "DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}" > /home/${{ secrets.VPS_USER }}/discord-bot/.env | |
echo "CLIENT_ID=${{ secrets.CLIENT_ID }}" >> /home/${{ secrets.VPS_USER }}/discord-bot/.env | |
- name: Extract and restart bot on VPS | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.VPS_HOST }} | |
username: ${{ secrets.VPS_USER }} | |
key: ${{ secrets.VPS_SSH_KEY }} | |
script: | | |
cd /home/${{ secrets.VPS_USER }}/discord-bot/ | |
tar xzf bot-build.tar.gz | |
pm2 restart bot || pm2 start ./dist/index.js --name bot |