ci: mirror Gitea releases to GitHub via Actions (release event)
Mirror release to GitHub / mirror (release) Successful in 3s
Mirror release to GitHub / mirror (release) Successful in 3s
This commit is contained in:
1 parent
d9d4c4219e
commit
ec7802463a
1 file changed
+191
@@ -0,0 +1,191 @@
|
|||||||
|
name: Mirror release to GitHub
|
||||||
|
|
||||||
|
# Mirrors Gitea releases (title, body, prerelease flag, APK assets) to
|
||||||
|
# github.com/${{ github.repository }}. The Gitea push mirror only syncs git
|
||||||
|
# refs (branches/tags), never releases — this workflow closes that gap.
|
||||||
|
# Requires repo secret: GH_PAT (GitHub PAT with repo scope / contents:write).
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
tag:
|
||||||
|
description: "Gitea tag to mirror (blank = latest Gitea release)"
|
||||||
|
required: false
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
mirror:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 5
|
||||||
|
steps:
|
||||||
|
- name: Mirror release to GitHub
|
||||||
|
env:
|
||||||
|
GH_PAT: ${{ secrets.GH_PAT }}
|
||||||
|
EVENT_TAG: ${{ github.event.release.tag_name }}
|
||||||
|
INPUT_TAG: ${{ github.event.inputs.tag }}
|
||||||
|
GITEA_SERVER: ${{ github.server_url }}
|
||||||
|
GH_REPO: ${{ github.repository }}
|
||||||
|
run: |
|
||||||
|
cat > /tmp/mirror-release.cjs <<'SCRIPT_EOF'
|
||||||
|
const { spawnSync } = require("node:child_process");
|
||||||
|
const fs = require("node:fs");
|
||||||
|
|
||||||
|
const GH_PAT = process.env.GH_PAT || "";
|
||||||
|
const GITEA = (process.env.GITEA_SERVER || "https://git.mohammedkhalid.in").replace(/\/+$/, "");
|
||||||
|
const REPO = process.env.GH_REPO || "thekhalidsha/stitch";
|
||||||
|
const TAG = ((process.env.EVENT_TAG || "") + "").trim() || ((process.env.INPUT_TAG || "") + "").trim();
|
||||||
|
|
||||||
|
function curlJson(url, opts = {}) {
|
||||||
|
const args = ["-sS", "-L", "-X", opts.method || "GET", "-w", "\n%{http_code}", url];
|
||||||
|
if (opts.token) args.push("-H", "Authorization: token " + opts.token);
|
||||||
|
args.push("-H", "Accept: application/json");
|
||||||
|
if (opts.body !== undefined) {
|
||||||
|
args.push("-H", "Content-Type: application/json");
|
||||||
|
args.push("--data", JSON.stringify(opts.body));
|
||||||
|
}
|
||||||
|
const r = spawnSync("curl", args, { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
||||||
|
if (r.error || r.status !== 0) throw new Error("curl failed for " + url + ": " + (r.stderr || r.error));
|
||||||
|
const out = r.stdout;
|
||||||
|
const nl = out.lastIndexOf("\n");
|
||||||
|
const code = parseInt(out.slice(nl + 1), 10);
|
||||||
|
const txt = out.slice(0, nl);
|
||||||
|
let body = null;
|
||||||
|
try { body = txt ? JSON.parse(txt) : null; } catch { body = txt; }
|
||||||
|
return { status: code, body };
|
||||||
|
}
|
||||||
|
|
||||||
|
function curlDownload(url, out, token) {
|
||||||
|
const args = ["-sS", "-L", "--fail", "--retry", "3", "-o", out, url];
|
||||||
|
if (token) args.splice(1, 0, "-H", "Authorization: token " + token);
|
||||||
|
const r = spawnSync("curl", args, { encoding: "utf8" });
|
||||||
|
if (r.error || r.status !== 0) throw new Error("download failed: " + (r.stderr || r.error));
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
if (!GH_PAT) {
|
||||||
|
console.error("ERROR: GH_PAT secret is not set");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Resolve the tag to mirror (event -> input -> latest Gitea release)
|
||||||
|
let tag = TAG;
|
||||||
|
if (!tag) {
|
||||||
|
const latest = curlJson(`${GITEA}/api/v1/repos/${REPO}/releases?limit=1&page=1`);
|
||||||
|
if (latest.status !== 200 || !Array.isArray(latest.body) || latest.body.length === 0) {
|
||||||
|
console.error("ERROR: cannot list Gitea releases:", latest.status, latest.body);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
tag = latest.body[0].tag_name;
|
||||||
|
console.log(`No tag given - using latest Gitea release: ${tag}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Fetch the Gitea release (source of truth)
|
||||||
|
const gr = curlJson(`${GITEA}/api/v1/repos/${REPO}/releases/tags/${encodeURIComponent(tag)}`);
|
||||||
|
if (gr.status === 404) {
|
||||||
|
console.log(`Release ${tag} not found on Gitea (deleted or draft) - nothing to mirror.`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
if (gr.status !== 200) {
|
||||||
|
console.error("ERROR: Gitea API error", gr.status, gr.body);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const g = gr.body;
|
||||||
|
console.log(`Gitea release: ${g.tag_name} "${g.name}" prerelease=${g.prerelease} assets=${(g.assets || []).length}`);
|
||||||
|
|
||||||
|
// 3. Upsert the GitHub release (title / body / prerelease flag)
|
||||||
|
const hr = curlJson(`https://api.github.com/repos/${REPO}/releases/tags/${encodeURIComponent(tag)}`, { token: GH_PAT });
|
||||||
|
const payload = {
|
||||||
|
tag_name: g.tag_name,
|
||||||
|
name: g.name || g.tag_name,
|
||||||
|
body: g.body || "",
|
||||||
|
prerelease: !!g.prerelease,
|
||||||
|
draft: false,
|
||||||
|
target_commitish: g.target_commitish || undefined,
|
||||||
|
};
|
||||||
|
let gh;
|
||||||
|
if (hr.status === 404) {
|
||||||
|
const created = curlJson(`https://api.github.com/repos/${REPO}/releases`, { token: GH_PAT, method: "POST", body: payload });
|
||||||
|
if (created.status !== 201 && created.status !== 200) {
|
||||||
|
console.error("ERROR: creating GitHub release failed:", created.status, created.body);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
gh = created.body;
|
||||||
|
console.log(`Created GitHub release ${tag} (prerelease=${payload.prerelease})`);
|
||||||
|
} else if (hr.status === 200) {
|
||||||
|
gh = hr.body;
|
||||||
|
const differs =
|
||||||
|
(gh.name || "") !== payload.name ||
|
||||||
|
(gh.body || "") !== payload.body ||
|
||||||
|
!!gh.prerelease !== payload.prerelease;
|
||||||
|
if (differs) {
|
||||||
|
const upd = curlJson(`https://api.github.com/repos/${REPO}/releases/${gh.id}`, {
|
||||||
|
token: GH_PAT, method: "PATCH",
|
||||||
|
body: { name: payload.name, body: payload.body, prerelease: payload.prerelease },
|
||||||
|
});
|
||||||
|
if (upd.status === 200) {
|
||||||
|
gh = upd.body;
|
||||||
|
console.log("Updated existing GitHub release to match Gitea (title/body/prerelease)");
|
||||||
|
} else {
|
||||||
|
console.log("WARN: could not update release fields:", upd.status);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log(`GitHub release ${tag} already exists and matches - no field changes`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error("ERROR: GitHub API error", hr.status, hr.body);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Mirror assets (download from Gitea, upload to GitHub, size-verified)
|
||||||
|
const gassets = g.assets || [];
|
||||||
|
for (const a of gassets) {
|
||||||
|
const existing = (gh.assets || []).find((x) => x.name === a.name);
|
||||||
|
if (existing && existing.size === a.size) {
|
||||||
|
console.log(`Asset ${a.name} already present (${a.size} B) - skip`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (existing) {
|
||||||
|
const d = curlJson(`https://api.github.com/repos/${REPO}/releases/assets/${existing.id}`, { token: GH_PAT, method: "DELETE" });
|
||||||
|
console.log(`Removed mismatched asset ${a.name} (HTTP ${d.status})`);
|
||||||
|
}
|
||||||
|
const tmp = "/tmp/" + a.name.replace(/[^\w.\-]/g, "_");
|
||||||
|
console.log(`Downloading ${a.name} (${a.size} B) from Gitea...`);
|
||||||
|
curlDownload(a.browser_download_url, tmp, process.env.GITEA_TOKEN || "");
|
||||||
|
const got = fs.statSync(tmp).size;
|
||||||
|
if (got !== a.size) {
|
||||||
|
console.error(`ERROR: download size mismatch ${got} != ${a.size}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const up = spawnSync("curl", [
|
||||||
|
"-sS", "--fail", "--retry", "3", "-X", "POST",
|
||||||
|
"-H", "Authorization: token " + GH_PAT,
|
||||||
|
"-H", "Content-Type: application/octet-stream",
|
||||||
|
"--data-binary", "@" + tmp,
|
||||||
|
`https://uploads.github.com/repos/${REPO}/releases/${gh.id}/assets?name=${encodeURIComponent(a.name)}`,
|
||||||
|
"-o", "/tmp/gh-upload.json",
|
||||||
|
], { encoding: "utf8" });
|
||||||
|
fs.unlinkSync(tmp);
|
||||||
|
if (up.error || up.status !== 0) {
|
||||||
|
console.error("ERROR: upload failed:", up.stderr, fs.existsSync("/tmp/gh-upload.json") ? fs.readFileSync("/tmp/gh-upload.json", "utf8") : "");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
console.log(`Uploaded ${a.name} to GitHub`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Final verification: refetch GitHub release, asset names + sizes must match
|
||||||
|
const verify = curlJson(`https://api.github.com/repos/${REPO}/releases/${gh.id}`, { token: GH_PAT });
|
||||||
|
let ok = verify.status === 200;
|
||||||
|
for (const a of gassets) {
|
||||||
|
const m = ((verify.body || {}).assets || []).find((x) => x.name === a.name);
|
||||||
|
if (!m || m.size !== a.size) {
|
||||||
|
ok = false;
|
||||||
|
console.error(`VERIFY FAIL: ${a.name} missing or size mismatch on GitHub`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!ok) process.exit(2);
|
||||||
|
console.log(`DONE: ${tag} mirrored to GitHub - ${gassets.length} asset(s) verified, prerelease=${payload.prerelease}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
SCRIPT_EOF
|
||||||
|
node /tmp/mirror-release.cjs
|
||||||
Reference in new issue
Block a user