From c1ee3cb4d08fbb3f19761d31dbb45f77998b79f2 Mon Sep 17 00:00:00 2001 From: Maximilian Franzke Date: Tue, 12 Nov 2024 13:25:08 +0100 Subject: [PATCH 1/2] refactor(CI/CD): replace shell by JavaScript --- .github/scripts/build-gh-page.js | 62 ++++++++++++++++++++++++ .github/scripts/build-gh-page.sh | 54 --------------------- .github/workflows/03-deploy-gh-pages.yml | 8 +-- 3 files changed, 67 insertions(+), 57 deletions(-) create mode 100644 .github/scripts/build-gh-page.js delete mode 100755 .github/scripts/build-gh-page.sh diff --git a/.github/scripts/build-gh-page.js b/.github/scripts/build-gh-page.js new file mode 100644 index 00000000000..735d91ae450 --- /dev/null +++ b/.github/scripts/build-gh-page.js @@ -0,0 +1,62 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import tar from 'tar'; + +const NAME = process.env.NAME; +const OWNER_NAME = process.env.OWNER_NAME; +const REPO_NAME = process.env.REPO_NAME; +const RELEASE = process.env.RELEASE === 'true'; +const PRE_RELEASE = process.env.PRE_RELEASE === 'true'; + +if (!NAME) { + console.error('Error: Missing NAME variable'); + process.exit(1); +} + +console.log('➕ Create public dir'); +if (!fs.existsSync('public')) { + fs.mkdirSync('public'); +} + +console.log('📥 Get gh-pages tar'); +fetch(`https://github.com/${OWNER_NAME}/${REPO_NAME}/tarball/gh-pages`) + .then((res) => { + if (!res.ok) { + throw new Error(`Failed to fetch tarball: ${res.statusText}`); + } + return res.arrayBuffer(); + }) + .then((buffer) => { + fs.writeFileSync('gh-pages.tar.gz', Buffer.from(buffer)); + console.log('📦 Unpack Tar'); + return tar.x({ + file: 'gh-pages.tar.gz', + C: 'public', + strip: 1 + }); + }) + .then(() => { + if (RELEASE) { + console.log('🔃 Create redirect'); + const redirectContent = ``; + fs.writeFileSync( + path.join('public', 'index.html'), + redirectContent + ); + } + + console.log('👣 Move out dir'); + if (PRE_RELEASE || RELEASE) { + const versionDir = path.join('public', 'version'); + if (!fs.existsSync(versionDir)) { + console.log(' Make dir ./public/version'); + fs.mkdirSync(versionDir); + } + const nameDir = path.join(versionDir, NAME); + if (fs.existsSync(nameDir)) { + console.log(` Remove dir ./public/version/${NAME}`); + fs.rmdirSync(nameDir, { recursive: true }); + } + } + }) + .catch((err) => console.error(err)); diff --git a/.github/scripts/build-gh-page.sh b/.github/scripts/build-gh-page.sh deleted file mode 100755 index e4df3ffde06..00000000000 --- a/.github/scripts/build-gh-page.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env bash - -if [[ $NAME == "true" ]]; then - echo "Erro: Missing NAME variable" - exit 1 -fi - -echo "➕ Create public dir" -mkdir public - -echo "📥 Get gh-pages tar" -curl -L https://github.com/"$OWNER_NAME"/"$REPO_NAME"/tarball/gh-pages --output gh-pages - -echo "📦 Unpack Tar" -tar -zxf gh-pages -C public --strip-components 1 - -if [[ $RELEASE == "true" ]]; then - echo "🔃 Create redirect" - echo "" > public/index.html -fi - -echo "👣 Move out dir" -if [[ $PRE_RELEASE == "true" || $RELEASE == "true" ]]; then - if [[ ! -d ./public/version ]]; then - echo " Make dir ./public/version" - mkdir ./public/version - fi - if [[ -d ./public/version/"$NAME" ]]; then - echo " Remove dir ./public/version/$NAME" - rm -rf ./public/version/"$NAME" - fi - if [[ $RELEASE == "true" ]]; then - if [[ -d ./public/version/latest ]]; then - echo " Remove dir ./public/version/latest" - rm -rf ./public/version/latest - fi - mkdir ./public/version/latest - cp -RT ./out ./public/version/latest - echo " Copied dir out to ./public/version/latest" - fi - mv ./out ./public/version/"$NAME" - echo " Moved dir out to ./public/version/$NAME" -else - if [[ ! -d ./public/review ]]; then - echo " Make dir ./public/review" - mkdir ./public/review - fi - if [[ -d ./public/review/"$NAME" ]]; then - echo " Remove dir ./public/review/$NAME" - rm -rf ./public/review/"$NAME" - fi - mv ./out ./public/review/"$NAME" - echo " Moved dir out to ./public/review/$NAME" -fi diff --git a/.github/workflows/03-deploy-gh-pages.yml b/.github/workflows/03-deploy-gh-pages.yml index 70be335eaf8..9781002d246 100644 --- a/.github/workflows/03-deploy-gh-pages.yml +++ b/.github/workflows/03-deploy-gh-pages.yml @@ -62,15 +62,17 @@ jobs: script: return context?.payload?.repository?.name - name: 🔨 Build page + uses: actions/github-script@v7 env: RELEASE: ${{ inputs.release }} PRE_RELEASE: ${{ inputs.preRelease }} NAME: ${{ steps.extract.outputs.name }} REPO_NAME: ${{ steps.repo-name.outputs.result }} OWNER_NAME: ${{ github.repository_owner }} - run: | - chmod +rx ./.github/scripts/build-gh-page.sh - ./.github/scripts/build-gh-page.sh + with: + script: | + const { default: buildGitHubPage } = await import('${{ github.workspace }}/.github/scripts/build-gh-page.js'); + return await buildGitHubPage(); - name: 🥅 Deploy to GH-Pages uses: peaceiris/actions-gh-pages@v4 From d9d2fa448690ce587acef55f63254e6b18808e29 Mon Sep 17 00:00:00 2001 From: Maximilian Franzke Date: Tue, 12 Nov 2024 14:53:12 +0100 Subject: [PATCH 2/2] refactor: we obviously need to provide and export that function --- .github/scripts/build-gh-page.js | 109 ++++++++++++++++--------------- 1 file changed, 56 insertions(+), 53 deletions(-) diff --git a/.github/scripts/build-gh-page.js b/.github/scripts/build-gh-page.js index 735d91ae450..98c7878daae 100644 --- a/.github/scripts/build-gh-page.js +++ b/.github/scripts/build-gh-page.js @@ -2,61 +2,64 @@ import fs from 'node:fs'; import path from 'node:path'; import tar from 'tar'; -const NAME = process.env.NAME; -const OWNER_NAME = process.env.OWNER_NAME; -const REPO_NAME = process.env.REPO_NAME; -const RELEASE = process.env.RELEASE === 'true'; -const PRE_RELEASE = process.env.PRE_RELEASE === 'true'; +const buildGitHubPage = () => { + const NAME = process.env.NAME; + const OWNER_NAME = process.env.OWNER_NAME; + const REPO_NAME = process.env.REPO_NAME; + const RELEASE = process.env.RELEASE === 'true'; + const PRE_RELEASE = process.env.PRE_RELEASE === 'true'; -if (!NAME) { - console.error('Error: Missing NAME variable'); - process.exit(1); -} + if (!NAME) { + console.error('Error: Missing NAME variable'); + process.exit(1); + } -console.log('➕ Create public dir'); -if (!fs.existsSync('public')) { - fs.mkdirSync('public'); -} + console.log('➕ Create public dir'); + if (!fs.existsSync('public')) { + fs.mkdirSync('public'); + } -console.log('📥 Get gh-pages tar'); -fetch(`https://github.com/${OWNER_NAME}/${REPO_NAME}/tarball/gh-pages`) - .then((res) => { - if (!res.ok) { - throw new Error(`Failed to fetch tarball: ${res.statusText}`); - } - return res.arrayBuffer(); - }) - .then((buffer) => { - fs.writeFileSync('gh-pages.tar.gz', Buffer.from(buffer)); - console.log('📦 Unpack Tar'); - return tar.x({ - file: 'gh-pages.tar.gz', - C: 'public', - strip: 1 - }); - }) - .then(() => { - if (RELEASE) { - console.log('🔃 Create redirect'); - const redirectContent = ``; - fs.writeFileSync( - path.join('public', 'index.html'), - redirectContent - ); - } - - console.log('👣 Move out dir'); - if (PRE_RELEASE || RELEASE) { - const versionDir = path.join('public', 'version'); - if (!fs.existsSync(versionDir)) { - console.log(' Make dir ./public/version'); - fs.mkdirSync(versionDir); + console.log('📥 Get gh-pages tar'); + fetch(`https://github.com/${OWNER_NAME}/${REPO_NAME}/tarball/gh-pages`) + .then((res) => { + if (!res.ok) { + throw new Error(`Failed to fetch tarball: ${res.statusText}`); + } + return res.arrayBuffer(); + }) + .then((buffer) => { + fs.writeFileSync('gh-pages.tar.gz', Buffer.from(buffer)); + console.log('📦 Unpack Tar'); + return tar.x({ + file: 'gh-pages.tar.gz', + C: 'public', + strip: 1 + }); + }) + .then(() => { + if (RELEASE) { + console.log('🔃 Create redirect'); + const redirectContent = ``; + fs.writeFileSync( + path.join('public', 'index.html'), + redirectContent + ); } - const nameDir = path.join(versionDir, NAME); - if (fs.existsSync(nameDir)) { - console.log(` Remove dir ./public/version/${NAME}`); - fs.rmdirSync(nameDir, { recursive: true }); + + console.log('👣 Move out dir'); + if (PRE_RELEASE || RELEASE) { + const versionDir = path.join('public', 'version'); + if (!fs.existsSync(versionDir)) { + console.log(' Make dir ./public/version'); + fs.mkdirSync(versionDir); + } + const nameDir = path.join(versionDir, NAME); + if (fs.existsSync(nameDir)) { + console.log(` Remove dir ./public/version/${NAME}`); + fs.rmdirSync(nameDir, { recursive: true }); + } } - } - }) - .catch((err) => console.error(err)); + }) + .catch((err) => console.error(err)); +}; +export default buildGitHubPage;