name: Release

on:
  push:
    tags:
      - 'v*.*.*'

# grant write permission to GITHUB_TOKEN for goreleaser
permissions:
  contents: write

env:
  ZITI_DEB_PROD_REPO: ${{ vars.ZITI_DEB_PROD_REPO || 'zitipax-openziti-deb-stable' }}
  ZITI_RPM_PROD_REPO: ${{ vars.ZITI_RPM_PROD_REPO || 'zitipax-openziti-rpm-stable' }}
  JFROG_CLI_VERSION: ${{ vars.JFROG_CLI_VERSION || '2.50.4' }}

jobs:
  build-linux-amd64:
    runs-on: ubuntu-20.04
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-multilib g++-multilib

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

      - run: git fetch --force --tags

      - uses: actions/setup-go@v5
        with:
          go-version-file: ./go.mod
          cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: '~> v2'
          args: release --skip=publish --config .goreleaser-linux-amd64.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v4
        with:
          name: release-builds-linux-amd64
          path: ./dist/*.gz

      - name: Configure jFrog CLI
        uses: jfrog/setup-jfrog-cli@v4
        with:
          version: ${{ env.JFROG_CLI_VERSION }}
        env:
          JF_ENV_1: ${{ secrets.ZITI_ARTIFACTORY_CLI_CONFIG_PACKAGE_UPLOAD }}

      - name: Upload RPM to Artifactory release repo
        shell: bash
        run: |
          for RPM in ./dist/*.rpm; do
            echo "INFO: Uploading $RPM"
            jf rt upload --recursive=false --flat=true \
              "$RPM" \
              ${{ env.ZITI_RPM_PROD_REPO }}/redhat/x86_64/
          done

      - name: Upload DEB to Artifactory release repo
        shell: bash
        run: |
          for DEB in ./dist/*.deb; do
            echo "INFO: Uploading $DEB"
            jf rt upload --recursive=false --flat=true \
            --deb=debian/main/amd64 \
            "$DEB" \
            ${{ env.ZITI_DEB_PROD_REPO }}/pool/zrok/amd64/
          done

  build-linux-arm64:
    runs-on: ubuntu-20.04
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-aarch64-linux-gnu

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

      - run: git fetch --force --tags

      - uses: actions/setup-go@v5
        with:
          go-version-file: ./go.mod
          cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: '~> v2'
          args: release --skip=publish --config .goreleaser-linux-arm64.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v4
        with:
          name: release-builds-linux-arm64
          path: ./dist/*.gz

      - name: Configure jFrog CLI
        uses: jfrog/setup-jfrog-cli@v4
        with:
          version: ${{ env.JFROG_CLI_VERSION }}
        env:
          JF_ENV_1: ${{ secrets.ZITI_ARTIFACTORY_CLI_CONFIG_PACKAGE_UPLOAD }}

      - name: Upload RPM to Artifactory release repo
        shell: bash
        run: |
          for RPM in ./dist/*.rpm; do
            echo "INFO: Uploading $RPM"
            jf rt upload --recursive=false --flat=true \
              "$RPM" \
              ${{ env.ZITI_RPM_PROD_REPO }}/redhat/aarch64/
          done

      - name: Upload DEB to Artifactory release repo
        shell: bash
        run: |
          for DEB in ./dist/*.deb; do
            echo "INFO: Uploading $DEB"
            jf rt upload --recursive=false --flat=true \
            --deb=debian/main/arm64 \
            "$DEB" \
            ${{ env.ZITI_DEB_PROD_REPO }}/pool/zrok/arm64/
          done

  build-linux-arm:
    runs-on: ubuntu-20.04
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-arm-linux-gnueabihf

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

      - run: git fetch --force --tags

      - uses: actions/setup-go@v5
        with:
          go-version-file: ./go.mod
          cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: '~> v2'
          args: release --skip=publish --config .goreleaser-linux-armhf.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v4
        with:
          name: release-builds-linux-arm
          path: ./dist/*.gz

      - name: Configure jFrog CLI
        uses: jfrog/setup-jfrog-cli@v4
        with:
          version: ${{ env.JFROG_CLI_VERSION }}
        env:
          JF_ENV_1: ${{ secrets.ZITI_ARTIFACTORY_CLI_CONFIG_PACKAGE_UPLOAD }}

      - name: Upload RPM to Artifactory release repo
        shell: bash
        run: |
          for RPM in ./dist/*.rpm; do
            echo "INFO: Uploading $RPM"
            jf rt upload --recursive=false --flat=true \
              "$RPM" \
              ${{ env.ZITI_RPM_PROD_REPO }}/redhat/armv7/
          done

      - name: Upload DEB to Artifactory release repo
        shell: bash
        run: |
          for DEB in ./dist/*.deb; do
            echo "INFO: Uploading $DEB"
            jf rt upload --recursive=false --flat=true \
            --deb=debian/main/armv7 \
            "$DEB" \
            ${{ env.ZITI_DEB_PROD_REPO }}/pool/zrok/armv7/
          done

  build-darwin:
    if: github.repository_owner == 'openziti' || vars.ZROK_SKIP_MACOS_BUILD == 'false'
    runs-on: macos-latest
    steps:
      - name: import distribution cert
        uses: apple-actions/import-codesign-certs@v3
        with:
          p12-file-base64: ${{ secrets.APPLE_DEVELOPER_CERTIFICATE_P12_BASE64 }}
          p12-password: ${{ secrets.APPLE_DEVELOPER_CERTIFICATE_PASSWORD }}

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

      - run: git fetch --force --tags

      - uses: actions/setup-go@v5
        with:
          go-version-file: ./go.mod
          cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: '~> v2'
          args: release --skip=publish --config .goreleaser-darwin.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          AC_USERNAME: ${{ secrets.AC_USERNAME }}
          AC_PASSWORD: ${{ secrets.AC_PASSWORD }}

      - uses: actions/upload-artifact@v4
        with:
          name: release-builds-darwin
          path: ./dist/*.gz

  build-windows:
    if: github.repository_owner == 'openziti' || vars.ZROK_SKIP_WINDOWS_BUILD == 'false'
    runs-on: ubuntu-latest
    steps:
      - run: sudo apt update
      - run: sudo apt-get install gcc-mingw-w64-x86-64

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

      - run: git fetch --force --tags

      - uses: actions/setup-go@v5
        with:
          go-version-file: ./go.mod
          cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: 18.x

      - run: npm install
        working-directory: ui

      - run: npm run build
        working-directory: ui
        env:
          CI: "true"

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: '~> v2'
          args: release --skip=publish --config .goreleaser-windows.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/upload-artifact@v4
        with:
          name: release-builds-windows
          path: ./dist/*.gz

  publish-release:
    # allow skipped but not failed
    if: ${{
          !cancelled()
          && (needs.build-linux-amd64.result == 'success')
          && (needs.build-linux-arm.result   == 'success')
          && (needs.build-linux-arm64.result == 'success')
          && (needs.build-darwin.result      == 'success' || needs.build-darwin.result      == 'skipped')
          && (needs.build-windows.result     == 'success' || needs.build-windows.result     == 'skipped')
      }}
    needs:
      - build-linux-amd64
      - build-linux-arm
      - build-linux-arm64
      - build-darwin
      - build-windows
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - run: |
          mkdir -p ./automated-release-build

      - uses: actions/download-artifact@v4
        with:
          path: ./automated-release-build
          merge-multiple: true
          pattern: release-builds-*

      - run: |
          ls -lAR ./automated-release-build/
          shasum ./automated-release-build/* > ./automated-release-build/checksums.txt

      - uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          version: '~> v2'
          args: release --config .goreleaser-release.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
