Github Actions to deploy a preactjs website to Github pages (also installable(PWA))preactjs's issue, but I can not find any good tutorial when I trying to do this. So I want to share my exp here, maybe can provide someone who are in need in the future.Github Pages normally under a REPO_NAME, but not a domain, exhttps://Useranme.github.io/repro_name/https://Useranme.github.io/"preact": "^10.0.1""preact-router": "^3.0.0"master branch to trigger Github Actionspackage.lock.jsonmaster or package.lock.json -> step 4, 5, 6Deploy Keys and add your public key with the Allow write accessSecrets and add your private key as ACTIONS_DEPLOY_KEY <--!!!dot-jsonnpm i --save-dev dot-jsonpackage.json script: "build:gh": "GITHUB_PAGES=REPO_NAME preact build && dot-json ./build/manifest.json start_url \"/REPO_NAME/\""2 REPO_NAME with your repo name !!!.gitignore, remove package-lock.json.github folderworkflows folder under .github foldergh-pages.yml file under workflows folder.github/workflows/gh-pages.ymlgh-pages.ymlname: github pages
on:
push:
branches:
- master
jobs:
build-deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '10.x'
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci
- run: npm run build:gh
- name: Deploy
uses: peaceiris/actions-gh-pages@v2
env:
ACTIONS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
PUBLISH_BRANCH: gh-pages
PUBLISH_DIR: ./build
src/manifest.json, update icons src from /assets/... to ./assets/... "icons": [
{
"src": "./assets/icons/android-chrome-192x192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "./assets/icons/android-chrome-512x512.png",
"type": "image/png",
"sizes": "512x512"
}
]
preact.config.js file export default {
webpack(config, env, helpers, options) {
const publicPath = process.env.GITHUB_PAGES
? `/${process.env.GITHUB_PAGES}/`
: '/';
const ghEnv = process.env.GITHUB_PAGES
&& JSON.stringify(`${process.env.GITHUB_PAGES}`);
config.output.publicPath = publicPath;
const { plugin } = helpers.getPluginsByName(config, 'DefinePlugin')[0];
Object.assign(
plugin.definitions,
{ ['process.env.GITHUB_PAGES']: ghEnv }
);
},
};
/src/baseroute.js filelet basename = '';
if (process.env.GITHUB_PAGES) {
basename = `/${process.env.GITHUB_PAGES}`;
}
export default basename;
src/components/app.js. import and add baseroute to path // import Header from './header';
import baseroute from '../baseroute'; // <-- make sure path correctly
//...
//<Router onChange={this.handleRoute}>
<Home path={`${baseroute}/`} /> // <-----
<Profile path={`${baseroute}/profile/`} user="me" /> // <-----
<Profile path={`${baseroute}/profile/:user`} /> // <-----
//</Router>
// ...
src/components/header/index.js import and add baseroute to href import baseroute from '../../baseroute'; // <-- make sure path correctly
//....
// <nav>
<Link activeClassName={style.active} href={`${baseroute}/`}>Home</Link>
<Link activeClassName={style.active} href={`${baseroute}/profile`}>Me</Link>
<Link activeClassName={style.active} href={`${baseroute}/profile/john`}>John</Link>
// </nav>
//....
master branch, and wait Github Actions donesettings, GitHub Pages sections and switch Source to gh-pages branch.
I'm not exactly know this post is appropriate or NOT? Cuz this post is NOT a issue
thanks for All preact Team and All Contributors.
That's awesome! It will probably get more views, when you publish it as a blog post or something similar, because there is only a minor portion of Preact users, that is active in our issue tracker :)
Nonetheless, thanks a bunch for the detailed writeup :+1:
Most helpful comment
That's awesome! It will probably get more views, when you publish it as a blog post or something similar, because there is only a minor portion of Preact users, that is active in our issue tracker :)
Nonetheless, thanks a bunch for the detailed writeup :+1: