name: Publish Packages

on:
  push:
    branches: [ main ]

jobs:
  build:
    if: contains(github.event.head_commit.message, 'chore(release)') == false
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      run: yarn build

  lint:
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build

    - name: Lint
      run: yarn lint

  test:
    runs-on: ubuntu-latest

    needs: build

    steps:
    - uses: actions/checkout@v3

    - name: Cache build
      id: cache-build
      uses: actions/cache@v3
      with:
        path: |
          packages/**/dist
        key: ${{ runner.os }}-build-${{ github.sha }}

    - name: Set up Node
      uses: actions/setup-node@v3
      with:
        registry-url: 'https://registry.npmjs.org'
        node-version-file: '.nvmrc'

    - name: Build
      if: steps.cache-build.outputs.cache-hit != 'true'
      run: yarn build

    - name: Test
      run: yarn test

  e2e:
    needs: build
    name: E2E
    uses: standardnotes/server/.github/workflows/common-e2e.yml@main
    secrets: inherit

  publish-self-hosting:
    needs: [ test, lint, e2e ]
    name: Publish Self Hosting Docker Image
    uses: standardnotes/server/.github/workflows/common-self-hosting.yml@main
    secrets: inherit

  publish-services:
    needs: [ test, lint, e2e ]

    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.CI_PAT_TOKEN }}
          fetch-depth: 0

      - name: Cache build
        id: cache-build
        uses: actions/cache@v3
        with:
          path: |
            packages/**/dist
          key: ${{ runner.os }}-build-${{ github.sha }}

      - name: Setup git config
        run: |
          git config --global user.name "standardci"
          git config --global user.email "ci@standardnotes.com"

      - name: Import GPG key
        uses: crazy-max/ghaction-import-gpg@v5
        with:
          gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
          passphrase: ${{ secrets.PASSPHRASE }}
          git_user_signingkey: true
          git_commit_gpgsign: true

      - name: Set up Node
        uses: actions/setup-node@v3
        with:
          registry-url: 'https://registry.npmjs.org'
          node-version-file: '.nvmrc'

      - name: Build
        if: steps.cache-build.outputs.cache-hit != 'true'
        run: yarn build

      - name: Bump version
        run: yarn release

      - name: Publish
        run: yarn publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.CI_NPM_TOKEN }}
