-
Notifications
You must be signed in to change notification settings - Fork 29
Improve bumpUpstream: semver sort and Docker registry-based version format resolution #475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,9 @@ | |
| "lint": "eslint . --ext .ts --fix", | ||
| "build": "tsc", | ||
| "prepublish": "npm run build", | ||
| "pre-commit": "npm run lint && npm run test" | ||
| "pre-commit": "npm run lint && npm run test", | ||
| "cli": "node dist/dappnodesdk.js", | ||
| "start": "yarn cli" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are these changes for? are you sure these are needed? |
||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
|
|
@@ -94,4 +96,4 @@ | |
| "engines": { | ||
| "node": ">=20.0.0" | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { Compose } from "@dappnode/types"; | ||
|
|
||
| /** | ||
| * Given a GitHub release tag (e.g. "v1.17.0", "n8n@2.10.3"), resolves the | ||
| * correct version format by checking the upstream Docker image registry. | ||
| * | ||
| * 1. Finds which compose service uses the given build arg | ||
| * 2. Parses the Dockerfile to extract the Docker image that uses that arg | ||
| * 3. Checks the Docker registry for tag existence (with/without prefix) | ||
| * 4. Returns the version in the format that matches the Docker registry | ||
| */ | ||
| export async function resolveVersionFormat({ | ||
| tag, | ||
| arg, | ||
| compose, | ||
| dir | ||
| }: { | ||
| tag: string; | ||
| arg: string; | ||
| compose: Compose; | ||
| dir: string; | ||
| }): Promise<string> { | ||
| const stripped = stripTagPrefix(tag); | ||
| if (!stripped || stripped === tag) return tag; // No prefix to strip | ||
|
|
||
| try { | ||
| const dockerImage = getDockerImageForArg(compose, arg, dir); | ||
| if (!dockerImage) return tag; | ||
|
|
||
| const tagExists = await checkDockerTagExists(dockerImage, stripped); | ||
| if (tagExists) return stripped; | ||
|
|
||
| return tag; | ||
| } catch (e) { | ||
| console.warn(`Could not resolve version format for ${tag}, using as-is:`, e.message); | ||
| return tag; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Finds the Docker image that uses a given build arg by parsing the Dockerfile. | ||
| */ | ||
| function getDockerImageForArg( | ||
| compose: Compose, | ||
| arg: string, | ||
| dir: string | ||
| ): string | null { | ||
| for (const [, service] of Object.entries(compose.services)) { | ||
| if ( | ||
| typeof service.build !== "string" && | ||
| service.build?.args && | ||
| arg in service.build.args | ||
| ) { | ||
| const buildContext = service.build.context || "."; | ||
| const dockerfileName = service.build.dockerfile || "Dockerfile"; | ||
| const dockerfilePath = path.resolve(dir, buildContext, dockerfileName); | ||
|
|
||
| if (!fs.existsSync(dockerfilePath)) continue; | ||
|
|
||
| const content = fs.readFileSync(dockerfilePath, "utf-8"); | ||
| return extractImageForArg(content, arg); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a Dockerfile to find the FROM line that references the given ARG, | ||
| * and extracts the Docker image name (without the tag). | ||
| * | ||
| * Handles patterns like: | ||
| * FROM ethereum/client-go:${UPSTREAM_VERSION} | ||
| * FROM ethereum/client-go:v${UPSTREAM_VERSION} | ||
| * FROM ollama/ollama:${OLLAMA_VERSION#v} | ||
| * FROM statusim/nimbus-eth2:multiarch-${UPSTREAM_VERSION} | ||
| */ | ||
| function extractImageForArg( | ||
| dockerfileContent: string, | ||
| arg: string | ||
| ): string | null { | ||
| const lines = dockerfileContent.split("\n"); | ||
|
|
||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
| if (!trimmed.startsWith("FROM") || !trimmed.includes(arg)) continue; | ||
|
|
||
| // Match: FROM image:tag_pattern (with optional "AS stage") | ||
| const match = trimmed.match(/^FROM\s+([^:\s]+)/i); | ||
| if (match) return match[1]; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a tag exists on a Docker registry using the Docker Hub v2 API. | ||
| * Supports Docker Hub, ghcr.io, and gcr.io. | ||
| */ | ||
| async function checkDockerTagExists( | ||
| image: string, | ||
| tag: string | ||
| ): Promise<boolean> { | ||
| const url = getRegistryTagUrl(image, tag); | ||
| if (!url) return false; | ||
|
|
||
| try { | ||
| const response = await fetch(url); | ||
| return response.ok; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function getRegistryTagUrl(image: string, tag: string): string | null { | ||
| // ghcr.io/org/image -> GitHub Container Registry | ||
| if (image.startsWith("ghcr.io/")) { | ||
| const imagePath = image.replace("ghcr.io/", ""); | ||
| return `https://ghcr.io/v2/${imagePath}/manifests/${tag}`; | ||
| } | ||
|
|
||
| // gcr.io/project/image -> Google Container Registry | ||
| if (image.startsWith("gcr.io/")) { | ||
| const imagePath = image.replace("gcr.io/", ""); | ||
| return `https://gcr.io/v2/${imagePath}/manifests/${tag}`; | ||
| } | ||
|
|
||
| // Docker Hub: library/image or org/image | ||
| const dockerImage = image.includes("/") ? image : `library/${image}`; | ||
| return `https://registry.hub.docker.com/v2/repositories/${dockerImage}/tags/${tag}`; | ||
| } | ||
|
|
||
| function stripTagPrefix(tag: string): string | null { | ||
| const match = tag.match(/(\d+\.\d+\.\d+.*)$/); | ||
| return match ? match[1] : null; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,7 @@ | |
| .listReleases({ | ||
| owner: this.owner, | ||
| repo: this.repo, | ||
| per_page: 100, | ||
| headers: { | ||
| "X-GitHub-Api-Version": "2022-11-28" | ||
| } | ||
|
|
@@ -221,7 +222,7 @@ | |
| return release.data.id; | ||
| } | ||
|
|
||
| async uploadReleaseAssets({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check warning |
||
| releaseId, | ||
| assetsDir, | ||
| matchPattern, | ||
|
|
@@ -246,7 +247,7 @@ | |
| owner: this.owner, | ||
| repo: this.repo, | ||
| release_id: releaseId, | ||
| data: fs.createReadStream(filepath) as any, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check warning |
||
| headers: { | ||
| "content-type": contentType, | ||
| "content-length": fs.statSync(filepath).size | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think this should go into this PR