Skip to content

Instantly share code, notes, and snippets.

@emptyacies
Forked from HenrySpartGlobal/deploy-new-bot.yml
Created August 28, 2023 21:23
Show Gist options
  • Select an option

  • Save emptyacies/6e6d63590eb46d8825c1e9f0d047c8b4 to your computer and use it in GitHub Desktop.

Select an option

Save emptyacies/6e6d63590eb46d8825c1e9f0d047c8b4 to your computer and use it in GitHub Desktop.

Revisions

  1. @HenrySpartGlobal HenrySpartGlobal revised this gist Oct 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy-new-bot.yml
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    # 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'll need to rerun it anyway!
    # However, if you copy paste this file, it's unlikely you wont need to rerun it anyway!


    name: Deploy new Bot
  2. @HenrySpartGlobal HenrySpartGlobal revised this gist Oct 1, 2022. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions deploy-new-bot.yml
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,14 @@
    # 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.

    # The workflow that handles the initial setup of your discord bot.
    # This job should, ideally, be run only ONCE. If you make no changes to the systemd file in create-systemctl-service it's fine. However, changes are appended to the systemd 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 and save it. Then re-run your pipeline.
    # 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'll need to rerun it anyway!


    name: Deploy new Bot
    on:
  3. @HenrySpartGlobal HenrySpartGlobal revised this gist Oct 1, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion deploy-new-bot.yml
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@
    # This job should, ideally, be run only ONCE. If you make no changes to the systemd file in create-systemctl-service it's fine. However, changes are appended to the systemd 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 and save it. Then re-run your pipeline.

    # Restart bot workflow file:
    name: Deploy new Bot
    on:
    # allows the workflow to be run manually
  4. @HenrySpartGlobal HenrySpartGlobal revised this gist Oct 1, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy-new-bot.yml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Author: Henry Koleoso
    # Put these two files in .github/workflows/
    # Prerequisites:
    # SSH Key pair - Private key in GitHub Secrets, Public key in authorized_keys on your server
    # 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.

    # The workflow that handles the initial setup of your discord bot.
  5. @HenrySpartGlobal HenrySpartGlobal revised this gist Oct 1, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy-new-bot.yml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Author: Henry Koleoso
    # Put these two files in .github/workflows/
    # Prerequisites:
    # SSH Key pair - Private key in GitHub Secrets, Public key in authorized_keys / known_hosts on your server
    # SSH Key pair - Private key in GitHub Secrets, Public key in authorized_keys on your server
    # Discord Bot token - In GitHub Secrets or Environment Variables.

    # The workflow that handles the initial setup of your discord bot.
  6. @HenrySpartGlobal HenrySpartGlobal created this gist Oct 1, 2022.
    122 changes: 122 additions & 0 deletions deploy-new-bot.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    # Author: Henry Koleoso
    # Put these two files in .github/workflows/
    # Prerequisites:
    # SSH Key pair - Private key in GitHub Secrets, Public key in authorized_keys / known_hosts on your server
    # Discord Bot token - In GitHub Secrets or Environment Variables.

    # The workflow that handles the initial setup of your discord bot.
    # This job should, ideally, be run only ONCE. If you make no changes to the systemd file in create-systemctl-service it's fine. However, changes are appended to the systemd 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 and save it. Then re-run your pipeline.

    # Restart bot workflow file:
    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/[email protected]
    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 }}
    35 changes: 35 additions & 0 deletions update-bot.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # 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/[email protected]
    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 }}