name: CI
on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - run: npm install
      - run: npm run build
      - run: npm run doc
      - uses: actions/upload-artifact@v3
        with:
          name: webiste
          path: |
            website/build/**

      - name: Generate Contributors Images
        uses: jaywcjlove/github-action-contributors@main
        with:
          filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\])
          output: website/build/CONTRIBUTORS.svg
          avatarSize: 42

      - name: Create Tag
        id: create_tag
        uses: jaywcjlove/create-tag-action@main
        with:
          package-path: ./website/package.json

      - name: get tag version
        id: tag_version
        uses: jaywcjlove/changelog-generator@main

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          commit_message: ${{ github.event.head_commit.message }} ${{steps.tag_version.outputs.tag}} 
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./website/build

      - name: Generate Changelog
        id: changelog
        uses: jaywcjlove/changelog-generator@main
        with:
          head-ref: ${{steps.create_tag.outputs.version}}
          filter-author: (renovate-bot|Renovate Bot)
          filter: '[R|r]elease[d]\s+[v|V]\d(\.\d+){0,2}'

    outputs:
      version: ${{ steps.changelog.outputs.version }}
      create_tag_version: ${{ steps.create_tag.outputs.version }}
      create_tag_versionNumber: ${{ steps.create_tag.outputs.versionNumber }}
      tag: ${{ steps.changelog.outputs.tag }}
      successful: ${{steps.create_tag.outputs.successful }}
      gh-pages-short-hash: ${{ steps.changelog.outputs.gh-pages-short-hash }}

  docker: 
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/download-artifact@v3
        with:
          name: webiste
          path: website/build

      - run: echo "outputs.version - ${{ needs.build.outputs.version }}"
      - run: echo "outputs.create_tag_version - ${{ needs.build.outputs.create_tag_version }}"
      - run: echo "outputs.create_tag_versionNumber - ${{ needs.build.outputs.create_tag_versionNumber }}"
      - run: echo "outputs.tag - ${{ needs.build.outputs.tag }}"

      # Create Docker Image
      - name: Docker login
        run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}

      - name: Build wxmp image
        working-directory: website
        run: docker image build -t wxmp .

      - name: Tags & Push image (latest)
        run: |
          echo "outputs.tag - ${{ needs.build.outputs.version }}"
          docker tag wxmp ${{ secrets.DOCKER_USER }}/wxmp:latest
          docker push ${{ secrets.DOCKER_USER }}/wxmp:latest

      - name: Tags & Push image
        if: needs.build.outputs.successful
        run: |
          echo "outputs.tag - ${{ needs.build.outputs.version }}"
          docker tag wxmp ${{ secrets.DOCKER_USER }}/wxmp:${{needs.build.outputs.version}}
          docker push ${{ secrets.DOCKER_USER }}/wxmp:${{needs.build.outputs.version}}

      # Create Docker Image in GitHub
      - name: Login to GitHub registry
        run: echo ${{ github.token }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin

      - name: Build docker image
        working-directory: website
        run: docker build -t ghcr.io/jaywcjlove/wxmp:latest .

      - name: Publish to GitHub registry
        run: docker push ghcr.io/jaywcjlove/wxmp:latest

      - name: Tag docker image (beta) and publish to GitHub registry
        if: needs.build.outputs.successful
        run: |
            echo "version: v${{ needs.build.outputs.version }}"
            docker tag ghcr.io/jaywcjlove/wxmp:latest ghcr.io/jaywcjlove/wxmp:${{needs.build.outputs.version}}
            docker push ghcr.io/jaywcjlove/wxmp:${{needs.build.outputs.version}}

  build_windows: 
    needs: [build]
    runs-on: windows-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Install
        run: npm install --build-from-source

      - run: npm run hoist
      - run: npm run build
      # - run: npm run electron

      - uses: actions/download-artifact@v3
        with:
          name: webiste
          path: website/build

      - name: electron-builder install-app-deps
        working-directory: electron/app
        run: npm run deps

      - run: npm run build:app
      - working-directory: electron/app/dist
        run: ls -R

      - uses: actions/upload-artifact@v3
        with:
          name: wxmp-windows
          path: |
            electron\app\dist\*.exe

  build_macos:
    needs: [build]
    runs-on: macos-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - run: npm install
      - run: npm run hoist
      - run: npm run build

      - uses: actions/download-artifact@v3
        with:
          name: webiste
          path: website/build

      - name: electron-builder install-app-deps
        working-directory: electron/app
        run: npm run deps
      
      - run: npm run build:app
      - working-directory: electron/app/dist
        run: ls -R

      - uses: actions/upload-artifact@v3
        with:
          name: wxmp-macos
          path: |
            electron/app/dist/*.zip

  build_linux:
    needs: [build]
    runs-on: ubuntu-latest
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
      - run: npm install
      - run: npm run hoist
      - run: npm run build
      # - run: npm run electron

      - uses: actions/download-artifact@v3
        with:
          name: webiste
          path: website/build

      - name: electron-builder install-app-deps
        working-directory: electron/app
        run: npm run deps

      - run: npm run build:app
      - working-directory: electron/app/dist
        run: ls -R

      - uses: actions/upload-artifact@v3
        with:
          name: wxmp-linux
          path: |
            electron/app/dist/*.deb
            electron/app/dist/*.rpm


  create_release:
    needs: [build, build_windows, build_macos, build_linux]
    if: needs.build.outputs.successful
    runs-on: ubuntu-latest
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - uses: actions/download-artifact@v3
        with:
          name: wxmp-linux
          path: dist/linux

      - uses: actions/download-artifact@v3
        with:
          name: wxmp-macos
          path: dist/macos

      - uses: actions/download-artifact@v3
        with:
          name: wxmp-windows
          path: dist/windows

      - name: Display structure of downloaded files
        working-directory: dist
        run: ls -R

      - name: Generate Changelog
        id: changelog
        uses: jaywcjlove/changelog-generator@main
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          filter-author: (jaywcjlove|小弟调调™|dependabot\[bot\]|Renovate Bot)
          filter: (^[\s]+?[R|r]elease)|(^[R|r]elease)

      - name: Create Release
        uses: ncipollo/release-action@v1
        if: needs.build.outputs.successful
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          name: ${{ steps.changelog.outputs.tag }}
          tag: ${{ steps.changelog.outputs.tag }}
          artifacts: "dist/linux/*.rpm,dist/linux/*.deb,dist/macos/*.zip,dist/macos/*.dmg,dist/windows/*.exe"
          body: |
            Documentation ${{ steps.changelog.outputs.tag }}: https://raw.githack.com/jaywcjlove/wxmp/${{ steps.changelog.outputs.gh-pages-short-hash }}/index.html  
            Comparing Changes: ${{ steps.changelog.outputs.compareurl }}  

            ${{ steps.changelog.outputs.changelog }}

            ```bash
            docker pull wcjiang/wxmp:${{needs.build.outputs.create_tag_versionNumber}}
            ```

            ```bash
            docker run --name wxmp --rm -d -p 9666:3000 wcjiang/wxmp:${{ needs.build.outputs.create_tag_versionNumber }}
            # Or
            docker run --name wxmp -itd -p 9666:3000 wcjiang/wxmp:${{ needs.build.outputs.create_tag_versionNumber }}
            ```

            Visit the following URL in your browser

            ```bash
            http://localhost:9666/
            ```

  roll_back:
    if: failure()
    needs: [build, create_release]
    runs-on: ubuntu-latest
    timeout-minutes: 4
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}

      - run: echo "outputs.version - ${{ needs.build.outputs.create_tag_version }}"

      - uses: dev-drprasad/delete-tag-and-release@v0.2.0
        if: needs.build.outputs.successful
        with:
          delete_release: true
          repo: jaywcjlove/wxmp
          tag_name: '${{ needs.build.outputs.create_tag_version }}'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
