Skip to content

Commit

Permalink
feat(day06): bazel + vite + github action to deploy a static page
Browse files Browse the repository at this point in the history
  • Loading branch information
dorayx committed Sep 11, 2023
1 parent 1be7db7 commit cebabb5
Show file tree
Hide file tree
Showing 29 changed files with 522 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-2103660156
pnpm-lock.yaml=-128733183
pnpm-lock.yaml=-853535587
challenges/day-02/package.json=1560744658
challenges/day-04/package.json=1436213359
challenges/day-06/package.json=189209422
package.json=-950038973
pnpm-workspace.yaml=683860905
pnpm-workspace.yaml=840565732
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
challenges/day-02/node_modules
challenges/day-04/node_modules
challenges/day-06/node_modules
82 changes: 82 additions & 0 deletions .github/workflows/github-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Simple workflow for deploying static content to GitHub Pages
name: GitHub Pages

on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install NodeJS
uses: actions/setup-node@v3
with:
node-version-file: .nvmrc

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install Bazelisk
uses: bazelbuild/setup-bazelisk@v2

- name: Setup bazel cache
uses: actions/cache@v3
with:
path: "~/.cache/bazel"
key: bazel-linux

- name: Install NodeJS dependencies
run: pnpm install

- name: Build with Bazel
run: |
bazel build //tools:build_packages --runs_on=linux
bazel build //pages:github-pages
- name: Setup Pages
uses: actions/configure-pages@v3

- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
# Upload entire repository
path: 'bazel-bin/pages/artifacts'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ and the projects here are all experimental, not always complete and not producti

I've tracked my practices in the table below:

| Day | Date | State | Challenge | Keywords | Demo | Source Code |
|:-----:|:-------------:|:-----:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------|:----:|:----------------------------------------:|
| 01 | 3 Sep, 2023 || ⚙️ Initialize this repository as a monorepo using Bazel for multi-language development, include a Rust program example and add a GitHub Action to test all the projects | Bazel, Rust | - | [challenges/day-01](./challenges/day-01) |
| 02 | 4 Sep, 2023 || ⚙️ Integrate NodeJS + PNPM Workspace + React + TypeScript + Vite + Vitest with Bazel | Bazel, NodeJS, PNPM Workspace | - | [challenges/day-02](./challenges/day-02) |
| 03 | 6 Sep, 2023 || 🍎 Create a macOS menu bar app with several menu items | Bazel, macOS App, Tray App | - | [challenges/day-03](./challenges/day-03) |
| 04 | 8 Sep, 2023 | ⚠️ | ⏰ Use CSS Scroll Snap to create a clock | CSS Scroll Snap | 🚧 | [challenges/day-04](./challenges/day-04) |
| 05 | 10 Sep, 2023 || ⚙️ Integrate Go Workspace with Bazel | Bazel, Go Workspace | - | [challenges/day-05](./challenges/day-05) |
| Day | Date | State | Challenge | Keywords | Demo | Source Code |
|:-----:|:-------------:|:-----:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------|:------------:|:----------------------------------------:|
| 01 | 3 Sep, 2023 || ⚙️ Initialize this repository as a monorepo using Bazel for multi-language development, include a Rust program example and add a GitHub Action to test all the projects | Bazel, Rust | - | [challenges/day-01](./challenges/day-01) |
| 02 | 4 Sep, 2023 || ⚙️ Integrate NodeJS + PNPM Workspace + React + TypeScript + Vite + Vitest with Bazel | Bazel, NodeJS, PNPM Workspace | - | [challenges/day-02](./challenges/day-02) |
| 03 | 6 Sep, 2023 || 🍎 Create a macOS menu bar app with several menu items | Bazel, macOS App, Tray App | - | [challenges/day-03](./challenges/day-03) |
| 04 | 8 Sep, 2023 | ⚠️ | ⏰ Use CSS Scroll Snap to create a clock | CSS Scroll Snap | 🚧 | [challenges/day-04](./challenges/day-04) |
| 05 | 10 Sep, 2023 || ⚙️ Integrate Go Workspace with Bazel | Bazel, Go Workspace | - | [challenges/day-05](./challenges/day-05) |
| 06 | 11 Sep, 2023 | 🚧 | ⚙️ Deploy static HTML to GitHub Pages with Bazel + GitHub Action | Bazel, GitHub Pages, Static HTML | GitHub Pages | [challenges/day-06](./challenges/day-06) |

