Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(CI/CD): replace shell script by JavaScript #3454

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .github/scripts/build-gh-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'node:fs';
import path from 'node:path';
import tar from 'tar';

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);
}

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 = `<meta http-equiv="refresh" content="0; URL=https://${OWNER_NAME}.github.io/${REPO_NAME}/version/latest" />`;
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));
};
export default buildGitHubPage;
54 changes: 0 additions & 54 deletions .github/scripts/build-gh-page.sh

This file was deleted.

8 changes: 5 additions & 3 deletions .github/workflows/03-deploy-gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading