Skip to content

Instantly share code, notes, and snippets.

@nimaa77
Last active June 23, 2024 19:11
Show Gist options
  • Save nimaa77/8a2c681d67bb7dae750b5af9a74c9e15 to your computer and use it in GitHub Desktop.
Save nimaa77/8a2c681d67bb7dae750b5af9a74c9e15 to your computer and use it in GitHub Desktop.
/**
I don't really like to build my app on my deployment server
it usually takes a long time to build and deploy
so I wrote a small script that hanldes the deployment
because our development machine is fast and powerful
and the docker cache is always hot, the app will be ready for deployments in a few seconds
in coolify dashboard you need to create your app from a docker image
and use your docker image name in the input (e.g for github container registery: ghcr.io/username/my-awesome-app:latest)
you many need to use `docker login` to allow docker to accees your pricate docker images on the registery (both on dev machine and deployment servers)
https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-with-a-personal-access-token-classic
you need to generate a token in order to trigger the webhook, you can get one form:
https://coolify.io/docs/api-reference/authorization
save the token, docker image name and server url in a .env file and you are good to go!
this module can be used to:
1) build a docker image locally
2) push it to a regsitery
3) trigger coolify servers to re-deploy the docker image
*/
import "dotenv/config.js";
import { spawn } from "child_process";
const server = process.env.DEPLOYMENT_SERVER;
const uuid = process.env.DEPLOYMENT_UUID;
const token = process.env.DEPLOYMENT_TOKEN;
const image_name = process.env.DEPLOYMENT_IMAGE_NAME;
async function runCommand(command, args) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: "inherit" });
child.on("error", (error) => {
reject(error);
});
child.on("message", (message) => {
console.log(`child process message: ${message}`);
});
child.on("close", function (code, signal) {
if (code) {
reject(`child process exited with code ${code}`);
} else if (signal) {
reject(`child process was killed with signal ${signal}`);
} else {
resolve();
}
});
});
}
async function dockerBuild() {
const buildImageCMD = ["image", "build", "-t", `${image_name}:latest`, "."];
await runCommand("docker", buildImageCMD);
}
async function dockerPush() {
const pushImageCMD = ["image", "push", `${image_name}:latest`];
await runCommand("docker", pushImageCMD);
}
async function triggerWebhook() {
const options = {
method: "GET",
headers: { Authorization: `Bearer ${token}` },
};
const url = new URL(server);
url.pathname = "/api/v1/deploy";
url.searchParams.append("uuid", uuid);
url.searchParams.append("force", false);
return fetch(url, options)
.then((response) => response.json())
.then((response) => console.log(response))
.catch((err) => console.error(err));
}
async function main() {
try {
await dockerBuild();
await dockerPush();
await triggerWebhook();
} catch (error) {
console.error(error);
}
}
main();
@nimaa77
Copy link
Author

nimaa77 commented Jun 21, 2024

I personally added this as a script to my package.json

"scripts": {
  "deploy": "node deploy.js"
}

so whenever I want to deploy a new version of the app I run npm run deploy
and in a few seconds a new version of app is built, deployed and running on the server

@nimaa77
Copy link
Author

nimaa77 commented Jun 21, 2024

keep in mind if you are using a private registry, you have to use ssh to connect to your Coolify server, use docker login command to log in to the registry and then the docker engine can access your private images and pull them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment