name: Release

on:
  push:
    branches:
      - main
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    if: startsWith(github.ref, 'refs/tags/v')
    name: Build ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            artifact_name: apimimic
            asset_name: apimimic-linux
          - os: windows-latest
            artifact_name: apimimic.exe
            asset_name: apimimic-windows.exe
          - os: macos-latest
            artifact_name: apimimic
            asset_name: apimimic-macos

    steps:
    - uses: actions/checkout@v2
    
    - name: Install Rust
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        
    - name: Build
      run: cargo build --release

    - name: Create Release
      id: create_release
      uses: softprops/action-gh-release@v1
      with:
        draft: false
        prerelease: false
        token: ${{ secrets.GITHUB_TOKEN }}
        name: Release ${{ github.ref_name }}
        tag_name: ${{ github.ref_name }}
        generate_release_notes: true
      
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: target/release/${{ matrix.artifact_name }}
        asset_name: ${{ matrix.asset_name }}
        asset_content_type: application/octet-stream 
