From 1ee7bb8630bbdecd482ef39bd955bd4669790300 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 29 Dec 2023 19:51:59 +0000 Subject: [PATCH] Update drone script to use rsync (#121) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Update drone pipelines to use resync > **NOTE:** This requires a new repository secret - `ssh_key` > **NOTE:** This requires moving the cards directory into the new appdata folder #56 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - This can only be tested once merged, so no testing has been done, but a check has been done by me as a review # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/121 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- .dev.env | 2 + .drone.yml | 66 +++++++++++++++++++-------- .prod.env | 2 + .stage.env | 2 + src/Functions/CardMetadataFunction.ts | 2 +- src/bot.ts | 3 +- src/buttonEvents/Reroll.ts | 2 +- src/commands/drop.ts | 2 +- src/commands/gdrivesync.ts | 2 +- 9 files changed, 58 insertions(+), 25 deletions(-) diff --git a/.dev.env b/.dev.env index 14ad89c..46544de 100644 --- a/.dev.env +++ b/.dev.env @@ -16,6 +16,8 @@ BOT_ENV=4 ABOUT_FUNDING= ABOUT_REPO= +DATA_DIR= + DB_HOST=127.0.0.1 DB_PORT=3301 DB_NAME=carddrop diff --git a/.drone.yml b/.drone.yml index 3756b47..d8ca0e7 100644 --- a/.drone.yml +++ b/.drone.yml @@ -4,16 +4,31 @@ kind: pipeline name: deployment steps: -- name: deploy - image: appleboy/drone-ssh +- name: build + image: node + commands: + - npm ci + - npm run build +- name: test + image: node + commands: + - npm test +- name: deploy to prod + image: drillster/drone-rsync settings: - host: 192.168.68.120 - username: vylpes - password: - from_secret: ssh_password - port: 22 + hosts: + - "minisforum.local" + user: vylpes + key: + from_secret: ssh_key + source: . + target: ~/apps/card-drop/card-drop_prod + recursive: true script: - - sh /home/vylpes/scripts/card-drop/deploy_prod.sh + - cd ~/apps/card-drop/card-drop_prod + - docker compose --file docker-compose.prod.yml up -d + - sleep 10 + - pm2 restart card-drop_prod || pm2 start --name card-drop_prod dist/bot.js trigger: event: @@ -25,16 +40,31 @@ kind: pipeline name: staging steps: -- name: stage - image: appleboy/drone-ssh +- name: build + image: node + commands: + - npm ci + - npm run build +- name: test + image: node + commands: + - npm test +- name: deploy to stage + image: drillster/drone-rsync settings: - host: 192.168.68.120 - username: vylpes - password: - from_secret: ssh_password - port: 22 + hosts: + - "minisforum.local" + user: vylpes + key: + from_secret: ssh_key + source: . + target: ~/apps/card-drop/card-drop_stage + recursive: true script: - - sh /home/vylpes/scripts/card-drop/deploy_stage.sh + - cd ~/apps/card-drop/card-drop_stage + - docker compose --file docker-compose.stage.yml up -d + - sleep 10 + - pm2 restart card-drop_stage || pm2 start --name card-drop_stage dist/bot.js trigger: branch: @@ -53,17 +83,13 @@ steps: commands: - npm ci - npm run build - - name: test image: node commands: - - npm ci - npm test trigger: branch: - - main - - develop - hotfix/* - feature/* - renovate/* diff --git a/.prod.env b/.prod.env index df28009..d252f1f 100644 --- a/.prod.env +++ b/.prod.env @@ -16,6 +16,8 @@ BOT_ENV=1 ABOUT_FUNDING= ABOUT_REPO= +DATA_DIR=/home/vylpes/appdata/card-drop/card-drop_prod + DB_HOST=127.0.0.1 DB_PORT=3321 DB_NAME=carddrop diff --git a/.stage.env b/.stage.env index 79dcc48..3ffed38 100644 --- a/.stage.env +++ b/.stage.env @@ -16,6 +16,8 @@ BOT_ENV=2 ABOUT_FUNDING= ABOUT_REPO= +DATA_DIR=/home/vylpes/appdata/card-drop/card-drop_stage + DB_HOST=127.0.0.1 DB_PORT=3311 DB_NAME=carddrop diff --git a/src/Functions/CardMetadataFunction.ts b/src/Functions/CardMetadataFunction.ts index 1d52a27..eefe2a7 100644 --- a/src/Functions/CardMetadataFunction.ts +++ b/src/Functions/CardMetadataFunction.ts @@ -26,7 +26,7 @@ export default class CardMetadataFunction { private static async FindMetadataJSONs(): Promise { const res: SeriesMetadata[] = []; - const seriesJSONs = await glob(path.join(process.cwd(), 'cards', '/**/*.json')); + const seriesJSONs = await glob(path.join(process.env.DATA_DIR!, 'cards', '/**/*.json')); for (let jsonPath of seriesJSONs) { console.log(`Reading file ${jsonPath}`); diff --git a/src/bot.ts b/src/bot.ts index 8bddbf4..d880244 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -14,6 +14,7 @@ const requiredConfigs: string[] = [ "BOT_OWNERID", "BOT_CLIENTID", "BOT_ENV", + "DATA_DIR", "DB_HOST", "DB_PORT", "DB_AUTH_USER", @@ -39,7 +40,7 @@ Registry.RegisterCommands(); Registry.RegisterEvents(); Registry.RegisterButtonEvents(); -if (!existsSync(`${process.cwd()}/cards`) && process.env.GDRIVESYNC_AUTO && process.env.GDRIVESYNC_AUTO == 'true') { +if (!existsSync(`${process.env.DATA_DIR}/cards`) && process.env.GDRIVESYNC_AUTO && process.env.GDRIVESYNC_AUTO == 'true') { console.log("Card directory not found, syncing..."); CoreClient.AllowDrops = false; diff --git a/src/buttonEvents/Reroll.ts b/src/buttonEvents/Reroll.ts index 3b86e72..7241db7 100644 --- a/src/buttonEvents/Reroll.ts +++ b/src/buttonEvents/Reroll.ts @@ -31,7 +31,7 @@ export default class Reroll extends ButtonEvent { let image: Buffer; const imageFileName = randomCard.card.path.split("/").pop()!; - image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path)); + image = readFileSync(path.join(process.env.DATA_DIR!, 'cards', randomCard.card.path)); await interaction.deferReply(); diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 2ecd936..fe08241 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -39,7 +39,7 @@ export default class Drop extends Command { let image: Buffer; const imageFileName = randomCard.card.path.split("/").pop()!; - image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path)); + image = readFileSync(path.join(process.env.DATA_DIR!, 'cards', randomCard.card.path)); await interaction.deferReply(); diff --git a/src/commands/gdrivesync.ts b/src/commands/gdrivesync.ts index 8b60722..169e63c 100644 --- a/src/commands/gdrivesync.ts +++ b/src/commands/gdrivesync.ts @@ -29,7 +29,7 @@ export default class Gdrivesync extends Command { CoreClient.AllowDrops = false; - exec(`rclone sync card-drop-gdrive: ${process.cwd()}/cards`, async (error: ExecException | null) => { + exec(`rclone sync card-drop-gdrive: ${process.env.DATA_DIR}/cards`, async (error: ExecException | null) => { if (error) { await interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`); await Config.SetValue('safemode', 'true');