Post

MonitokyoGas Frontend 1 - Design & CICD

MonitokyoGas Frontend 1 - Design & CICD

Project Overview

在這個篇中,將紀錄

  • 使用bolt.new 設計前端原型
  • 使用Github Action 自動部署Github Page

Bolt.new

因為實在不想寫前端,使用Copilot 來從零設計前端似乎容易跟我想像的不太一樣。因此使用bolt.new 來做原型。 這次使用Vue 來寫。 但也沒啥好說的:https://github.com/elelmokao/monitokyogas/tree/main/frontend

Github Page CICD

但最重要的是使用Github Action 部署Github page 來展示歷史用電量。我參考了Chirpy的CICD過程,改成了適合我目前同時配置前後端的repo。

主要為幾個重點:

  1. Setup Node.js 時,必須指定package-lock.jsonfrontend裡。
  2. 在build, setup page, Upload artifact 時都必須在frontend 裡執行。
  3. Deploy時,因為是要生成name.github.io/monitokyogas的子網頁,因此vite.config.ts中也必須調整為
1
2
3
4
5
6
7
8
9
10
11
12
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const repoName = 'monitokyogas';

  return {
    base: mode === 'production' ? `/${repoName}/` : '/',
    plugins: [vue()],
  }
})

完整個Github Action YML為下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
name: "Build and Deploy Vue to GitHub Pages"

on:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'
          cache-dependency-path: frontend/package-lock.json

      - name: Install dependencies
        working-directory: frontend
        run: npm install

      - name: Build project
        working-directory: frontend
        run: npm run build

      - name: Setup Pages
        uses: actions/configure-pages@v4

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: './frontend/dist'

  deploy:
    environment:
      name: github-pages
      url: $
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

如此一來,就可以在{name}.github.io/monitokyogas下看到自己的前端。


Ref

This post is licensed under CC BY 4.0 by the author.