name: Build and Deploy Staging Instance

on:
  push:
    branches: [ main ]

permissions:
  contents: read

jobs:
  build-deploy:
    if: github.repository == 'openstreetmap/id-tagging-schema'
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
        with:
          node-version-file: '.nvmrc'
      # install and build development version of id-tagging-schema
      - run: npm clean-install
      - run: npm run translations
        env:
          transifex_password: ${{secrets.TX_TOKEN}}
        if: env.transifex_password != null
      - run: npm run dist
      # install and build development version of iD using freshest version of presets and ELI
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: openstreetmap/id
          path: './iD'
      - run: npm clean-install
        working-directory: './iD'
      - run: npm install editor-layer-index
        working-directory: './iD'
      - run: mkdir dist/data
        working-directory: './iD'
      - run: npm run imagery
        working-directory: './iD'
      - run: npm run all
        working-directory: './iD'
      - run: npm run translations
        working-directory: './iD'
        env:
          transifex_password: ${{secrets.TX_TOKEN}}
        if: env.transifex_password != null
      - run: mkdir iD/dist/id-tagging-schema && mv dist iD/dist/id-tagging-schema/dist
      - run: npm run dist
        working-directory: './iD'
        env:
          ID_PRESETS_CDN_URL: './id-tagging-schema/'
      # deploy to netlify
      - name: Deploy to Netlify
        env:
          NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}}
          NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}}
        run: ./node_modules/.bin/netlify deploy --no-build --prod --dir=iD/dist
