{% render "scripts/helper/templates/copyright-liquid.yml" %}

{% if platforms == "all" -%}
  {% assign platforms = "linux-x64,linux-arm64,linux-arm,darwin-x64,darwin-arm64,win32-x64" -%}
{% endif -%}
{% assign platforms = platforms | split: "," -%}

name: 'Publish the xPack {{ APP_NAME }} release'

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'The semver of the release'
        required: false
        default: 'current'

jobs:
  create-release:
    name: 'Create the pre-release'
    runs-on: ubuntu-latest
    steps:
      - name: 'Machine'
        run: uname -a
      - name: 'Checkout'
        uses: actions/checkout@v2
        with:
          fetch-depth: 3
          submodules: true
      - name: 'Create body'
        run: |
          npm install
          cat scripts/templates/body-github-release-liquid.md | npm run liquidjs --silent -- '{ "RELEASE_VERSION": "{% raw %}${{ github.event.inputs.version }}{% endraw %}" }' >.github/workflows/body-github-release.md
          cat .github/workflows/body-github-release.md
      - name: 'Publish release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: false
          bodyFile: '.github/workflows/body-github-release.md'
          commit: 'xpack-develop'
          draft: true
          name: 'xPack {{ APP_NAME }} v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: false
          repo: '{{ APP_LC_NAME }}-xpack'
          tag: 'v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}

{% for platform in platforms -%}
  {% if platform == "linux-x64" or platform == "win32-x64" or platform == "linux-ia32" or platform == "win32-ia32" %}
  linux-x64:
    needs: create-release
    name: 'Linux Intel - {{ APP_NAME }} {% raw %}${{ github.event.inputs.version }}{% endraw %} publish release'
    runs-on: [self-hosted, linux, x64]
    steps:
      - name: 'Machine'
        run: uname -a
      - name: 'Publish release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: '~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*'
          commit: 'xpack-develop'
          draft: true
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: false
          repo: '{{ APP_LC_NAME }}-xpack'
          tag: 'v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}
      - name: 'Show SHA'
        run: cat ~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*.sha
{% break -%}
{% endif -%}
{% endfor %}

{% for platform in platforms -%}
{% if platform == "linux-arm" %}
  linux-arm:
    needs: create-release
    name: 'Linux Arm 32 - {{ APP_NAME }} {% raw %}${{ github.event.inputs.version }}{% endraw %} publish release'
    runs-on: [self-hosted, linux, arm, xbbla32]
    steps:
      - name: 'Machine'
        run: uname -a
      - name: 'Publish release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: '~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*'
          commit: 'xpack-develop'
          draft: true
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: false
          repo: '{{ APP_LC_NAME }}-xpack'
          tag: 'v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}
      - name: 'Show SHA'
        run: cat ~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*.sha
{% break -%}
{% endif -%}
{% endfor %}

{% for platform in platforms -%}
{% if platform == "linux-arm64" %}
  linux-arm64:
    needs: create-release
    name: 'Linux Arm 64 - {{ APP_NAME }} {% raw %}${{ github.event.inputs.version }}{% endraw %} publish release'
    runs-on: [self-hosted, linux, arm64, xbbla64]
    steps:
      - name: 'Machine'
        run: uname -a
      - name: 'Publish release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: '~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*'
          commit: 'xpack-develop'
          draft: true
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: false
          repo: '{{ APP_LC_NAME }}-xpack'
          tag: 'v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}
      - name: 'Show SHA'
        run: cat ~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*.sha
{% break -%}
{% endif -%}
{% endfor %}

{% for platform in platforms -%}
{% if platform == "darwin-x64" %}
  macos-x64:
    needs: create-release
    name: 'macOS Intel - {{ APP_NAME }} {% raw %}${{ github.event.inputs.version }}{% endraw %} publish release'
{% if MACOS_INTEL_VERSION %}
    runs-on: [self-hosted, macos, intel, {{ MACOS_INTEL_VERSION }}]
{% else %}
    runs-on: [self-hosted, macos, intel]
{% endif %}
    steps:
      - name: 'Machine'
        run: uname -a
      - name: 'Publish release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: '~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*'
          commit: 'xpack-develop'
          draft: true
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: false
          repo: '{{ APP_LC_NAME }}-xpack'
          tag: 'v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}
      - name: 'Show SHA'
        run: cat ~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*.sha
{% break -%}
{% endif -%}
{% endfor %}

{% for platform in platforms -%}
{% if platform == "darwin-arm64" %}
  macos-arm64:
    needs: create-release
    name: 'macOS Arm - {{ APP_NAME }} {% raw %}${{ github.event.inputs.version }}{% endraw %} publish release'
{% if MACOS_ARM_VERSION %}
    runs-on: [self-hosted, macos, apple, {{ MACOS_ARM_VERSION }}]
{% else %}
    runs-on: [self-hosted, macos, apple]
{% endif %}
    defaults:
      run:
        shell: "/usr/bin/arch -arch arm64e /bin/bash -l {0}"

    steps:
      - name: 'Machine'
        run: uname -a

      - name: 'Publish release'
        # https://github.com/ncipollo/release-action
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: '~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*'
          commit: 'xpack-develop'
          draft: true
          name: 'xPack {{ APP_NAME }} v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          omitBodyDuringUpdate: true
          omitDraftDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
          owner: 'xpack-dev-tools'
          prerelease: true
          replacesArtifacts: false
          repo: '{{ APP_LC_NAME }}-xpack'
          tag: 'v{% raw %}${{ github.event.inputs.version }}{% endraw %}'
          token: {% raw %}${{ secrets.PUBLISH_TOKEN }}{% endraw %}
      - name: 'Show SHA'
        run: cat ~/Work/{{ APP_LC_NAME }}-{% raw %}${{ github.event.inputs.version }}{% endraw %}/deploy/*.sha
{% break -%}
{% endif -%}
{% endfor %}
