on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0.0

name: Create Release

jobs:
  build:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set RELEASE_VERSION environment variable
        run: echo "RELEASE_VERSION=${GITHUB_REF:10}" >> $GITHUB_ENV
      - uses: actions/setup-node@v1
        with:
          node-version: '16.x'
      - run: npm ci
      - run: npm run build
      - run: npm run docs
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          body: |
            See [CHANGELOG.md](https://github.com/symbioquine/ol-grid/blob/${{ env.RELEASE_VERSION }}/CHANGELOG.md) for release notes.
          draft: false
          prerelease: false
      - name: Upload 'ol-side-panel.js' Release Asset
        id: upload-ol-side-panel-js-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`.
          # See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./dist/ol-side-panel.js
          asset_name: ol-side-panel.js
          asset_content_type: application/javascript
      - name: Upload 'ol-side-panel.css' Release Asset
        id: upload-ol-side-panel-css-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./dist/ol-side-panel.css
          asset_name: ol-side-panel.css
          asset_content_type: application/javascript

  publish-npm:
    name: Publish to NPM
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: '16.x'
          registry-url: https://registry.npmjs.org/
      - run: npm ci
      - run: npm run build
      - run: npm run docs
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

  publish-to-gh-pages:
    name: Publish to Github Pages
    needs: build
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Set RELEASE_VERSION environment variable
      run: echo "RELEASE_VERSION=${GITHUB_REF:10}" >> $GITHUB_ENV
    - uses: actions/setup-node@v1
      with:
        node-version: '16.x'
    - run: npm ci
    - run: npm run build
    - run: npm run docs
    - name: Cleanup and prepare pages
      run: |
        rm -rf ./gh-pages-staging
        mkdir -p ./gh-pages-staging/main/
        cp ./dist/* ./gh-pages-staging/main/
        cp -r ./docs ./gh-pages-staging/main/
    - name: Deploy to gh-pages 'latest' version
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: ./gh-pages-staging/main
        destination_dir: ./latest
        keep_files: true
    - name: Deploy gh-pages version-specific
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: ./gh-pages-staging/main
        destination_dir: ./${{ env.RELEASE_VERSION }}
        keep_files: true
