Skip to content

Instantly share code, notes, and snippets.

@HenrySpartGlobal
Last active May 25, 2024 23:07
Show Gist options
  • Save HenrySpartGlobal/1e6c782d8f32e0818885e1c717347ed8 to your computer and use it in GitHub Desktop.
Save HenrySpartGlobal/1e6c782d8f32e0818885e1c717347ed8 to your computer and use it in GitHub Desktop.
Auto Deployment of Discord Bot with Systemd and GitHub Actions
# Author: Henry Koleoso
# Put these two files in .github/workflows/
# Prerequisites:
# SSH Key pair - Private key in GitHub Secrets, Public key in /root/.ssh/authorized_keys on your server
# Discord Bot token - In GitHub Secrets or Environment Variables.
# This file is the workflow that handles the initial setup of your Discord bot.
# This job should be run only ONCE. Changes in 'create-systemctl-service' are appended to the systemd.service file, not overwritten so the file will be invalid!
# If you make a mistake, and need to run this job more than once, navigate to /etc/systemd/system
# and just remove everything in the your-service.service file, save it and run systemctl daemon-reload then rerun your pipeline
# However, if you copy paste this file, it's unlikely you wont need to rerun it anyway!
name: Deploy new Bot
on:
# allows the workflow to be run manually
workflow_dispatch:
env:
REPO_NAME: ${{ github.event.repository.name }}
jobs:
install-requirements:
runs-on: ubuntu-latest
steps:
- name: Installing requirements for ${{ env.REPO_NAME }}
uses: fifsky/ssh-action@master
with:
# install the libraries required for your bot
command: |
# pip install discord.py
# pip install discord.py
# etc
host: # 123.45.678.890
user: root
key: ${{ secrets.SSH_PRIVATE_KEY }}
deploy-via-sftp:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- id: SFTPDeploy
uses: wlixcc/SFTP-Deploy-Action@v1.2.1
with:
username: root
server: # 123.45.678.890
port: 22
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
# clones entire github repo
local_path: ./*
# destination of the code on the server
remote_path: /root/${{ env.REPO_NAME }}/
args: '-o ConnectTimeout=5'
# you may or may not need this. It all depends on how your code retrieves your discord token
# environment variables or Github secrets are heavily recommended
add-bot-token:
needs: [ deploy-via-sftp ]
runs-on: ubuntu-latest
timeout-minutes: 2
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
steps:
- id: add-bot-token
uses: fifsky/ssh-action@master
with:
command: |
cd ${{ env.REPO_NAME }}/lib/bot
touch token.0
echo ${{ env.BOT_TOKEN }} > token.0
echo $?
host: # 123.45.678.890
user: root
key: ${{ secrets.SSH_PRIVATE_KEY }}
create-systemctl-service:
needs: [add-bot-token, deploy-via-sftp]
runs-on: ubuntu-latest
steps:
- id: creating-systemctl-service
uses: fifsky/ssh-action@master
with:
# Make sure ExecStart=, WorkingDirectory= and chmod +x point to the same directory. These may be unique to your code setup
command: |
echo "[Unit]
Description=${{ env.REPO_NAME }} Discord Bot
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /root/${{ env.REPO_NAME }}/launcher.py
User=root
Restart=on-failure
RestartSec=30
WorkingDirectory=/root/${{ env.REPO_NAME }}/
[Install]
WantedBy=multi-user.target" >> /etc/systemd/system/${{ env.REPO_NAME }}.service
chmod +x /root/${{ env.REPO_NAME }}/launcher.py
sudo systemctl enable ${{ env.REPO_NAME }}.service
sudo systemctl daemon-reload
sudo systemctl start ${{ env.REPO_NAME }}.service
host: # 123.45.678.890
user: root
key: ${{ secrets.SSH_PRIVATE_KEY }}
create-systemctl-restart:
needs: [create-systemctl-service, add-bot-token, deploy-via-sftp]
runs-on: ubuntu-latest
steps:
- id: create-systemctl-restart-service
uses: fifsky/ssh-action@master
with:
command: |
echo "[Unit]
Description=${{ env.REPO_NAME }} Discord Bot restart
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart ${{ env.REPO_NAME }}.service
[Install]
WantedBy=multi-user.target" >> /etc/systemd/system/${{ env.REPO_NAME }}-watcher.service
sudo systemctl enable ${{ env.REPO_NAME }}-watcher.service
sudo systemctl daemon-reload
sudo systemctl start ${{ env.REPO_NAME }}-watcher.service
host: # 123.45.678.890
user: root
key: ${{ secrets.SSH_PRIVATE_KEY }}
# This workflow restarts your bot whenever you merge into the branch called 'main'
name: Update Bot
on:
push:
branches: [ main ]
workflow_dispatch:
env:
REPO_NAME: ${{ github.event.repository.name }}
jobs:
deploy-via-sftp:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: SFTP Deploy
uses: wlixcc/SFTP-Deploy-Action@v1.2.1
with:
username: root
server: # 123.45.678.890
port: 22
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: ./*
remote_path: /root/${{ env.REPO_NAME }}/
args: '-o ConnectTimeout=5'
restart-bot:
needs: [deploy-via-sftp]
runs-on: ubuntu-latest
steps:
- id: creating-systemctl-service
uses: fifsky/ssh-action@master
with:
command: sudo systemctl start ${{ env.REPO_NAME }}-watcher.service
host: # 123.45.678.890
user: root
key: ${{ secrets.SSH_PRIVATE_KEY }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment