# miniooni checks whether we can build the research client miniooni
# and publishes the linux binaries as artifacts and, if we're building
# a tag, into the current release. We don't publish artifacts for
# Windows and macOS because they're not signed. For releases, we have
# a script that we'll later use to sign unsigned binaries.
name: miniooni
on:
  push:
    branches:
      - "master"
      - "release/**"
    tags:
      - "v*"

jobs:
  build_and_publish:
    runs-on: ubuntu-20.04
    steps:

      - uses: actions/setup-go@v1
        with:
          go-version: "1.17.6"

      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - run: |
          echo -n $PSIPHON_CONFIG_KEY > ./internal/engine/psiphon-config.key
          echo $PSIPHON_CONFIG_JSON_AGE_BASE64 | base64 -d > ./internal/engine/psiphon-config.json.age
        env:
          PSIPHON_CONFIG_KEY: ${{ secrets.PSIPHON_CONFIG_KEY }}
          PSIPHON_CONFIG_JSON_AGE_BASE64: ${{ secrets.PSIPHON_CONFIG_JSON_AGE_BASE64 }}

      - run: ./mk ./CLI/miniooni
      - run: ./E2E/miniooni.bash ./CLI/miniooni-linux-amd64

      - uses: actions/upload-artifact@v1
        with:
          name: miniooni-linux-386
          path: ./CLI/miniooni-linux-386
      - uses: actions/upload-artifact@v1
        with:
          name: miniooni-linux-amd64
          path: ./CLI/miniooni-linux-amd64
      - uses: actions/upload-artifact@v1
        with:
          name: miniooni-linux-armv7
          path: ./CLI/miniooni-linux-armv7
      - uses: actions/upload-artifact@v1
        with:
          name: miniooni-linux-arm64
          path: ./CLI/miniooni-linux-arm64

      - run: |
          tag=$(echo $GITHUB_REF | sed 's|refs/tags/||g')
          gh release create -p $tag --target $GITHUB_SHA || true
          gh release upload $tag --clobber ./CLI/miniooni-darwin-amd64 \
                                           ./CLI/miniooni-darwin-arm64 \
                                           ./CLI/miniooni-linux-386 \
                                           ./CLI/miniooni-linux-amd64 \
                                           ./CLI/miniooni-linux-armv7 \
                                           ./CLI/miniooni-linux-arm64 \
                                           ./CLI/miniooni-windows-386.exe \
                                           ./CLI/miniooni-windows-amd64.exe
        if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