- ✅: Completed
- ⚠️: Need improvements
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ npm_translate_lock(
data = [
"@//:challenges/day-02/package.json",
"@//:challenges/day-04/package.json",
"@//:challenges/day-06/package.json",
"@//:package.json",
"@//:pnpm-workspace.yaml",
],
Expand Down
71 changes: 71 additions & 0 deletions challenges/day-06/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//:vite/package_json.bzl", vite_bin = "bin")
load("@npm//:vitest/package_json.bzl", vitest_bin = "bin")

# This macro expands to a link_npm_package for each third-party package in package.json
# See https://docs.aspect.build/rules/aspect_rules_js/docs/pnpm
npm_link_all_packages(name = "node_modules")

package(default_visibility = ["//visibility:public"])

filegroup(
name = "config",
srcs = [
"index.html",
"package.json",
"tsconfig.json",
"tsconfig.node.json",
"vite.config.ts",
],
)

filegroup(
name = "vite_srcs_export",
srcs = glob(["src/**/*"]),
visibility = ["//visibility:public"],
)

filegroup(
name = "vite_public_export",
srcs = glob(["public/**/*"]),
visibility = ["//visibility:public"],
)

filegroup(
name = "vite_srcs_tests",
srcs = glob([
"src/**/*.spec.ts",
"tests/**/*",
]),
visibility = ["//visibility:public"],
)

vite_bin.vite(
name = "build",
srcs = [
":config",
":node_modules",
":vite_public_export",
":vite_srcs_export",
],
args = ["build"],
chdir = package_name(),
out_dirs = ["canary"],
visibility = ["//visibility:public"],
)

vitest_bin.vitest_test(
name = "ut_test",
args = [
"run",
],
chdir = package_name(),
data = [
":config",
":node_modules",
":vite_public_export",
":vite_srcs_export",
":vite_srcs_tests",
],
visibility = ["//visibility:public"],
)
36 changes: 36 additions & 0 deletions challenges/day-06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Day 06

## Challenge description

Deploy static HTML to GitHub Pages with Bazel + GitHub Action.

## Getting Started

### Use PNPM to install dependencies

```bash
pnpm install
```

### Use PNPM to develop

```bash
pnpm --filter day-06 dev
```

### Use Bazel to build and test

```bash
bazel build //challenges/day-06:build
bazel test //challenges/day-06:ut_test
```

## Build Assets

- The output directory is `/canary`
- The public assets path is `/canary`

## References

- Doc: [actions/upload-pages-artifact](https://github.com/actions/upload-pages-artifact)
- Doc: [aspect-build/bazel-lib - copy_to_directory](https://github.com/aspect-build/bazel-lib/blob/main/docs/copy_to_directory.md)
13 changes: 13 additions & 0 deletions challenges/day-06/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions challenges/day-06/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "day-06",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7"
}
}
1 change: 1 addition & 0 deletions challenges/day-06/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions challenges/day-06/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo.bazel:hover {
filter: drop-shadow(0 0 2em #72ca71);
}
.logo.vite:hover {
filter: drop-shadow(0 0 2em rgba(100, 108, 255, 0.67));
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a .logo.react {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
35 changes: 35 additions & 0 deletions challenges/day-06/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useState } from 'react';
import reactLogo from './assets/react.svg';
import viteLogo from '/vite.svg';
import bazelLogo from './assets/bazel.svg';
import './App.css';

function App() {
const [count, setCount] = useState(0);

return (
<>
<div>
<a href="https://bazel.build" target="_blank">
<img src={bazelLogo} className="logo bazel" alt="Bazel logo" />
</a>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo vite" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Bazel + Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">Click on the Bazel, Vite and React logos to learn more</p>
</>
);
}

export default App;
11 changes: 11 additions & 0 deletions challenges/day-06/src/assets/bazel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions challenges/day-06/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cebabb5

Please sign in to comment.