name: CD

on:
  release:
    types:
    - created

jobs:
  linux_windows:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout the repository
      uses: actions/checkout@v2

    - name: Install Linux and Windows Cross Compilers
      run: sudo apt-get install --yes --no-install-recommends musl-tools gcc-mingw-w64-x86-64-win32

    - name: Install rustup targets
      run: rustup target add x86_64-unknown-linux-musl x86_64-pc-windows-gnu

    - name: Build the executable
      run: cargo build --release --target x86_64-unknown-linux-musl --target x86_64-pc-windows-gnu

    - name: Tar x86_64 binary
      run: tar -czvf textpod-gnu-linux-x86_64.tar.gz -C target/x86_64-unknown-linux-musl/release textpod

    - name: Zip windows binary
      run: zip -j textpod-windows.zip target/x86_64-pc-windows-gnu/release/textpod.exe

    - name: Generate SHA256 checksums
      run: |
        shasum -a 256 textpod-gnu-linux-x86_64.tar.gz > textpod-gnu-linux-x86_64.tar.gz.sha256
        shasum -a 256 textpod-windows.zip > textpod-windows.zip.sha256

    - name: Upload release binaries
      uses: alexellis/upload-assets@0.4.0
      env:
        GITHUB_TOKEN: ${{ github.token }}
      with:
        asset_paths: '["textpod-gnu-linux-x86_64.tar.gz", "textpod-windows.zip", "textpod-gnu-linux-x86_64.tar.gz.sha256", "textpod-windows.zip.sha256"]'

  macos:
    runs-on: macos-latest
    steps:
    - name: Checkout the repository
      uses: actions/checkout@v2

    - name: Install rustup targets
      run: rustup target add x86_64-apple-darwin aarch64-apple-darwin

    - name: Build the executable
      run: cargo build --release --target=x86_64-apple-darwin --target=aarch64-apple-darwin

    - name: Zip x86_64 binary
      run: tar -czvf textpod-macos-x86_64.tar.gz -C target/x86_64-apple-darwin/release textpod

    - name: Zip arm64 binary
      run: tar -czvf textpod-macos-aarch64.tar.gz -C target/aarch64-apple-darwin/release textpod

    - name: Generate SHA256 checksums
      run: |
        shasum -a 256 textpod-macos-x86_64.tar.gz > textpod-macos-x86_64.tar.gz.sha256
        shasum -a 256 textpod-macos-aarch64.tar.gz > textpod-macos-aarch64.tar.gz.sha256

    - name: Upload release binaries
      uses: alexellis/upload-assets@0.4.0
      env:
        GITHUB_TOKEN: ${{ github.token }}
      with:
        asset_paths: '["textpod-macos-x86_64.tar.gz", "textpod-macos-aarch64.tar.gz", "textpod-macos-x86_64.tar.gz.sha256", "textpod-macos-aarch64.tar.gz.sha256"]'

  crates:
    runs-on: ubuntu-latest
    needs: [linux_windows, macos]
    steps:
    - uses: actions/checkout@v3
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true
    - uses: katyo/publish-crates@v2
      with:
        registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  docker:
    runs-on: ubuntu-latest
    needs: crates
    steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v6
        with:
          platforms: linux/amd64,linux/arm64
          push: true
          tags: freetonik/textpod:latest
