|
| 1 | +import fs from "node:fs"; |
| 2 | + |
| 3 | +const owner = "openclaw"; |
| 4 | +const repo = "openclaw"; |
| 5 | +const token = process.env.GITHUB_TOKEN; |
| 6 | +if (!token) { |
| 7 | + console.error("Missing GITHUB_TOKEN"); |
| 8 | + process.exit(2); |
| 9 | +} |
| 10 | + |
| 11 | +async function gh(path) { |
| 12 | + const url = `https://api.github.com${path}`; |
| 13 | + const res = await fetch(url, { |
| 14 | + headers: { |
| 15 | + authorization: `Bearer ${token}`, |
| 16 | + accept: "application/vnd.github+json", |
| 17 | + "user-agent": "clawdbot-railway-template-bot", |
| 18 | + }, |
| 19 | + }); |
| 20 | + if (!res.ok) { |
| 21 | + throw new Error(`GitHub API ${res.status}: ${await res.text()}`); |
| 22 | + } |
| 23 | + return res.json(); |
| 24 | +} |
| 25 | + |
| 26 | +function readCurrentTag(dockerfile) { |
| 27 | + const m = dockerfile.match(/\nARG OPENCLAW_GIT_REF=([^\n]+)\n/); |
| 28 | + return m ? m[1].trim() : null; |
| 29 | +} |
| 30 | + |
| 31 | +function replaceTag(dockerfile, next) { |
| 32 | + const re = /\nARG OPENCLAW_GIT_REF=([^\n]+)\n/; |
| 33 | + if (!re.test(dockerfile)) throw new Error("Could not find OPENCLAW_GIT_REF line"); |
| 34 | + return dockerfile.replace(re, `\nARG OPENCLAW_GIT_REF=${next}\n`); |
| 35 | +} |
| 36 | + |
| 37 | +const latest = await gh(`/repos/${owner}/${repo}/releases/latest`); |
| 38 | +const latestTag = latest.tag_name; |
| 39 | +if (!latestTag) throw new Error("No tag_name in latest release response"); |
| 40 | + |
| 41 | +const dockerPath = "Dockerfile"; |
| 42 | +const docker = fs.readFileSync(dockerPath, "utf8"); |
| 43 | +const currentTag = readCurrentTag(docker); |
| 44 | +if (!currentTag) throw new Error("Could not parse current OPENCLAW_GIT_REF"); |
| 45 | + |
| 46 | +console.log(`current=${currentTag} latest=${latestTag}`); |
| 47 | + |
| 48 | +if (currentTag === latestTag) { |
| 49 | + console.log("No update needed."); |
| 50 | + process.exit(0); |
| 51 | +} |
| 52 | + |
| 53 | +fs.writeFileSync(dockerPath, replaceTag(docker, latestTag)); |
| 54 | +console.log(`Updated ${dockerPath} to ${latestTag}`); |
0 commit comments