name: Publish artifact

on:
  release:
    types: [published]

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  determine-version:
    runs-on: ubuntu-latest
    outputs:
      project-version: ${{ steps.set-version.outputs.project-version }}
      is-lts: ${{ steps.set-version.outputs.is-lts }}
      is-rc: ${{ steps.set-version.outputs.is-rc }}
    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: "temurin"
          java-version: 17
          cache: "maven"
      - name: Set version
        id: set-version
        run: |
          version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)

          dot_count=$(echo $version | sed 's/[^.]//g' | awk '{ print length }')
          if [ "${dot_count}" = '2' ]
          then
            is_lts=true
          else
            is_lts=false
          fi

          is_rc=false
          if [[ ${version} == *"-SNAPSHOT" ]]; then
            is_rc=true
          fi

          echo "Version is $version, is_lts: $is_lts, is_rc: $is_rc"

          echo "is-lts=${is_lts}" >> $GITHUB_OUTPUT
          echo "project-version=$version" >> $GITHUB_OUTPUT
          echo "is-rc=${is_rc}" >> $GITHUB_OUTPUT
  war:
    permissions:
      contents: write # to upload release asset (softprops/action-gh-release)

    runs-on: ubuntu-latest
    needs: determine-version
    if: ${{ needs.determine-version.outputs.is-rc == 'false' }}
    steps:
      - name: Fetch war
        id: fetch-war
        run: |
          FILE_NAME=jenkins.war
          PROJECT_VERSION=${{ needs.determine-version.outputs.project-version }}
          IS_LTS=${{ needs.determine-version.outputs.is-lts }}
          echo $PROJECT_VERSION
          echo "file-name=$FILE_NAME" >> $GITHUB_OUTPUT

          REPO=war
          if [ ${IS_LTS} = 'true' ]
          then
            REPO=war-stable
          fi

          echo $REPO

          wget -q https://get.jenkins.io/${REPO}/${PROJECT_VERSION}/${FILE_NAME}
      - name: Upload Release Asset
        id: upload-war
        uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: ${{ steps.fetch-war.outputs.file-name }}
  debian:
    permissions:
      contents: write # to upload release asset (softprops/action-gh-release)

    runs-on: ubuntu-latest
    needs: determine-version
    if: ${{ needs.determine-version.outputs.is-rc == 'false' }}
    steps:
      - name: Fetch Deb
        id: fetch-deb
        if: always()
        run: |
          PROJECT_VERSION=${{ needs.determine-version.outputs.project-version }}
          echo $PROJECT_VERSION
          FILE_NAME=jenkins_${PROJECT_VERSION}_all.deb
          IS_LTS=${{ needs.determine-version.outputs.is-lts }}
          echo "file-name=$FILE_NAME" >> $GITHUB_OUTPUT

          REPO=debian
          if [ ${IS_LTS} = 'true' ]
          then
            REPO=debian-stable
          fi

          echo $REPO

          wget -q https://get.jenkins.io/${REPO}/${FILE_NAME}
      - name: Upload Release Asset
        id: upload-deb
        if: always()
        uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: ${{ steps.fetch-deb.outputs.file-name }}
  redhat:
    permissions:
      contents: write # to upload release asset (softprops/action-gh-release)

    runs-on: ubuntu-latest
    needs: determine-version
    if: ${{ needs.determine-version.outputs.is-rc == 'false' }}
    steps:
      - name: Fetch RPM
        id: fetch-rpm
        if: always()
        run: |
          PROJECT_VERSION=${{ needs.determine-version.outputs.project-version }}
          echo $PROJECT_VERSION
          FILE_NAME=jenkins-${PROJECT_VERSION}-1.1.noarch.rpm
          IS_LTS=${{ needs.determine-version.outputs.is-lts }}

          echo "file-name=$FILE_NAME" >> $GITHUB_OUTPUT

          REPO=redhat
          if [ ${IS_LTS} = 'true' ]
          then
            REPO=redhat-stable
          fi

          echo $REPO

          wget -q https://get.jenkins.io/${REPO}/${FILE_NAME}
      - name: Upload Release Asset
        id: upload-rpm
        if: always()
        uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: ${{ steps.fetch-rpm.outputs.file-name }}
  windows:
    permissions:
      contents: write # to upload release asset (softprops/action-gh-release)

    runs-on: ubuntu-latest
    needs: determine-version
    if: ${{ needs.determine-version.outputs.is-rc == 'false' }}
    steps:
      - name: Fetch MSI
        id: fetch-msi
        if: always()
        run: |
          PROJECT_VERSION=${{ needs.determine-version.outputs.project-version }}
          echo $PROJECT_VERSION
          FILE_NAME=jenkins.msi
          IS_LTS=${{ needs.determine-version.outputs.is-lts }}

          echo "file-name=$FILE_NAME" >> $GITHUB_OUTPUT

          REPO=windows
          if [ ${IS_LTS} = 'true' ]
          then
            REPO=windows-stable
          fi

          echo $REPO

          wget -q https://get.jenkins.io/${REPO}/${PROJECT_VERSION}/${FILE_NAME}
      - name: Upload Release Asset
        id: upload-msi
        if: always()
        uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: ${{ steps.fetch-msi.outputs.file-name }}
  suse:
    permissions:
      contents: write # to upload release asset (softprops/action-gh-release)

    runs-on: ubuntu-latest
    needs: determine-version
    if: ${{ needs.determine-version.outputs.is-rc == 'false' }}
    steps:
      - name: Fetch suse rpm
        id: fetch-suse-rpm
        if: always()
        run: |
          PROJECT_VERSION=${{ needs.determine-version.outputs.project-version }}
          echo $PROJECT_VERSION
          # we need opensuse to the file 
          OUTPUT_FILE_NAME=jenkins-${PROJECT_VERSION}-1.2-opensuse.noarch.rpm
          IS_LTS=${{ needs.determine-version.outputs.is-lts }}
          echo "file-name=$OUTPUT_FILE_NAME" >> $GITHUB_OUTPUT

          REPO=opensuse
          if [ ${IS_LTS} = 'true' ]
          then
            REPO=opensuse-stable
          fi

          echo $REPO

          wget -q https://get.jenkins.io/${REPO}/jenkins-${PROJECT_VERSION}-1.2.noarch.rpm -O ${OUTPUT_FILE_NAME}
      - name: Upload Release Asset
        id: upload-suse-rpm
        if: always()
        uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: ${{ steps.fetch-suse-rpm.outputs.file-name }}
