From 2f76058b31ffb382a6c800189f6844fbc34c833f Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Thu, 6 Feb 2025 21:11:37 +0530 Subject: [PATCH] feat: enable beta deploy (#23) --- .env.example | 1 + package.json | 1 + pnpm-lock.yaml | 69 ++++++++++++++++++++++++++ src/app/actions/github.ts | 22 ++++++-- src/components/ui/switch.tsx | 29 +++++++++++ src/sanity/components/CustomNavbar.tsx | 24 +++++++-- 6 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 src/components/ui/switch.tsx diff --git a/.env.example b/.env.example index 55bc82a..e51a98b 100644 --- a/.env.example +++ b/.env.example @@ -12,4 +12,5 @@ IMAGES_FTP_PASS="" GITHUB_PAT="" DEPLOY_REPO_URL="user/repo" DEPLOY_WORKFLOW="workflow.yml" +DEPLOY_BETA_WORKFLOW="workflow2.yml" DEPLOY_REF="main" diff --git a/package.json b/package.json index ebafdb7..b34ead0 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@radix-ui/react-label": "^2.1.1", "@radix-ui/react-select": "^2.1.4", "@radix-ui/react-slot": "^1.1.1", + "@radix-ui/react-switch": "^1.1.3", "@sanity/icons": "^3.4.0", "@sanity/image-url": "1", "@sanity/ui": "^2.8.25", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a8cf623..4fdf433 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,9 @@ importers: '@radix-ui/react-slot': specifier: ^1.1.1 version: 1.1.1(@types/react@18.3.18)(react@18.3.1) + '@radix-ui/react-switch': + specifier: ^1.1.3 + version: 1.1.3(@types/react-dom@18.3.5(@types/react@18.3.18))(@types/react@18.3.18)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@sanity/icons': specifier: ^3.4.0 version: 3.5.7(react@18.3.1) @@ -1813,6 +1816,19 @@ packages: '@types/react-dom': optional: true + '@radix-ui/react-primitive@2.0.2': + resolution: {integrity: sha512-Ec/0d38EIuvDF+GZjcMU/Ze6MxntVJYO/fRlCPhCaVUyPY9WTalHJw54tp9sXeJo3tlShWpy41vQRgLRGOuz+w==} + peerDependencies: + '@types/react': '*' + '@types/react-dom': '*' + react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc + react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc + peerDependenciesMeta: + '@types/react': + optional: true + '@types/react-dom': + optional: true + '@radix-ui/react-select@2.1.4': resolution: {integrity: sha512-pOkb2u8KgO47j/h7AylCj7dJsm69BXcjkrvTqMptFqsE2i0p8lHkfgneXKjAgPzBMivnoMyt8o4KiV4wYzDdyQ==} peerDependencies: @@ -1835,6 +1851,28 @@ packages: '@types/react': optional: true + '@radix-ui/react-slot@1.1.2': + resolution: {integrity: sha512-YAKxaiGsSQJ38VzKH86/BPRC4rh+b1Jpa+JneA5LRE7skmLPNAyeG8kPJj/oo4STLvlrs8vkf/iYyc3A5stYCQ==} + peerDependencies: + '@types/react': '*' + react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc + peerDependenciesMeta: + '@types/react': + optional: true + + '@radix-ui/react-switch@1.1.3': + resolution: {integrity: sha512-1nc+vjEOQkJVsJtWPSiISGT6OKm4SiOdjMo+/icLxo2G4vxz1GntC5MzfL4v8ey9OEfw787QCD1y3mUv0NiFEQ==} + peerDependencies: + '@types/react': '*' + '@types/react-dom': '*' + react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc + react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc + peerDependenciesMeta: + '@types/react': + optional: true + '@types/react-dom': + optional: true + '@radix-ui/react-use-callback-ref@1.1.0': resolution: {integrity: sha512-CasTfvsy+frcFkbXtSJ2Zu9JHpN8TYKxkgJGWbjiZhFivxaeW7rMeZt7QELGVLaYVfFMsKHjb7Ak0nMEe+2Vfw==} peerDependencies: @@ -8227,6 +8265,15 @@ snapshots: '@types/react': 18.3.18 '@types/react-dom': 18.3.5(@types/react@18.3.18) + '@radix-ui/react-primitive@2.0.2(@types/react-dom@18.3.5(@types/react@18.3.18))(@types/react@18.3.18)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + dependencies: + '@radix-ui/react-slot': 1.1.2(@types/react@18.3.18)(react@18.3.1) + react: 18.3.1 + react-dom: 18.3.1(react@18.3.1) + optionalDependencies: + '@types/react': 18.3.18 + '@types/react-dom': 18.3.5(@types/react@18.3.18) + '@radix-ui/react-select@2.1.4(@types/react-dom@18.3.5(@types/react@18.3.18))(@types/react@18.3.18)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: '@radix-ui/number': 1.1.0 @@ -8263,6 +8310,28 @@ snapshots: optionalDependencies: '@types/react': 18.3.18 + '@radix-ui/react-slot@1.1.2(@types/react@18.3.18)(react@18.3.1)': + dependencies: + '@radix-ui/react-compose-refs': 1.1.1(@types/react@18.3.18)(react@18.3.1) + react: 18.3.1 + optionalDependencies: + '@types/react': 18.3.18 + + '@radix-ui/react-switch@1.1.3(@types/react-dom@18.3.5(@types/react@18.3.18))(@types/react@18.3.18)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + dependencies: + '@radix-ui/primitive': 1.1.1 + '@radix-ui/react-compose-refs': 1.1.1(@types/react@18.3.18)(react@18.3.1) + '@radix-ui/react-context': 1.1.1(@types/react@18.3.18)(react@18.3.1) + '@radix-ui/react-primitive': 2.0.2(@types/react-dom@18.3.5(@types/react@18.3.18))(@types/react@18.3.18)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@radix-ui/react-use-controllable-state': 1.1.0(@types/react@18.3.18)(react@18.3.1) + '@radix-ui/react-use-previous': 1.1.0(@types/react@18.3.18)(react@18.3.1) + '@radix-ui/react-use-size': 1.1.0(@types/react@18.3.18)(react@18.3.1) + react: 18.3.1 + react-dom: 18.3.1(react@18.3.1) + optionalDependencies: + '@types/react': 18.3.18 + '@types/react-dom': 18.3.5(@types/react@18.3.18) + '@radix-ui/react-use-callback-ref@1.1.0(@types/react@18.3.18)(react@18.3.1)': dependencies: react: 18.3.1 diff --git a/src/app/actions/github.ts b/src/app/actions/github.ts index a94bfe7..5e3fafa 100644 --- a/src/app/actions/github.ts +++ b/src/app/actions/github.ts @@ -6,9 +6,14 @@ type RunType = { id: string; }; -export async function dispatchWorkflow() { +export async function dispatchWorkflow({ + isBetaDeploy, +}: { + isBetaDeploy: boolean; +}) { const repoURL = process.env.DEPLOY_REPO_URL; - const workflow = process.env.DEPLOY_WORKFLOW; + const productionWorkflow = process.env.DEPLOY_WORKFLOW; + const betaWorkflow = process.env.DEPLOY_BETA_WORKFLOW; const ref = process.env.DEPLOY_REF; try { @@ -16,7 +21,14 @@ export async function dispatchWorkflow() { auth: process.env.GITHUB_PAT, }); - // Get all workflow runs to check the uncompleted ones + const workflow = isBetaDeploy ? betaWorkflow : productionWorkflow; + + if (!workflow) { + throw new Error( + `No ${isBetaDeploy ? 'beta' : 'production'} workflow configured` + ); + } + const { data: runs } = await octokit.request( `GET /repos/${repoURL}/actions/workflows/${workflow}/runs`, { @@ -49,7 +61,9 @@ export async function dispatchWorkflow() { { ref } ); - console.log('Dispatched new workflow run'); + console.log( + `Dispatched new ${isBetaDeploy ? 'beta' : 'production'} workflow run` + ); } catch (error) { throw error; } diff --git a/src/components/ui/switch.tsx b/src/components/ui/switch.tsx new file mode 100644 index 0000000..9c51976 --- /dev/null +++ b/src/components/ui/switch.tsx @@ -0,0 +1,29 @@ +'use client'; + +import * as React from 'react'; +import * as SwitchPrimitives from '@radix-ui/react-switch'; + +import { cn } from '@/lib/utils'; + +const Switch = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + + + +)); +Switch.displayName = SwitchPrimitives.Root.displayName; + +export { Switch }; diff --git a/src/sanity/components/CustomNavbar.tsx b/src/sanity/components/CustomNavbar.tsx index eb4da1d..455d4be 100644 --- a/src/sanity/components/CustomNavbar.tsx +++ b/src/sanity/components/CustomNavbar.tsx @@ -13,21 +13,23 @@ import { DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; +import { Switch } from '@/components/ui/switch'; import FTPComponent from '@/components/FTPComponent'; export function CustomNavbar(props: NavbarProps) { const { renderDefault } = props; const [isDeploying, setIsDeploying] = useState(false); const [timeLeft, setTimeLeft] = useState(0); + const [isBetaDeploy, setIsBetaDeploy] = useState(false); const toast = useToast(); const deploySite = async () => { try { - await dispatchWorkflow(); + await dispatchWorkflow({ isBetaDeploy }); toast.push({ status: 'success', title: 'Deployment Triggered', - description: 'Your site is being deployed!', + description: `Deploying to ${isBetaDeploy ? 'Beta' : 'Production'} environment`, }); } catch (error: unknown) { setIsDeploying(false); @@ -79,6 +81,21 @@ export function CustomNavbar(props: NavbarProps) { return (
+
+ + Production + +
+ +
+ + Beta + +
+