Skip to content

Commit 62aa25a

Browse files
authored
Use TS to fetch tags (#940)
* Use TS to fetch tags * Compare manifests to get the right tag * Address coderabbit feedback
1 parent f6a1397 commit 62aa25a

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ jobs:
4646
- name: Get AppBuilder Version
4747
id: get_version
4848
run: |
49-
# Get tag from GHCR
50-
TOKEN_JSON=$(curl https://ghcr.io/token\?scope\="repository:sillsdev/app-builders:pull")
51-
TOKEN=$(echo "$TOKEN_JSON" | jq -r ".token")
52-
TAG_DATA=$(curl -H "Authorization: Bearer $TOKEN" https://ghcr.io/v2/sillsdev/app-builders/tags/list)
53-
NUM_TAGS=$(echo "$TAG_DATA" | jq -r '.tags | length')
54-
FROM_GHCR=$(echo "$TAG_DATA" | jq -r ".tags[$(("$NUM_TAGS" - 1))]")
55-
echo "Latest tag of sillsdev/app-builders is: $FROM_GHCR"
56-
echo "appbuilder_version=$FROM_GHCR" >> $GITHUB_OUTPUT
49+
npx ts-node convert/fetchTags.ts >> $GITHUB_OUTPUT
5750
5851
- name: Restore Projects cache
5952
id: restore-projects-cache

convert/fetchTags.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { exit } from 'node:process';
2+
import { compareVersions } from './stringUtils';
3+
4+
const verNumRegExp = /^\d+(\.\d+){0,2}$/;
5+
6+
async function getDigest(token: string, ref: string) {
7+
return (
8+
await fetch(`https://ghcr.io/v2/sillsdev/app-builders/manifests/${ref}`, {
9+
headers: { Authorization: `Bearer ${token}` }
10+
}).then((r) => r.json())
11+
).config.digest as string;
12+
}
13+
14+
(async () => {
15+
try {
16+
const token = (
17+
await fetch('https://ghcr.io/token?scope=repository:sillsdev/app-builders:pull').then(
18+
(r) => r.json()
19+
)
20+
).token as string;
21+
22+
const tags = (
23+
await fetch('https://ghcr.io/v2/sillsdev/app-builders/tags/list', {
24+
headers: { Authorization: `Bearer ${token}` }
25+
}).then((r) => r.json())
26+
).tags as string[];
27+
28+
const sortedTags = tags.filter((t) => t.match(verNumRegExp)).sort(compareVersions);
29+
30+
const latestDigest = await getDigest(token, 'latest');
31+
32+
let i = sortedTags.length;
33+
let digest = '';
34+
do {
35+
i--;
36+
digest = await getDigest(token, sortedTags[i]);
37+
} while (i > 0 && latestDigest !== digest);
38+
39+
console.log(`appbuilder_version=${sortedTags[i]}`);
40+
} catch (e) {
41+
console.error(e);
42+
exit(1);
43+
}
44+
})().catch((r) => {
45+
console.error(r);
46+
exit(1);
47+
});

0 commit comments

Comments
 (0)