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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have this dependency in our package.json?


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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use async & await here, instead of multiple then

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.

20 changes: 20 additions & 0 deletions .github/scripts/get-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const GITHUB_REF = process.env.GITHUB_REF;
const GITHUB_ACTOR = process.env.GITHUB_ACTOR;
const GITHUB_COMMITISH = process.env.GITHUB_COMMITISH;
const GITHUB_PRE_RELEASE = process.env.GITHUB_PRE_RELEASE === 'true';

if (GITHUB_REF && GITHUB_REF.startsWith('refs/tags/v')) {
if (GITHUB_ACTOR !== 'dependabot[bot]') {
if (GITHUB_COMMITISH === 'main' && !GITHUB_PRE_RELEASE) {
console.log('RELEASE');
} else {
console.log('PRE_RELEASE');
}
} else {
console.error('Dependabot has no permission to publish!');
process.exit(1);
}
} else {
console.error("Your tag has to start with 'v'");
process.exit(1);
}
17 changes: 0 additions & 17 deletions .github/scripts/get-release.sh

This file was deleted.

32 changes: 32 additions & 0 deletions .github/scripts/package-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import findVersions from 'find-versions';

const TAG = process.env.TAG;
const RELEASE = process.env.RELEASE === 'true';
const PRE_RELEASE = process.env.PRE_RELEASE === 'true';
const GITHUB_SHA = process.env.GITHUB_SHA;

const SEMVER_VERSION = findVersions(TAG).toString();

if (RELEASE) {
if (SEMVER_VERSION.includes('-')) {
console.error(
`Version ${SEMVER_VERSION} contains hyphen, maybe you forgot to check the prerelease checkbox in GitHub release draft. A release should not have a hyphen!`
);
process.exit(1);
}
console.log(SEMVER_VERSION);
} else if (PRE_RELEASE) {
if (SEMVER_VERSION.includes('-')) {
const GITHUB_SHA_SHORT = GITHUB_SHA.substring(0, 7);
const VALID_SEMVER_VERSION = `${SEMVER_VERSION}-${GITHUB_SHA_SHORT}`;
console.log(VALID_SEMVER_VERSION);
} else {
console.error(
`Version ${SEMVER_VERSION} doesn't contain a hyphen. A prerelease should have a hyphen!`
);
process.exit(1);
}
} else {
console.error('nothing found in environment for RELEASE or PRE_RELEASE');
process.exit(1);
}
22 changes: 0 additions & 22 deletions .github/scripts/package-version.sh

This file was deleted.

6 changes: 2 additions & 4 deletions .github/workflows/01-get-publish-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ jobs:
- name: 💃🕺 Check if release or prerelease
id: releaseCheck
run: |
chmod +rx ./.github/scripts/get-release.sh
OUTPUT=$(./.github/scripts/get-release.sh)
OUTPUT=$(node .github/scripts/get-release.js)
if [[ $OUTPUT == "RELEASE" ]];
then
echo "release=true" >> $GITHUB_OUTPUT
Expand All @@ -60,8 +59,7 @@ jobs:
PRE_RELEASE: ${{ steps.releaseCheck.outputs.preRelease }}
TAG: ${{ steps.extractTag.outputs.tag }}
run: |
chmod +rx ./.github/scripts/package-version.sh
OUTPUT=$(./.github/scripts/package-version.sh)
OUTPUT=$(node .github/scripts/package-version.js)
echo "version=$OUTPUT" >> $GITHUB_OUTPUT

- name: 🌳 Log Valid Version
Expand Down
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
Loading