# UFPE Design System — GitLab CI/CD
#
# Pipelines:
#   - on every push: build tokens + docs + plugin (validação)
#   - on push to master: deploy docs e plugin para Cloudflare Pages
#   - on tag v*: release com snapshot Penpot + publish library
#
# Variáveis (CI/CD Settings → Variables):
#   CLOUDFLARE_API_TOKEN  (masked, protected)
#   CLOUDFLARE_ACCOUNT_ID (masked)
#   PENPOT_API            (URL pública do Penpot UFPE, p/ jobs penpot:*)
#   PENPOT_TOKEN          (masked, protected — só para tag/release)

default:
  image: node:22-alpine
  cache:
    key:
      files: [pnpm-lock.yaml]
    paths: [.pnpm-store/, node_modules/]
  before_script:
    - corepack enable
    - corepack prepare pnpm@9.0.0 --activate
    - pnpm config set store-dir .pnpm-store
    - pnpm install --frozen-lockfile

stages:
  - build
  - deploy
  - release

# ─────────────── BUILD ───────────────

build:tokens:
  stage: build
  script:
    - pnpm --filter @ufpe/tokens build
    - test -f packages/tokens/src/tokens.css
    - test -f packages/tokens/src/tokens.js
    - test -f packages/tokens/src/tokens.d.ts
    - test -f packages/tokens/src/tokens.penpot.json
  artifacts:
    paths: [packages/tokens/src/]
    expire_in: 1 week

build:docs:
  stage: build
  needs: [build:tokens]
  script:
    - pnpm --filter @ufpe/docs build
  artifacts:
    paths: [apps/docs/dist/]
    expire_in: 1 week

build:plugin:
  stage: build
  needs: [build:tokens]
  script:
    - pnpm --filter @ufpe/penpot-plugin build
  artifacts:
    paths: [packages/penpot-plugin/dist/]
    expire_in: 1 week

# ─────────────── DEPLOY (Cloudflare Pages) ───────────────

.cloudflare-deploy: &cloudflare-deploy
  stage: deploy
  image: node:22-alpine
  before_script:
    - npm install -g wrangler@4
  rules:
    - if: $CI_COMMIT_BRANCH == "master"

deploy:docs:
  <<: *cloudflare-deploy
  needs: [build:docs]
  environment:
    name: docs/production
    url: https://ufpe-design-system.pages.dev
  script:
    - wrangler pages deploy apps/docs/dist
        --project-name=ufpe-design-system
        --branch=master
        --commit-hash=$CI_COMMIT_SHA
        --commit-message="$CI_COMMIT_TITLE"

deploy:plugin:
  <<: *cloudflare-deploy
  needs: [build:plugin]
  environment:
    name: plugin/production
    url: https://ufpe-design-system-plugin.pages.dev
  script:
    - wrangler pages deploy packages/penpot-plugin/dist
        --project-name=ufpe-design-system-plugin
        --branch=master
        --commit-hash=$CI_COMMIT_SHA
        --commit-message="$CI_COMMIT_TITLE"

# ─────────────── RELEASE (Penpot library publish + tag artifact) ───────────────

release:penpot:
  stage: release
  needs: [build:tokens]
  rules:
    - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+/
  script:
    - node penpot/scripts/publish-library.js

release:artifact:
  stage: release
  needs: [build:tokens, build:docs, build:plugin]
  rules:
    - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+/
  script:
    - mkdir -p release
    - cp packages/tokens/src/tokens.penpot.json release/
    - cp -r apps/docs/dist release/docs
    - cp -r packages/penpot-plugin/dist release/penpot-plugin
    - tar czf release/ufpe-design-system-${CI_COMMIT_TAG}.tgz -C release .
  artifacts:
    paths: [release/]
    expire_in: never
