Skip to content

Commit 4d4fbd8

Browse files
committed
Added automated deployment with GitHub Actions
1 parent 9e54733 commit 4d4fbd8

79 files changed

Lines changed: 121 additions & 507 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/deploy.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Build and Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
10+
permissions:
11+
contents: read
12+
pages: write
13+
id-token: write
14+
15+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
16+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
17+
concurrency:
18+
group: "pages"
19+
cancel-in-progress: false
20+
21+
jobs:
22+
# Build job
23+
build:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v4
28+
29+
- name: Setup Node.js
30+
uses: actions/setup-node@v4
31+
with:
32+
node-version: "20"
33+
cache: "npm"
34+
cache-dependency-path: "build/package-lock.json"
35+
36+
- name: Install dependencies
37+
run: |
38+
cd build
39+
npm ci
40+
41+
- name: Build site
42+
run: |
43+
node build/build.js
44+
45+
- name: Setup Pages
46+
uses: actions/configure-pages@v4
47+
48+
- name: Upload artifact
49+
uses: actions/upload-pages-artifact@v3
50+
with:
51+
path: ./docs
52+
53+
# Deployment job
54+
deploy:
55+
environment:
56+
name: github-pages
57+
url: ${{ steps.deployment.outputs.page_url }}
58+
runs-on: ubuntu-latest
59+
needs: build
60+
if: github.ref == 'refs/heads/main'
61+
steps:
62+
- name: Deploy to GitHub Pages
63+
id: deployment
64+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
*/node_modules
2-
*/__pycache__
2+
*/__pycache__
3+
4+
# Built files that's generated by the build script, deployed via GitHub Actions
5+
docs/

build/build.js

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { minify } from 'minify';
2-
import tryToCatch from 'try-to-catch';
3-
import fse from 'fs-extra';
4-
import path from 'path';
1+
import { minify } from "minify";
2+
import tryToCatch from "try-to-catch";
3+
import fse from "fs-extra";
4+
import path from "path";
55

