refactor: use service image sha instead of image itself for checking updates

This commit is contained in:
UndefinedPony
2024-12-21 09:04:25 +01:00
parent f40e802331
commit 18eae9f7d7

View File

@@ -1,8 +1,10 @@
import { readdirSync } from "node:fs"; import { readdirSync } from "node:fs";
import { join } from "node:path"; import { join } from "node:path";
import { docker } from "@dokploy/server/constants"; import { docker } from "@dokploy/server/constants";
import { execAsyncRemote } from "@dokploy/server/utils/process/execAsync"; import {
import { spawnAsync } from "../utils/process/spawnAsync"; execAsync,
execAsyncRemote,
} from "@dokploy/server/utils/process/execAsync";
// import packageInfo from "../../../package.json"; // import packageInfo from "../../../package.json";
/** Returns current Dokploy docker image tag or `latest` by default. */ /** Returns current Dokploy docker image tag or `latest` by default. */
@@ -23,15 +25,12 @@ export const pullLatestRelease = async () => {
}); });
}; };
/** Returns current docker image digest */ /** Returns Dokploy docker service image digest */
export const getCurrentImageDigest = async () => { export const getServiceImageDigest = async () => {
const commandResult = await spawnAsync("docker", [ const { stdout } = await execAsync(
"inspect", "docker service inspect dokploy --format '{{.Spec.TaskTemplate.ContainerSpec.Image}}'",
"--format={{index .RepoDigests 0}}", );
getDokployImage(), const currentDigest = stdout.trim().split("@")[1];
]);
const currentDigest = commandResult.toString().trim().split("@")[1];
return currentDigest; return currentDigest;
}; };
@@ -41,18 +40,7 @@ export const getUpdateData = async (): Promise<{
latestVersion: string | null; latestVersion: string | null;
updateAvailable: boolean; updateAvailable: boolean;
}> => { }> => {
let currentDigest: string | undefined; const currentDigest = await getServiceImageDigest();
try {
currentDigest = await getCurrentImageDigest();
} catch {
// In case image doesn't exist yet, pull latest release
await pullLatestRelease();
currentDigest = await getCurrentImageDigest();
}
if (!currentDigest) {
throw new Error("Could not get current image digest");
}
const url = "https://hub.docker.com/v2/repositories/dokploy/dokploy/tags"; const url = "https://hub.docker.com/v2/repositories/dokploy/dokploy/tags";
const response = await fetch(url, { const response = await fetch(url, {