Skip to content

Commit

Permalink
add cron script to regularly check for latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Nov 17, 2023
1 parent 5207d86 commit 649dec2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Docker Build
on:
workflow_dispatch:
inputs:
prestoVersion:
version:
description: 'Presto version to build'
required: true
type: string
Expand All @@ -16,6 +16,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building prestodb-sandbox:${{ inputs.version }}"
- name: Checkout
uses: actions/checkout@v4
- name: Docker meta
Expand All @@ -24,7 +25,7 @@ jobs:
with:
images: ghcr.io/popsql/prestodb-sandbox
tags: |
type=raw,value=${{ inputs.prestoVersion }}
type=raw,value=${{ inputs.version }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/version_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Version Check

on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:


jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup node 20
uses: actions/setup-node@v4
with:
node-version: 20.x
- run: node scripts/trigger_workflow.js
84 changes: 84 additions & 0 deletions scripts/trigger_workflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Script that checks what the latest version of presto is, and triggers our build.yml workflow
* if we have not yet built that version.
*/

const ghToken = process.env.GITHUB_TOKEN;

const getLatestVersion = async () => {
const req = await fetch('https://api.github.com/repos/prestodb/presto/git/refs/tags', {
headers: {
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28'
},
method: 'GET',
});

const json = await req.json();
const versions = json.reduce((acc, { ref }) => {
const version = ref.replace('refs/tags/', '');
if (!Number.isNaN(Number(version))) {
acc.push(version);
}
return acc;
}, []).sort((a, b) => {
const splitA = a.split('.').map(Number);
const splitB = b.split('.').map(Number);

for (let i = 0; i < splitA.length; i++) {
if (splitA[i] > splitB[i]) {
return -1;
}
if (splitA[i] < splitB[i]) {
return 1;
}
}
return 0;
});
for (const version of versions) {
const req = await fetch(`https://repo1.maven.org/maven2/com/facebook/presto/presto-server/${version}`);
if (req.ok) {
return version;
}
}
throw new Error('Could not determine latest version');
}

const imageExists = async (version) => {
const token = await getToken();
const req = await fetch(`https://ghcr.io/v2/popsql/prestodb-sandbox/manifests/${version}`, {
headers: {
Authorization: `Bearer ${ghToken}`
}
});
return req.ok;
}

(async () => {
const version = await getLatestVersion();
const check = await imageExists(version);
if (!check) {
const req = await fetch(
'https://api.github.com/repos/popsql/prestodb-sandbox/actions/workflows/build.yml/dispatches',
{
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${ghToken}`,
'X-GitHub-Api-Version': '2022-11-28'
},
body: JSON.stringify({
version,
}),
},
);
if (!req.ok) {
throw new Error('Could not trigger workflow');
}
console.log(`Triggered workflow for ${version}`);
}
})().then(() => {
process.exit(0);
}).catch((err) => {
console.error(err);
process.exit(1);
});

0 comments on commit 649dec2

Please sign in to comment.