66
function getFilesWithExtension(root, directory, extension, ignoreUnderscored) {
77
const target = root + directory;
8-
if (!fse.existsSync(target)) { // Check if the directory exists
8+
if (!fse.existsSync(target)) {
9+
// Check if the directory exists
910
console.log(`(error) \x1b[91mdirectory "${target}" does not exist!\x1b[0m`);
1011
throw new Error("FAILED");
1112
}
@@ -16,12 +17,11 @@ function getFilesWithExtension(root, directory, extension, ignoreUnderscored) {
1617
// Filter files by the extension
1718
const filtered = files.filter((file) => {
1819
const ext = path.extname(file).toLowerCase();
19-
if (ignoreUnderscored && file.startsWith('_')) return false;
20+
if (ignoreUnderscored && file.startsWith("_")) return false;
2021
return ext === extension.toLowerCase();
2122
});
2223

23-
24-
const result = filtered.map(file => directory + file);
24+
const result = filtered.map((file) => directory + file);
2525
return result;
2626
}
2727

@@ -30,16 +30,20 @@ async function calculateFileSize(fileList) {
3030
let totalSize = 0;
3131

3232
for (const filePath of fileList) {
33-
const stats = await fse.stat(filePath);
34-
35-
if (stats.isFile()) {
36-
totalSize += stats.size;
33+
// Check if file exists before trying to stat it
34+
if (fse.existsSync(filePath)) {
35+
const stats = await fse.stat(filePath);
36+
if (stats.isFile()) {
37+
totalSize += stats.size;
38+
}
3739
}
3840
}
3941

4042
return totalSize;
4143
} catch (error) {
42-
console.log("(error) \x1b[91mfailed while calculating size:\x1b[0m\n" + error.message);
44+
console.log(
45+
"(error) \x1b[91mfailed while calculating size:\x1b[0m\n" + error.message
46+
);
4347
throw new Error("FAILED");
4448
}
4549
}
@@ -53,7 +57,9 @@ async function grabAndMinify(file) {
5357

5458
const [error, data] = await tryToCatch(minify, file, options);
5559
if (error) {
56-
console.log("(error) \x1b[91merror occured while minifying:\x1b[0m\n" + error.message);
60+
console.log(
61+
"(error) \x1b[91merror occured while minifying:\x1b[0m\n" + error.message
62+
);
5763
throw new Error("FAILED");
5864
}
5965
return data;
@@ -67,21 +73,26 @@ async function main() {
6773
src = "../";
6874
dest = "../docs/";
6975
}
70-
76+
7177
const htmlFiles = getFilesWithExtension(src, "", ".html", true);
7278
const cssFiles = getFilesWithExtension(src, "css/", ".css");
7379
const jsFiles = getFilesWithExtension(src, "js/", ".js");
74-
80+
7581
const files = [...htmlFiles, ...cssFiles, ...jsFiles];
76-
const srcFiles = files.map(file => src + file);
77-
const destFiles = files.map(file => dest + file);
78-
79-
const prevTotal = await calculateFileSize(destFiles) / 1000;
82+
const srcFiles = files.map((file) => src + file);
83+
const destFiles = files.map((file) => dest + file);
84+
85+
const prevTotal = (await calculateFileSize(destFiles)) / 1000;
8086

8187
console.log(`(setup) clearing ${dest} folder and contents`);
8288
fse.rmSync(dest, { recursive: true, force: true });
8389
fse.mkdirSync(dest);
8490

91+
// Will always be 0.
92+
if (prevTotal === 0) {
93+
console.log(`(info) starting fresh build - no previous files found`);
94+
}
95+
8596
const dirs = ["js", "css"];
8697
for (let i = 0; i < dirs.length; i++) {
8798
console.log(`(setup) creating empty ${dest + dirs[i]} folder`);
@@ -103,18 +114,33 @@ async function main() {
103114
console.log(`\n(recursive copy) ${src + "assets"} --> ${dest + "assets"}`);
104115
fse.copySync(src + "assets", dest + "assets", { overwrite: true });
105116

106-
const srcTotal = await calculateFileSize(srcFiles) / 1000;
107-
const destTotal = await calculateFileSize(destFiles) / 1000;
117+
const srcTotal = (await calculateFileSize(srcFiles)) / 1000;
118+
const destTotal = (await calculateFileSize(destFiles)) / 1000;
108119

109120
console.log(`\n(result) raw size: \x1b[93m${srcTotal}\x1b[0m kb`);
110121
console.log(`(result) minified size: \x1b[93m${destTotal}\x1b[0m kb`);
111-
console.log(`(result) compressed by \x1b[92m${Math.round((1 - (destTotal / srcTotal)) * 100)}%\x1b[0m\n`);
112-
122+
console.log(
123+
`(result) compressed by \x1b[92m${Math.round(
124+
(1 - destTotal / srcTotal) * 100
125+
)}%\x1b[0m\n`
126+
);
127+
113128
const sizeChange = Math.round((destTotal - prevTotal) * 100) / 100;
114129
console.log(`\n(result) previous size: \x1b[93m${prevTotal}\x1b[0m kb`);
115-
const resultColor = sizeChange > 0 ? "\x1b[91m+" : sizeChange < 0 ? "\x1b[92m" : "~";
116-
console.log(`(result) build size change: ${resultColor}${sizeChange}\x1b[0m kb`);
130+
131+
// Will always be 0.
132+
if (prevTotal === 0) {
133+
console.log(
134+
`(result) fresh build completed - final size: \x1b[93m${destTotal}\x1b[0m kb`
135+
);
136+
} else {
137+
const resultColor =
138+
sizeChange > 0 ? "\x1b[91m+" : sizeChange < 0 ? "\x1b[92m" : "~";
139+
console.log(
140+
`(result) build size change: ${resultColor}${sizeChange}\x1b[0m kb`
141+
);
142+
}
117143
}
118144

119145
console.log("\n>>> Starting minification of GeoSMART site...\n");
120-
main();
146+
main();

0 commit comments

Comments
 (0)