name: CI
on:
  push:
    branches:
      - main

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

      - run: npm install
      - run: npm run build

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

      - name: Modify CONTRIBUTING.md
        uses: jaywcjlove/github-action-modify-file-content@main
        with:
          path: CONTRIBUTING.md
          body: |
            ${{steps.contributors.outputs.htmlTable}}

      - name: Modify README.md
        uses: jaywcjlove/github-action-modify-file-content@main
        with:
          path: README.md
          body: |
            ${{steps.contributors.outputs.htmlList}}

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

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          commit_message: ${{ github.event.head_commit.message }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./dist

      - name: Generate Changelog
        id: changelog
        uses: jaywcjlove/changelog-generator@main
        with:
          filter-author: (小弟调调™)
          filter: (^[\s]+?[R|r]elease)|(^[R|r]elease)


      - name: Create Release
        uses: ncipollo/release-action@v1
        if: steps.create_tag.outputs.successful
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          name: ${{ steps.create_tag.outputs.version }}
          tag: ${{ steps.create_tag.outputs.version }}
          body: |
            Documentation ${{ needs.build.outputs.create_tag_version }}: https://raw.githack.com/jaywcjlove/reference/${{ steps.changelog.outputs.gh-pages-short-hash }}/index.html  
            Comparing Changes: ${{ steps.changelog.outputs.compareurl }}  
            
            ${{ steps.changelog.outputs.changelog }}

            ## Docker
            [![Docker Image Version (latest by date)](https://img.shields.io/docker/v/wcjiang/reference)](https://hub.docker.com/r/wcjiang/reference) [![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/wcjiang/reference)](https://hub.docker.com/r/wcjiang/reference) [![Docker Pulls](https://img.shields.io/docker/pulls/wcjiang/reference)](https://hub.docker.com/r/wcjiang/reference)

            轻松通过 `docker` 部署 `Quick Reference` 网站。
            
            ```bash
            docker pull wcjiang/reference
            ```
            
            ```bash
            docker run --name reference --rm -d -p 9667:3000 wcjiang/reference:latest
            # Or
            docker run --name reference -itd -p 9667:3000 wcjiang/reference:latest
            ```
            
            在浏览器中访问以下 URL
            
            ```bash
            http://localhost:9667/
            ```

      # 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
        run: docker build -t ghcr.io/jaywcjlove/reference:latest .

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

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

      # Create Docker Image
      - name: Docker login
        run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}
        
      - name: Build reference image
        run: docker image build -t reference .

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

      - name: Tags & Push image
        if: steps.create_tag.outputs.successful
        run: |
          echo "outputs.tag - ${{ steps.changelog.outputs.version }}"
          docker tag reference ${{ secrets.DOCKER_USER }}/reference:${{steps.changelog.outputs.version}}
          docker push ${{ secrets.DOCKER_USER }}/reference:${{steps.changelog.outputs.version}}