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