name: Build + Publish

on:
  push:
    branches:
      - scarlett

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - uses: actions/checkout@v7

    - name: Setup Node.js environment
      uses: actions/setup-node@v7
      with:
        node-version: 26.x

    - name: Set Build Variables
      run: |
        echo "REL_VERSION=3.0.0-alpha.$GITHUB_RUN_NUMBER" >> $GITHUB_ENV

    - name: Set Version
      run: |
        yq -iP '.version = strenv(REL_VERSION)' backend/package.json -o json
        yq -iP '.version = strenv(REL_VERSION)' frontend/package.json -o json

    - name: Upload translations source file
      uses: crowdin/github-action@v2
      env:
        CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
        CROWDIN_BASE_PATH: ${{ github.workspace }}
      with:
        command: 'upload'
        command_args: '--config dev/crowdin.yml'

    - name: Build Assets
      working-directory: frontend
      run: |
        npm ci
        npm run build

    - name: Build Blocks
      working-directory: blocks
      run: |
        npm ci
        npm run build

    - name: Login to GitHub Container Registry
      uses: docker/login-action@v4
      with:
        registry: ghcr.io
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v4

    - name: Build and push Docker images
      uses: docker/build-push-action@v7
      with:
        context: .
        file: dev/build/Dockerfile
        push: true
        # platforms: linux/amd64
        platforms: linux/amd64,linux/arm64
        tags: |
          ghcr.io/requarks/wiki:3.0.0-alpha
          ghcr.io/requarks/wiki:${{ env.REL_VERSION }}

    - name: Prepare build archive
      run: |
        mkdir -p _dist/blocks
        cp -R assets _dist/assets
        cp -R blocks/compiled _dist/blocks/compiled
        cp -R backend _dist/backend
        cp LICENSE _dist/LICENSE
        cp config.sample.yml _dist/config.sample.yml
        cd _dist/backend
        npm ci --omit=dev
        cd -
        find ./_dist/ -printf "%P\n" | tar -czf wiki-js.tar.gz --no-recursion -C ./_dist/ -T -

    - name: Upload Build Artifact
      uses: actions/upload-artifact@v7
      with:
        name: build
        path: wiki-js.tar.gz


