name: release

on:
  push:
    branches: [ master ]
  release:
    types: [ published ]

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
    - name: checkout
      uses: actions/checkout@v4
    - name: setup qemu
      uses: docker/setup-qemu-action@v3
    - name: setup docker buildx
      uses: docker/setup-buildx-action@v3
    - name: login docker hub
      uses: docker/login-action@v3
      with:
        registry: docker.io
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - name: login github container registry
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    - name: setup version
      if: github.event_name == 'release'
      run: |
        GIT_TAG=${{ github.event.release.tag_name }}
        echo "IMAGE_TAG=${GIT_TAG#v}" >> $GITHUB_ENV
    - name: build and push
      uses: docker/build-push-action@v5
      if: github.event_name == 'release'
      with:
        context: .
        provenance: false
        platforms: linux/amd64,linux/arm64,linux/arm/v7
        push: true
        tags: |
          docker.io/${{ secrets.DOCKERHUB_USERNAME }}/candy:${{ env.IMAGE_TAG }}
          docker.io/${{ secrets.DOCKERHUB_USERNAME }}/candy:latest
          ghcr.io/${{ github.actor }}/candy:${{ env.IMAGE_TAG }}
          ghcr.io/${{ github.actor }}/candy:latest

  windows:
    runs-on: windows-latest
    steps:
    - name: setup msys2
      uses: msys2/setup-msys2@v2
      with:
        msystem: MINGW64
        update: true
        install: >-
          mingw-w64-x86_64-cmake
          mingw-w64-x86_64-ninja
          mingw-w64-x86_64-gcc
          mingw-w64-x86_64-spdlog
          mingw-w64-x86_64-poco
    - name: checkout
      uses: actions/checkout@v4
    - name: cache
      uses: actions/cache@v4
      with:
        path: build
        key: ${{ hashFiles('CMakeLists.txt') }}
    - name: build
      shell: msys2 {0}
      run: |
        cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo && cmake --build build
        mkdir artifact
        cp candy.cfg artifact
        cp build/candy/wintun/bin/amd64/wintun.dll artifact
        scripts/search-deps.sh build/candy-cli/candy.exe artifact
        scripts/search-deps.sh build/candy-service/candy-service.exe artifact
    - name: set release package name
      shell: bash
      if: github.event_name == 'release'
      run: |
        GIT_TAG=${{ github.event.release.tag_name }}
        echo "PKGNAME=candy_${GIT_TAG#v}+windows_amd64" >> $GITHUB_ENV
    - name: upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: windows-${{ github.event.release.tag_name || github.sha }}
        path: artifact
    - name: zip release
      uses: thedoctor0/zip-release@0.7.5
      if: github.event_name == 'release'
      with:
        type: 'zip'
        filename: ${{ env.PKGNAME }}.zip
        directory: artifact
    - name: upload release
      uses: softprops/action-gh-release@v2
      if: github.event_name == 'release'
      with:
        files: artifact/${{ env.PKGNAME }}.zip
