mirror of
https://github.com/daydreamer-json/ak-endfield-api-archive.git
synced 2026-03-22 07:12:28 +01:00
ci: prevent duplicate execution in github actions
This commit is contained in:
@@ -13,7 +13,12 @@ import logger from '../utils/logger.js';
|
||||
import mathUtils from '../utils/math.js';
|
||||
import stringUtils from '../utils/string.js';
|
||||
|
||||
let githubAuthCfg: { token: string; owner: string; repo: string; tag: string } | null = null;
|
||||
let githubAuthCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null = null;
|
||||
let octoClient: Octokit | null = null;
|
||||
let saveToGHMirrorNeedUrls: { url: string; name: string | null }[] = [];
|
||||
|
||||
@@ -165,7 +170,7 @@ async function saveToGHMirror(url: string, name: string | null): Promise<void> {
|
||||
if (githubAuthCfg) {
|
||||
mirrorFileDb.push({
|
||||
orig: stringUtils.removeQueryStr(url),
|
||||
mirror: `https://github.com/${githubAuthCfg.owner}/${githubAuthCfg.repo}/releases/download/${githubAuthCfg.tag}/${name ?? new URL(url).pathname.split('/').pop() ?? ''}`,
|
||||
mirror: `https://github.com/${githubAuthCfg.github.relArchive.owner}/${githubAuthCfg.github.relArchive.repo}/releases/download/${githubAuthCfg.github.relArchive.tag}/${name ?? new URL(url).pathname.split('/').pop() ?? ''}`,
|
||||
});
|
||||
await Bun.write(localJsonPath, JSON.stringify(mirrorFileDb));
|
||||
}
|
||||
@@ -503,7 +508,7 @@ async function fetchAndSaveLatestGames(gameTargets: GameTarget[]) {
|
||||
const unique = [...new Map(hashPair.map((item) => [item.md5, item])).values()];
|
||||
const subChns = appConfig.network.api.akEndfield.subChannel;
|
||||
unique.forEach((e) => {
|
||||
if ([subChns.cnWinRel, subChns.osWinRel].includes(target.subChannel)) {
|
||||
if ([subChns.cnWinRel, subChns.cnWinRelBilibili, subChns.osWinRel].includes(target.subChannel)) {
|
||||
saveToGHMirrorNeedUrls.push({ url: e.url, name: null });
|
||||
}
|
||||
});
|
||||
@@ -591,7 +596,7 @@ async function fetchAndSaveLatestGamePatches(gameTargets: GameTarget[]) {
|
||||
const unique = [...new Map(hashPair.map((item) => [item.md5, item])).values()];
|
||||
const subChns = appConfig.network.api.akEndfield.subChannel;
|
||||
unique.forEach((e) => {
|
||||
if ([subChns.cnWinRel, subChns.osWinRel].includes(target.subChannel)) {
|
||||
if ([subChns.cnWinRel, subChns.cnWinRelBilibili, subChns.osWinRel].includes(target.subChannel)) {
|
||||
const urlObj = new URL(e.url);
|
||||
urlObj.search = '';
|
||||
saveToGHMirrorNeedUrls.push({
|
||||
@@ -781,14 +786,19 @@ async function fetchAndSaveLatestLauncher(launcherTargets: LauncherTarget[]) {
|
||||
async function mainCmdHandler() {
|
||||
githubAuthCfg = await (async () => {
|
||||
if (await Bun.file('config/config_auth.yaml').exists()) {
|
||||
return YAML.parse(await Bun.file('config/config_auth.yaml').text()).github;
|
||||
return YAML.parse(await Bun.file('config/config_auth.yaml').text());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
if (githubAuthCfg) {
|
||||
logger.info('Logging in to GitHub using a PAT');
|
||||
octoClient = new Octokit({ auth: githubAuthCfg.token });
|
||||
octoClient = new Octokit({ auth: githubAuthCfg.github.relArchive.token });
|
||||
}
|
||||
|
||||
if ((await githubUtils.checkIsActionRunning(githubAuthCfg)) === true) {
|
||||
logger.error('Duplicate exec of a GitHub Actions workflow has been detected');
|
||||
throw new Error('Github Actions workflow duplicate exec detected');
|
||||
}
|
||||
|
||||
const cfg = appConfig.network.api.akEndfield;
|
||||
|
||||
@@ -5,7 +5,12 @@ import logger from './logger.js';
|
||||
|
||||
async function uploadAsset(
|
||||
client: Octokit | null,
|
||||
authCfg: { token: string; owner: string; repo: string; tag: string } | null,
|
||||
authCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null,
|
||||
url: string,
|
||||
targetFileName: string | null,
|
||||
) {
|
||||
@@ -20,8 +25,8 @@ async function uploadAsset(
|
||||
const binSize: number = bin.byteLength;
|
||||
logger.info(`Mirror archive: Uploading ${new URL(url).pathname.split('/').pop()} ...`);
|
||||
await client.rest.repos.uploadReleaseAsset({
|
||||
owner: authCfg.owner,
|
||||
repo: authCfg.repo,
|
||||
owner: authCfg.github.relArchive.owner,
|
||||
repo: authCfg.github.relArchive.repo,
|
||||
release_id: releaseId,
|
||||
name,
|
||||
data: bin as any,
|
||||
@@ -31,18 +36,42 @@ async function uploadAsset(
|
||||
|
||||
async function getReleaseInfo(
|
||||
client: Octokit | null,
|
||||
authCfg: { token: string; owner: string; repo: string; tag: string } | null,
|
||||
authCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null,
|
||||
) {
|
||||
if (!client || !authCfg) return;
|
||||
const { data: release } = await client.rest.repos.getReleaseByTag({
|
||||
owner: authCfg.owner,
|
||||
repo: authCfg.repo,
|
||||
tag: authCfg.tag,
|
||||
owner: authCfg.github.relArchive.owner,
|
||||
repo: authCfg.github.relArchive.repo,
|
||||
tag: authCfg.github.relArchive.tag,
|
||||
});
|
||||
return release;
|
||||
}
|
||||
|
||||
async function checkIsActionRunning(
|
||||
authCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null,
|
||||
): Promise<boolean> {
|
||||
if (!authCfg) return false;
|
||||
logger.debug('Checking GitHub Actions running status ...');
|
||||
const client = new Octokit({ auth: authCfg.github.main.token });
|
||||
const data = await client.rest.actions.listWorkflowRunsForRepo({
|
||||
owner: authCfg.github.main.owner,
|
||||
repo: authCfg.github.main.repo,
|
||||
});
|
||||
return data.data.workflow_runs.filter((e) => e.status !== 'completed').length > 1;
|
||||
}
|
||||
|
||||
export default {
|
||||
uploadAsset,
|
||||
getReleaseInfo,
|
||||
checkIsActionRunning,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user