havn 0.1.7

A fast configurable port scanner with reasonable defaults
name: Release CI
on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:

  #################################################
  ## Cross platform binary build for release page #
  #################################################

  cross_platform_build:
    strategy:
      matrix:
        platform:
          - target: x86_64-unknown-linux-musl
          - target: aarch64-unknown-linux-musl
          - target: arm-unknown-linux-musleabihf
          - target: x86_64-pc-windows-gnu
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      # Install stable rust, and associated tools
      - name: install rust
        uses: dtolnay/rust-toolchain@stable

      # Install cross-rs
      - name: install cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      # Set env's
      - name: set names
        run: |

              translate_platform() { case "$1" in x86_64-unknown-linux-musl) echo "linux_x86_64.tar.gz";; aarch64-unknown-linux-musl) echo "linux_aarch64.tar.gz";; arm-unknown-linux-musleabihf) echo "linux_armv6.tar.gz";; x86_64-pc-windows-gnu) echo "windows_x86_64.zip";; *) echo "Error: Unsupported platform $1"; exit 1;; esac; }
              target_platform="${{ matrix.platform.target }}"
              output_name=$(translate_platform "$target_platform")
              echo "TARGET_OUTPUT_NAME=${output_name}" >> $GITHUB_ENV
              echo "TARGET_PLATFORM=${target_platform}" >> $GITHUB_ENV
     
      # Build binary
      - name: build
        run: cross build --target "${TARGET_PLATFORM}" --release
              
      # Compress, rename, and move
      - name: compress
        run: |

            if [[ $TARGET_PLATFORM == *windows-gnu* ]]; then
               zip -j "./havn_${TARGET_OUTPUT_NAME}" target/${TARGET_PLATFORM}/release/havn.exe
            else
              tar -C "target/${TARGET_PLATFORM}/release" -czf "./havn_${TARGET_OUTPUT_NAME}" havn
            fi

      # Upload output for release page
      - name: Upload Artifacts
        uses: actions/upload-artifact@v3
        with:
          if-no-files-found: error
          name: ${{ env.TARGET_PLATFORM }}
          path: havn_${{ env.TARGET_OUTPUT_NAME }}
          retention-days: 1

  ###################
  ## Create release #
  ###################

  create_release:
    needs: [cross_platform_build]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup | Artifacts
        uses: actions/download-artifact@v3

      - name: Update Release
        uses: ncipollo/release-action@v1
        with:
          makeLatest: true
          name: ${{ github.ref_name }}
          tag: ${{ github.ref }}
          bodyFile: ".github/release-body.md"
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: |

            **/havn_*.zip
            **/havn_*.tar.gz

  #########################
  ## Publish to crates.io #
  #########################
  
  cargo_publish:
    needs: [create_release]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: publish to crates.io
        uses: katyo/publish-crates@v1
        with:
          registry-token: ${{ secrets.CRATES_IO_TOKEN }}


  #########################################
  ## Build images for Dockerhub & ghcr.io #
  #########################################

  image_build:
    needs: [cargo_publish]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Write release version to env
        run: |

          CURRENT_SEMVER=${GITHUB_REF_NAME#v}
          echo "CURRENT_SEMVER=$CURRENT_SEMVER" >> $GITHUB_ENV

      - uses: docker/setup-buildx-action@v3
        id: buildx
        with:
          install: true
      - name: Build for Dockerhub & ghcr.io
        run: |

          docker build --platform linux/arm/v6,linux/arm64,linux/amd64 \
          -t ${{ secrets.DOCKERHUB_USERNAME }}/havn:${{env.CURRENT_SEMVER}} \
          -t ${{ secrets.DOCKERHUB_USERNAME }}/havn:latest \
          -t ghcr.io/${{ github.repository }}:latest \
          -t ghcr.io/${{ github.repository }}:${{env.CURRENT_SEMVER}} \
          --provenance=false --sbom=false \
          --push \
          -f containerised/Dockerfile .