name: CD

on:
  release:
    types: [published]

jobs:
  build_for_linux:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v2
      - name: Build
        run: |
          docker pull clux/muslrust:stable
          docker run -v "$(pwd)":/volume --rm -t clux/muslrust:stable cargo build --release
      - name: Archive
        run: |
          mkdir notion2pandoc-x86-64-linux
          cp target/x86_64-unknown-linux-musl/release/notion2pandoc notion2pandoc-x86-64-linux
          tar czf notion2pandoc-x86-64-linux.tar.gz notion2pandoc-x86-64-linux
      - name: Upload Linux executables as artifacts
        uses: actions/upload-artifact@v2
        with:
          name: ${{ github.sha }}-linux-artifacts
          path: notion2pandoc-x86-64-linux.*

  release_if_tag_exits:
    needs: build_for_linux
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v2
      - name: Download the artifact
        uses: actions/download-artifact@v2
        with:
          name: ${{ github.sha }}-linux-artifacts
      - name: Release
        run: |
          gh release upload ${{ github.event.release.tag_name }} \
          notion2pandoc-x86-64-linux.tar.gz
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
