Skip to content

Commit

Permalink
Add maintenance mode functionality to deployment workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hiletmis committed Jan 17, 2025
1 parent 2946df1 commit 788bc68
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
22 changes: 8 additions & 14 deletions .github/workflows/deployfe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@ name: Deploy to Github Pages
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
maintenance:
description: 'Maintenance mode'
required: true
default: 'warning'
default: 'disabled'
type: choice
options:
- info
- warning
- debug
tags:
description: 'Test scenario tags'
required: false
type: boolean
environment:
description: 'Environment to run tests against'
type: environment
required: true
- enabled
- delayed
- disabled
schedule:
- cron: '0 8,12,16,20 * * *'
push:
Expand Down Expand Up @@ -79,6 +71,8 @@ jobs:
- name: Install Frontend Packages
if : github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'deploy')
run: pnpm install
- name: Check maintenance mode
run: ACTION=${{ inputs.maintenance }} pnpm maintenance
- name: Build Frontend
if : github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'deploy')
run: pnpm --filter "viewer" build
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"optimize-api-provider-logos": "rimraf ./optimized/api-provider & svgo -q -p 8 -f ./raw/api-providers -o ./optimized/api-provider",
"build": "pnpm run optimize-chain-logos && pnpm run optimize-symbol-logos && pnpm run optimize-api-provider-logos && node scripts/build-svg.js",
"fetch": "node scripts/fetch-missing.js",
"maintenance": "node scripts/toggle-mainteance-mode.js",
"version-check": "node scripts/version-check.js",
"package": "pnpm run build",
"lint": "pnpm run format:check && pnpm run lint:eslint",
Expand Down
66 changes: 66 additions & 0 deletions scripts/toggle-mainteance-mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const fs = require('fs');
const os = require('os');

function setEnvValue(value) {
// read file from hdd & split if from a linebreak to a array
const ENV_VARS = fs.readFileSync('./.env', 'utf8').split(os.EOL);

if (!ENV_VARS) {
console.error('No .env file found');
throw new Error('No .env file found');
}

const line = ENV_VARS.find((line) => {
return line.includes('VITE_APP_MAINTENANCE_START_TIME');
});

if (!line) {
ENV_VARS.push(`VITE_APP_MAINTENANCE_START_TIME=${value}`);
}

// find the env we want based on the key
const target = ENV_VARS.indexOf(line);

// replace the key/value with the new value
ENV_VARS.splice(target, 1, `VITE_APP_MAINTENANCE_START_TIME=${value}`);

// write everything back to the file system
fs.writeFileSync('./.env', ENV_VARS.join(os.EOL));
}

function enableMaintenanceMode(isDelayed) {
const now = new Date();
const nowPlus15Minutes = new Date(now.getTime() + 15 * 60000);
const ticks = isDelayed ? Math.floor(nowPlus15Minutes.getTime() / 1000) : Math.floor(now.getTime() / 1000);
setEnvValue(ticks.toString());
}

function disableMaintenanceMode() {
setEnvValue('');
}

function main() {
const command = process.env.ACTION;

if (!command) {
console.error('No command provided. Please use enable or disable');
process.exit(1);
}

switch (command) {
case 'enable':
enableMaintenanceMode();
break;
case 'delayed':
enableMaintenanceMode(true);
break;
case 'disable':
disableMaintenanceMode();
break;
default:
console.error('Invalid command. Please use enable or disable');
process.exit(1);
}
}

main();

0 comments on commit 788bc68

Please sign in to comment.