A step by step tutorial to teach how to use Github Actions
to deploy a preactjs Web App
to Github pages
well (also installable(PWA)
)
I post same article on github
Github Actions
, preactjs,Github Pages
- https://github.com/flameddd/preact-github-actions-to-github-pages
- github page: https://flameddd.github.io/preact-github-actions-to-github-pages/
"preact"
:"^10.0.1"
"preact-router"
:"^3.0.0"
- use
master
branch to triggerGithub Actions
- use
package.lock.json
- If don't want to use
master
orpackage.lock.json
-> step 4, 5, 6
- If don't want to use
We deploy project is under a route NOT a domain.
- under
https://Useranme.github.io/repro_name
- NOT under
https://Useranme.github.io/
So, we need some trick to make this work.
- add repo public/private keys: follows https://github.com/peaceiris/actions-gh-pages#%EF%B8%8F-create-ssh-deploy-key
- Go to
Deploy Keys
and add your public key with theAllow write access
- Go to
Secrets
and add your private key asACTIONS_DEPLOY_KEY
<--!!!
- add
dot-json
:npm i --save-dev dot-json
- add npm script:
"build:gh": "GITHUB_PAGES=REPO_NAME preact build && dot-json ./build/manifest.json start_url \"/REPO_NAME/\""
- replace
2 REPO_NAME
with your repo name !!!
- update
.gitignore
, removepackage-lock.json
- add
.github
folder
- add
workflows
folder under.github
folder- add
gh-pages.yml
file underworkflows
folder - so, it would be
.github/workflows/gh-pages.yml
- add
- edit
gh-pages.yml
name: 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
- eidt
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"
}
]
- create
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 }
);
},
};
- add
/src/baseroute.js
file
let basename = '';
if (process.env.GITHUB_PAGES) {
basename = `/${process.env.GITHUB_PAGES}`;
}
export default basename;
- edit
src/components/app.js
. import andadd baseroute to path
// import Header from './header';
import baseroute from '../baseroute'; // <-- make sure path correctly
// export default class App extends Component {
// ...
// render() {
// return (
// <div id="app">
// <Header />
// <Router onChange={this.handleRoute}>
<Home path={`${baseroute}/`} /> // <-----
<Profile path={`${baseroute}/profile/`} user="me" /> // <-----
<Profile path={`${baseroute}/profile/:user`} /> // <-----
// </Router>
// </div>
// );
// }
// }
- edit
src/components/header/index.js
import andadd baseroute to href
// import style from './style.css';
import baseroute from '../../baseroute'; // <-- make sure path correctly
// const Header = () => (
// <header class={style.header}>
// <h1>Preact App</h1>
// <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>
// </header>
// );
- push to
master
branch, and waitactions
done - go to repo's
settings
,GitHub Pages
sections and switchSource
togh-pages
branch.
- I do it manually, but I also had exp this setting switch automatically
- as My exp, it may need wait 1 ~ 3 min github page update