From acf12f0d586391927af41c19fb2ecb384e0d84db Mon Sep 17 00:00:00 2001 From: Stefan Peters Date: Mon, 26 Feb 2024 12:10:57 +0100 Subject: [PATCH] Add update script to Docker image --- .docker/Dockerfile | 5 +++++ .docker/README.md | 10 ++++++++- .docker/update.ts | 40 ++++++++++++++++++++++++++++++++++++ .github/workflows/docker.yml | 1 + 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100755 .docker/update.ts diff --git a/.docker/Dockerfile b/.docker/Dockerfile index 39d4ca2..77a2dd6 100644 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -22,6 +22,7 @@ RUN apt install -y nodejs # Deno RUN apt install -y unzip RUN curl -fsSL https://deno.land/install.sh | sh +RUN ln -s /root/.deno/bin/deno /usr/bin/deno # Dependencies for specific vocabularies ## nkostypes @@ -30,5 +31,9 @@ RUN npm install -g wikibase-cli@latest # Make Docker-related scripts available in root folder COPY .docker/*.sh . +COPY .docker/*.ts . + +# Cache Deno dependencies for update script +RUN deno cache --reload --lock=deno.lock update.ts CMD ["bash", "entrypoint.sh"] diff --git a/.docker/README.md b/.docker/README.md index bf42455..fb69772 100644 --- a/.docker/README.md +++ b/.docker/README.md @@ -4,7 +4,8 @@ This container aims to offer all the tools and dependencies needed to build voca ## To-Dos - [ ] Test all vocabularies -- [ ] Add script that updates repo and rebuilds vocabularies where files were changed +- [x] Add script that updates repo and rebuilds vocabularies where files were changed + - [ ] Deal with modified files in the repo - [ ] Error with `nkostypes` - [ ] Error with `ssg` - [ ] `rvk`: @@ -38,6 +39,13 @@ To run an interactive shell: docker compose run -it jskos-data bash ``` +## Update Data and Rebuild Vocabularies +There is a script provided in the Docker container that updates the repository and rebuilds all vocabularies where files were changed. + +```sh +docker compose run -it jskos-data /usr/src/app/update.ts +``` + ## Publishing the Docker Image Currently, only the `master` branch will be published by the GitHub workflow, and only whenever Docker-related files (`Dockerfile`, `entrypoint.sh`) are changed. diff --git a/.docker/update.ts b/.docker/update.ts new file mode 100755 index 0000000..7aaaafa --- /dev/null +++ b/.docker/update.ts @@ -0,0 +1,40 @@ +#!/usr/bin/env -S deno run --allow-read --allow-run --allow-env --allow-sys --ext=ts --lock=/usr/src/app/deno.lock + +import { $, cd } from "npm:zx@7" + +console.log("##########", new Date(), "Updating jskos-data repository...", "##########") + +const basePath = "/jskos-data" +await cd(basePath) + +// Determine updated files before pulling +const updatedFiles = (await $`git fetch --quiet && git diff --name-only @ @{u}`.quiet()).stdout.split("\n").filter(file => file && !file.startsWith(".")) + +// TODO: Building vocabularies can modify files inside the repo, so we either need to reset it or possibly stash the changes + +// Update repo +await $`git pull` +console.log() + +if (updatedFiles.includes("package-lock.json")) { + console.log("### package-lock.json was updated, reinstalling Node.js dependencies... ###") + await $`npm ci` + console.log() +} + +const builtVocabularies = new Set() +for (const file of updatedFiles) { + const vocabulary = file.match(/^(.*)\//)?.[1] + if (!vocabulary || builtVocabularies.has(vocabulary)) { + continue + } + console.log(`##### ${vocabulary} was updated, rebuilding files... #####\n`) + try { + await cd(`${basePath}/${vocabulary}`) + await $`make -B` + } catch (error) { + console.error(`There was an error rebuilding ${vocabulary}:`, error) + } + console.log() + builtVocabularies.add(vocabulary) +} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index c346e7b..7c88efa 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -7,6 +7,7 @@ on: paths: - .docker/Dockerfile - .docker/entrypoint.sh + - .docker/update.ts # Adapted from: # - https://github.com/docker/metadata-action#semver