Skip to content

Commit 28457a5

Browse files
committed
fix: Deploy workflow and error handling in builDb.js
1 parent ef9c2fa commit 28457a5

3 files changed

Lines changed: 65 additions & 31 deletions

File tree

.github/actions/buildDB/action.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ name: "Build DB"
22
description: "Compiles separate files into one json file"
33
inputs:
44
GITHUB_TOKEN:
5-
description: "The GITHUB_TOKEN secret"
5+
description: "GitHub token for authentication"
6+
required: true
67
runs:
7-
using: "node12"
8+
using: "node16"
89
main: "buildDB.js"
10+
11+
12+

.github/actions/buildDB/buildDB.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
1-
require('dotenv').config();
2-
const fs = require('fs');
3-
const path = require('path');
1+
require("dotenv").config();
2+
const fs = require("fs");
3+
const path = require("path");
44
const core = require('@actions/core');
55
const github = require('@actions/github');
6-
const dir = path.join(__dirname, '..', '..', '..', 'public', 'directory');
6+
const dir = path.join(__dirname, "..", "..", "..", "public", "directory");
77
const files = fs.readdirSync(dir);
88

9-
const gh = github.getOctokit(core.getInput('GITHUB_TOKEN', { required: true }));
9+
const gh = github.getOctokit(process.env.INPUT_GITHUB_TOKEN);
1010
const nameDb = [];
1111

1212
const build = async () => {
13-
for (let i = 0; i < files.length; i++) {
14-
const file = files[i];
15-
console.log({ i, file });
13+
for (let i = 0; i < files.length; i++) {
14+
const file = files[i];
15+
console.log({ i, file });
1616

17-
if (file.endsWith('.json')) {
18-
const data = require(path.join(dir, file));
19-
20-
await gh.users.getByUsername({username: data.githubId}).then(prof => {
21-
data.avatar_url = prof.data.avatar_url;
22-
nameDb.push(data);
23-
}).catch(console.log);
24-
}
17+
if (file.endsWith(".json")) {
18+
try {
19+
const data = require(path.join(dir, file));
20+
21+
nameDb.push(data);
22+
23+
await gh.users
24+
.getByUsername({ username: data.githubId })
25+
.then((prof) => {
26+
data.avatar_url = prof.data.avatar_url;
27+
});
28+
29+
} catch (e) {
30+
console.log(e);
31+
}
2532
}
33+
}
2634
};
2735

28-
build().then(() => {
29-
fs.writeFileSync(path.join(__dirname, '..', '..', '..', 'public', 'nameDB.json'), JSON.stringify(nameDb));
30-
}).catch(e => {
36+
build()
37+
.then(() => {
38+
fs.writeFileSync(
39+
path.join(__dirname, "..", "..", "..", "public", "nameDB.json"),
40+
JSON.stringify(nameDb)
41+
);
42+
})
43+
.catch((e) => {
3144
core.setFailed(`Failed to compile a DB, \n${e.message} `);
32-
})
45+
});

.github/workflows/gh-page.yml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
name: GH Pages
22

33
on:
4-
push:
5-
branches:
6-
- master
4+
# push:
5+
# branches:
6+
# - master
7+
workflow_dispatch:
78
workflow_run:
89
workflows: ["Merge Bot"]
910
types: completed
1011

1112
jobs:
12-
Deploy:
13+
deploy:
14+
1315
runs-on: ubuntu-latest
1416
name: Build and Deploy
17+
18+
permissions:
19+
pages: write
20+
id-token: write
21+
22+
environment:
23+
name: github-pages
24+
url: ${{ steps.deployment.outputs.page_url }}
25+
1526
steps:
1627
- name: Checkout
1728
uses: actions/checkout@v2
@@ -24,13 +35,19 @@ jobs:
2435
- name: Build DB
2536
uses: ./.github/actions/buildDB
2637
with:
27-
GITHUB_TOKEN: ${{ secrets.AVATAR_TOKEN }}
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2839

2940
- name: Build static site
3041
run: yarn run build
3142

32-
- name: Deploy
33-
uses: peaceiris/actions-gh-pages@v3
43+
- name: Upload static files as artifact
44+
id: upload
45+
uses: actions/upload-pages-artifact@v3
46+
with:
47+
path: ./dist
48+
49+
- name: Deploy to GitHub Pages
50+
id: deployment
51+
uses: actions/deploy-pages@v4
3452
with:
35-
github_token: ${{ secrets.GITHUB_TOKEN }}
36-
publish_dir: ./dist
53+
path: ./dist

0 commit comments

Comments
 (0)