Skip to main content

gitlab page CICD

.yml

image: node:22.14.0

stages:
- build
- deploy

cache:
key: npm-cache
paths:
- node_modules/

before_script:
- printenv
- npm install

build:
stage: build
script:
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week

pages:
stage: deploy
script:
- mkdir -p public
- cp -r dist/* public/
artifacts:
paths:
- public
only:
- release-test

當然可以!這段是 GitLab CI/CD 的設定檔 .gitlab-ci.yml,它定義了整個自動化流程的步驟。以下是逐段說明:


基本設定

image: node:22.14.0
  • 使用官方 Node.js 22.14.0 的 Docker image 作為執行環境。

階段(Stages)

stages:
- build
- deploy
  • 定義了兩個階段:

    1. build:建構專案。
    2. deploy:部署網站頁面。

快取(Cache)

cache:
key: npm-cache
paths:
- node_modules/
  • 把 node_modules 目錄快取起來,以加速後續 pipeline 執行時的安裝速度。

共用前置指令(Before Script)

before_script:
- printenv
- npm install
  • 在所有 job 開始前執行以下動作:

    • 列出環境變數。
    • 執行 npm install 安裝依賴。

建構階段(Build)

build:
stage: build
script:
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week
  • 執行 npm run build 建構應用程式。
  • 把 dist/ 資料夾存為產物(artifacts),保存一週。

部署階段(Deploy - Pages)

pages:
stage: deploy
script:
- mkdir -p public
- cp -r dist/* public/
artifacts:
paths:
- public
only:
- release-test
  • 這是針對 GitLab Pages 的部署流程:

    • 建立 public 目錄。
    • 將 dist 中的檔案複製到 public。
    • 指定 public 為 GitLab Pages 需要的目錄。
  • 只在 release-test 分支時執行這個部署步驟。


gitlab ci 官方可用變數