name: Publish Release

on:
  workflow_dispatch:

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions: write-all

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Variable Gathering
        id: gathervars
        run: |
          NOT_PREVIOUSLY_PUBLISHED=0
          # get a current BUILD_DATE
          VERSION=$(jq -r '.version' ./package.json)
          echo "::set-output name=BUILD_DATE::$(date +%Y%m%d-%H%M%S)"
          echo "::set-output name=VERSION::$VERSION"

          # setting tags
          if echo "$VERSION" | grep -q "beta"; then
          
            TAGS="ghcr.io/${{ github.repository }}:beta, ghcr.io/${{ github.repository }}:$VERSION, ghcr.io/${{ github.repository }}:latest"
            PRIMARY_TAG=latest
          else
            TAGS="ghcr.io/${{ github.repository }}:release, ghcr.io/${{ github.repository }}:latest, ghcr.io/${{ github.repository }}:$VERSION"
            PRIMARY_TAG=latest
          fi
          echo "::set-output name=TAG::$TAGS"
          echo "::set-output name=PRIMARY_TAG::$PRIMARY_TAG"
          # check if version has already been published
          $(docker manifest inspect ghcr.io/${{ github.repository }}:$VERSION > /dev/null) || NOT_PREVIOUSLY_PUBLISHED=1
          echo "::set-output name=NOT_PREVIOUSLY_PUBLISHED::$NOT_PREVIOUSLY_PUBLISHED"

      - name: Log in to the Container registry
        uses: docker/login-action@v1
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker Image
        uses: docker/build-push-action@v2
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        with:
          build-args: |
            BUILD_DATE=${{ steps.gathervars.outputs.BUILD_DATE }}
            VERSION=${{ steps.gathervars.outputs.VERSION }}
          context: ./docker/production
          tags: |
            ${{ steps.gathervars.outputs.TAG }}
          push: true

      - name: Extract build out of docker image
        uses: shrink/actions-docker-extract@v1
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        id: extract
        with:
          image: ghcr.io/${{ github.repository }}:${{ steps.gathervars.outputs.PRIMARY_TAG }}
          path: web
          
      - name: create release asset
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        run: |
          cd "${{ steps.extract.outputs.destination }}"
          7z a headscale-ui.zip web

      - name: Create Draft Release
        id: create_release
        uses: actions/create-release@v1
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.gathervars.outputs.VERSION }}
          release_name: headscale-ui
          draft: true
          prerelease: false
        
      - name: upload asset to releases  
        uses: actions/upload-release-asset@v1.0.1
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ${{ steps.extract.outputs.destination }}/headscale-ui.zip
          asset_name: headscale-ui.zip
          asset_content_type: application/zip

      - name: publish release
        uses: eregon/publish-release@v1
        if: ${{ steps.gathervars.outputs.NOT_PREVIOUSLY_PUBLISHED != 0 }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          release_id: ${{ steps.create_release.outputs.id }}