# SPDX-License-Identifier: MIT
#
# https://code.forgejo.org/forgejo/runner
#
#  Build the runner binaries and OCI images
#
#  ROLE: forgejo-integration
#  DOER: forgejo-ci
#  TOKEN: <generated from https://code.forgejo.org/forgejo-ci>
#
name: Build release

on:
  push:
    tags:
    - 'v*'

enable-email-notifications: true

jobs:
  release:
    runs-on: lxc-bookworm
    # root is used for testing, allow it
    if: vars.ROLE == 'forgejo-integration' || forge.repository_owner == 'root'
    steps:
      - uses: https://data.forgejo.org/actions/checkout@v4

      - name: Increase the verbosity when there are no secrets
        id: verbose
        run: |
          if test -z "${{ secrets.TOKEN }}"; then
            value=true
          else
            value=false
          fi
          echo "value=$value" >> "$FORGEJO_OUTPUT"

      - name: Sanitize the name of the repository
        id: repository
        run: |
          echo "value=${FORGEJO_REPOSITORY##*/}" >> "$FORGEJO_OUTPUT"

      - name: create test TOKEN
        id: token
        if: ${{ secrets.TOKEN == '' }}
        run: |
          apt-get -qq install -y jq
          url="${{ env.FORGEJO_SERVER_URL }}"
          hostport=${url##http*://}
          hostport=${hostport%%/}
          doer=root
          api=http://$doer:admin1234@$hostport/api/v1/users/$doer/tokens
          curl -sS -X DELETE $api/release
          token=$(curl -sS -X POST -H 'Content-Type: application/json' --data-raw '{"name": "release", "scopes": ["all"]}' $api | jq --raw-output .sha1)
          echo "value=${token}" >> "$FORGEJO_OUTPUT"

      - name: version from ref_name
        id: tag-version
        run: |
          version=${FORGEJO_REF_NAME##*v}
          echo "value=$version" >> "$FORGEJO_OUTPUT"

      - name: build without TOKEN
        if: ${{ secrets.TOKEN == '' }}
        uses: https://data.forgejo.org/forgejo/forgejo-build-publish/build@v5.4.1
        with:
          forgejo: "${{ env.FORGEJO_SERVER_URL }}"
          owner: "${{ env.FORGEJO_REPOSITORY_OWNER }}"
          repository: "${{ steps.repository.outputs.value }}"
          doer: root
          sha: "${{ forge.sha }}"
          release-version: "${{ steps.tag-version.outputs.value }}"
          token: ${{ steps.token.outputs.value }}
          platforms: linux/amd64,linux/arm64
          binary-name: forgejo-runner
          binary-path: /bin/forgejo-runner
          verbose: ${{ steps.verbose.outputs.value }}

      - name: build with TOKEN
        if: ${{ secrets.TOKEN != '' }}
        uses: https://data.forgejo.org/forgejo/forgejo-build-publish/build@v5.4.1
        with:
          forgejo: "${{ env.FORGEJO_SERVER_URL }}"
          owner: "${{ env.FORGEJO_REPOSITORY_OWNER }}"
          repository: "${{ steps.repository.outputs.value }}"
          doer: "${{ secrets.DOER }}"
          sha: "${{ forge.sha }}"
          release-version: "${{ steps.tag-version.outputs.value }}"
          token: "${{ secrets.TOKEN }}"
          platforms: linux/amd64,linux/arm64
          binary-name: forgejo-runner
          binary-path: /bin/forgejo-runner
          verbose: ${{ steps.verbose.outputs.value }}
