# This is a basic workflow to help you get started with Actions
name: addDocsToS3BucketAWS
# Controls when the action will run.
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: ['main']
    paths:
      - "i18n/zh/**"
      - "variables/**"
      - "blog/**"
      - "src/**"
      - "static/**"
      - "versioned_sidebars/**"
      - "skills/**"
      - "scripts/sync-skill.mjs"
      - "sidebars.ts"
      - "docusaurus.config.ts"
      - ".npmrc"
      - "package.json"
      - "pnpm-lock.yaml"
      - "versions.json"
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    if: ${{ github.repository == 'GreptimeTeam/docs' }}
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [22.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
      - uses: actions/checkout@v3
      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8.6.0
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'pnpm'
      - run: pnpm install
      - run: pnpm build
        env:
          BIEL_PROJECT_ID: ${{ secrets.BIEL_PROJECT_ID }}
          DOC_LANG: zh

      - name: Check the built files
        shell: bash
        run: |
          ls -lhR ./build

      - name: Install s5cmd
        shell: bash
        run: |
          wget https://github.com/peak/s5cmd/releases/download/v2.3.0/s5cmd_2.3.0_Linux-64bit.tar.gz
          tar -xzf s5cmd_2.3.0_Linux-64bit.tar.gz
          sudo mv s5cmd /usr/local/bin/
          sudo chmod +x /usr/local/bin/s5cmd

      - name: Upload to S3
        run: |
          s5cmd --numworkers 1024 sync ./build/ s3://${{ secrets.DOCUMENT_BUCKET }}
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: cn-northwest-1

      - name: Create cloudfront invalidation
        run: |
          aws cloudfront create-invalidation --distribution-id ${{ secrets.DOCS_DISTRIBUTION_ID }} --paths "/*"
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: cn-northwest-1
