-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d463cb
commit 4b5dcf1
Showing
9 changed files
with
484 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
NEXT_PUBLIC_SANITY_DATASET="" | ||
NEXT_PUBLIC_SANITY_PROJECT_ID="" | ||
|
||
FTP_HOST="" | ||
FTP_USER="" | ||
FTP_PASS="" | ||
|
||
GITHUB_PAT="" | ||
DEPLOY_REPO_URL="user/repo" | ||
DEPLOY_WORKFLOW="workflow.yml" | ||
DEPLOY_REF="main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use server"; | ||
import { Octokit } from "@octokit/core"; | ||
|
||
type RunType = { | ||
status: string; | ||
id: string; | ||
}; | ||
|
||
export async function dispatchWorkflow() { | ||
const repoURL = process.env.DEPLOY_REPO_URL; | ||
const workflow = process.env.DEPLOY_WORKFLOW; | ||
const ref = process.env.DEPLOY_REF; | ||
|
||
try { | ||
const octokit = new Octokit({ | ||
auth: process.env.GITHUB_PAT, | ||
}); | ||
|
||
// Get all workflow runs to check the uncompleted ones | ||
const { data: runs } = await octokit.request( | ||
`GET /repos/${repoURL}/actions/workflows/${workflow}/runs`, | ||
{ | ||
per_page: 100, | ||
} | ||
); | ||
|
||
const unCompletedRuns = runs.workflow_runs.filter( | ||
(run: RunType) => run.status !== "completed" | ||
); | ||
|
||
if (unCompletedRuns.length > 0) { | ||
// Cancel the uncompleted ones | ||
for (const run of unCompletedRuns) { | ||
await octokit.request(`POST /repos/${repoURL}/actions/runs/${run.id}/cancel`, { | ||
headers: { | ||
Accept: "application/vnd.github.v3+json", | ||
}, | ||
}); | ||
console.log(`Cancelled workflow run: ${run.id}`); | ||
} | ||
} | ||
|
||
// Dispatch a new workflow run | ||
await octokit.request( | ||
`POST /repos/${repoURL}/actions/workflows/${workflow}/dispatches`, | ||
{ ref } | ||
); | ||
|
||
console.log("Dispatched new workflow run"); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Box, useToast, Spinner } from "@sanity/ui"; | ||
import { NavbarProps } from "sanity"; | ||
import { useState } from "react"; | ||
import Image from "next/image"; | ||
import { redirect, RedirectType } from "next/navigation"; | ||
import { dispatchWorkflow } from "@/app/actions/github"; | ||
import clsx from "clsx"; | ||
|
||
export function CustomNavbar(props: NavbarProps) { | ||
const { renderDefault } = props; | ||
const [isDeploying, setIsDeploying] = useState(false); | ||
const [timeLeft, setTimeLeft] = useState(0); | ||
const toast = useToast(); | ||
|
||
const deploySite = async () => { | ||
try { | ||
await dispatchWorkflow(); | ||
toast.push({ | ||
status: "success", | ||
title: "Deployment Triggered", | ||
description: "Your site is being deployed!", | ||
}); | ||
} catch (error: unknown) { | ||
toast.push({ | ||
status: "error", | ||
title: "Deployment Failed", | ||
description: "Server error", | ||
}); | ||
} | ||
}; | ||
|
||
const handleDeploy = async () => { | ||
setIsDeploying(true); | ||
await deploySite(); | ||
|
||
const expectedTime = 60; | ||
|
||
setTimeLeft(expectedTime); | ||
|
||
setInterval(() => { | ||
setTimeLeft(prev => prev - 1); | ||
}, 1000); | ||
|
||
setTimeout(() => { | ||
setIsDeploying(false); | ||
}, expectedTime * 1000); | ||
}; | ||
|
||
const checkStatus = () => { | ||
redirect( | ||
"https://github.com/velocity-iiitdwd/iiitdwd.ac.in/actions/", | ||
RedirectType.push | ||
); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className="flex justify-center items-center gap-2 p-2 bg-slate-950"> | ||
<button | ||
onClick={handleDeploy} | ||
disabled={isDeploying} | ||
className={clsx( | ||
"flex items-center justify-center px-4 py-1 text-white bg-slate-800 rounded-md transition duration-200", | ||
isDeploying | ||
? "bg-slate-600 cursor-not-allowed" | ||
: "hover:bg-slate-700" | ||
)} | ||
> | ||
{isDeploying ? ( | ||
<> | ||
<Spinner muted /> | ||
<span className="ml-2">Deploying... {timeLeft} sec</span> | ||
</> | ||
) : ( | ||
"Deploy" | ||
)} | ||
</button> | ||
<button | ||
onClick={checkStatus} | ||
className="flex items-center justify-center bg-slate-400 rounded-full transition duration-200 size-5" | ||
title="Check GitHub Action Status" | ||
> | ||
<Image src="/info.svg" height={48} width={48} alt="info" /> | ||
</button> | ||
</div> | ||
<Box flex={1}>{renderDefault(props)}</Box> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters