[GitHub] Actions

張庭瑋
3 min readApr 19, 2023

最近在尋找免費的雲服務來上傳 Node.js 應用程式時

對於 render 感到十分佩服

只要稍微設定過,就能在每次 github 專案的指定 branch 有新的 commit 時

自動 deploy

後來突然想到

每次自我介紹更新時,都要另外手動 deploy 到 github page

應該也可以這樣有新的 commit 時自動 deploy 吧?

首先在專案的根目錄建立 .github/workflows 資料夾

接著建立 build.yml 檔案選

name: Build and Deploy

on:
push:
branches:
- master # 選擇要監聽的 branch

jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: 16.13.0 # 需要的 node 版本
- name: Install dependencies
run: yarn install # 需要的 install 指令
- name: Build
run: yarn build # 需要的 build 指令
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.AUTO_TOKEN }} # token
publish_dir: ./dist # build 檔案路徑
publish_branch: gh-pages

裡面當然需要設定 github token 了

首先先創建一個新的 token(自己要保存好)

並且選取 workflow

由於 token 是機密資料

不該直接寫在專案裡,有可能會被別人盜用

就算直接寫在專案裡,github 也會自動將 token 作廢

所以需要設定 secrets(類似環境變數)

打開專案的 Settings tab, 選取 Secrets and variables 的 Actions

點擊 New repository secret 並加入剛剛新增的 token

而這裡我的 Name 的設定是 AUTO_TOKEN

這樣就可以藉由secrets.AUTO_TOKEN來取得我設定的 token 了

往後只要 master 分支有新的 commit,就會自動幫我上傳到 github page 了

不得不說越來越方便了...

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response