Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide/gha-update-image/lib/code-pin-version.ts
2499 views
1
// Copyright (c) 2024 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
import { $ } from "bun";
6
import {
7
getIDEVersionOfImage,
8
getLatestInstallerVersions,
9
pathToConfigmap,
10
readIDEConfigmapJson,
11
readWorkspaceYaml,
12
} from "./common";
13
14
$.nothrow();
15
16
const ideConfigmapInfo = await readIDEConfigmapJson();
17
const ideConfigmapJson = ideConfigmapInfo.parsedObj;
18
const ideConfigmapJsonObj = ideConfigmapInfo.rawObj;
19
const workspaceYaml = await readWorkspaceYaml().then((d) => d.parsedObj);
20
21
export async function updateCodeIDEConfigMapJson() {
22
const latestInstaller = await getLatestInstallerVersions();
23
const latestBuildImage = {
24
code: latestInstaller.components.workspace.codeImage.version,
25
webExtension: latestInstaller.components.workspace.codeWebExtensionImage.version,
26
codeHelper: latestInstaller.components.workspace.codeHelperImage.version,
27
};
28
29
console.log("comparing with latest installer versions", latestInstaller.version, latestBuildImage);
30
31
const firstPinnedInfo = ideConfigmapJson.ideOptions.options.code.versions[0];
32
const hasChangedMap = {
33
image: !ideConfigmapJson.ideOptions.options.code.image.includes(latestBuildImage.code),
34
webExtension: !ideConfigmapJson.ideOptions.options.code.imageLayers[0].includes(latestBuildImage.webExtension),
35
codeHelper: !ideConfigmapJson.ideOptions.options.code.imageLayers[1].includes(latestBuildImage.codeHelper),
36
};
37
38
console.log("image change status", hasChangedMap);
39
40
const replaceImageHash = (image: string, hash: string) => image.replace(/commit-.*/, hash);
41
const updateImages = <T extends { image: string; imageLayers: string[] }>(originData: T) => {
42
const data = structuredClone(originData);
43
data.image = replaceImageHash(data.image, latestBuildImage.code);
44
data.imageLayers[0] = replaceImageHash(data.imageLayers[0], latestBuildImage.webExtension);
45
data.imageLayers[1] = replaceImageHash(data.imageLayers[1], latestBuildImage.codeHelper);
46
return data;
47
};
48
49
const newJson = structuredClone(ideConfigmapJsonObj);
50
newJson.ideOptions.options.code = updateImages(newJson.ideOptions.options.code);
51
52
// try append new pin versions
53
const previousCodeVersion = await getIDEVersionOfImage("eu.gcr.io/gitpod-core-dev/build/" + ideConfigmapJson.ideOptions.options.code.image.replace("{{.Repository}}/", ""));
54
const installationCodeVersion = await getIDEVersionOfImage("eu.gcr.io/gitpod-core-dev/build/" + newJson.ideOptions.options.code.image.replace("{{.Repository}}/", ""));
55
if (installationCodeVersion.trim() === "" || previousCodeVersion.trim() === "") {
56
throw new Error("installation or previous code version can't be empty");
57
}
58
let appendNewVersion = false;
59
console.log("code versions", { installationCodeVersion, previousCodeVersion });
60
if (installationCodeVersion === previousCodeVersion) {
61
console.log("code version is the same, no need to update (ide-service will do it)", installationCodeVersion);
62
} else {
63
const hasPinned = firstPinnedInfo.version === installationCodeVersion;
64
if (!hasPinned) {
65
console.log("updating related pinned version", installationCodeVersion);
66
newJson.ideOptions.options.code.versions.unshift({
67
version: previousCodeVersion,
68
image: ideConfigmapJsonObj.ideOptions.options.code.image,
69
imageLayers: ideConfigmapJsonObj.ideOptions.options.code.imageLayers,
70
});
71
appendNewVersion = true;
72
}
73
}
74
75
console.log("updating ide-configmap.json");
76
await Bun.write(pathToConfigmap, JSON.stringify(newJson, null, 2) + "\n");
77
return appendNewVersion ? workspaceYaml.defaultArgs.codeVersion : undefined;
78
}
79
80