Skip to content

Instantly share code, notes, and snippets.

@zicklag
Created November 3, 2024 19:05
Show Gist options
  • Save zicklag/a588f6a474900624e10140ec15c25715 to your computer and use it in GitHub Desktop.
Save zicklag/a588f6a474900624e10140ec15c25715 to your computer and use it in GitHub Desktop.
A mini script to re-deploy a docker container with deno and a webhook.
import { Server } from "https://deno.land/[email protected]/http/server.ts";
const containerName = "bones-matchmaker-main";
const containerImage = "ghcr.io/fishfolk/bones-matchmaker:main";
async function updateContainer() {
console.log("Updating container");
// Stop the container if it is already running
await Deno.run({ cmd: ["docker", "stop", containerName] }).status();
// Remove the old container if it exists
await Deno.run({ cmd: ["docker", "rm", containerName] }).status();
// Pull the latest version of the container
let status = await Deno.run({
cmd: ["docker", "pull", containerImage],
}).status();
if (!status.success) {
console.error("Error pulling docker container");
return;
}
// Run the new container
status = await Deno.run({
cmd: [
"docker",
"run",
"-d",
"--name",
containerName,
"-p",
"65534:8943/udp",
containerImage,
],
}).status();
if (!status.success) {
console.error("Error running docker container");
}
}
const handler = async (request: Request) => {
const secret = await request.text();
const response = new Response(null, { status: 200 });
if (secret !== Deno.env.get("SECRET")) {
console.log("Invalid request secret");
} else {
console.log("Valid request");
updateContainer();
}
return response;
};
const port = parseInt(Deno.env.get("PORT") || "80");
const server = new Server({ handler, port });
console.log(`Server listening on port ${port}`);
server.listenAndServe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment