---
name: release
run-name: release ${{ inputs.tag }}

on:
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to be released'
        required: true

env:
  PROJECT_NAME: ${{ github.event.repository.name }}

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildk
        uses: docker/setup-buildx-action@v3

      - name: Login to Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ github.token }}

      - name: Install Oras
        run: |
          curl -L https://github.com/oras-project/oras/releases/download/v${ORAS_VERSION}/oras_${ORAS_VERSION}_linux_amd64.tar.gz | \
            tar -xz -C /usr/local/bin oras
        env:
          ORAS_VERSION: 1.2.2

      - name: Push dashboards into the production namespace
        run: |
          oras push ghcr.io/${{ github.repository }}/${{ env.PROJECT_NAME }}-dashboards:${{ github.event.inputs.tag }} \
            dashboard.json:application/grafana-dashboard+json \
            alerts.yaml:application/prometheus-alerts+yaml
        working-directory: monitoring

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          target: production
          tags: ghcr.io/${{ github.repository }}:${{ github.event.inputs.tag }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Build and push test coverage image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          target: testcoverage
          tags: ghcr.io/${{ github.repository }}:${{ github.event.inputs.tag }}-testcoverage
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Build and push federation image
        uses: docker/build-push-action@v5
        with:
          push: true
          context: images/federation
          provenance: false
          build-args:
            CLOUDSERVER_VERSION=${{ github.event.inputs.tag }}
          tags: |
            ghcr.io/${{ github.repository }}:${{ github.event.inputs.tag }}-federation
          labels: |
            git.repository=${{ github.repository }}
            git.commit-sha=${{ github.sha }}
          cache-from: type=gha,scope=federation
          cache-to: type=gha,mode=max,scope=federation

      - name: Create Release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          name: Release ${{ github.event.inputs.tag }}
          tag_name: ${{ github.event.inputs.tag }}
          generate_release_notes: true
          target_commitish: ${{ github.sha }}

  trigger-kms-release:
    runs-on: ubuntu-latest
    needs: release
    if: github.repository_owner == 'scality'
    steps:
      - name: Generate GitHub App Token
        uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: ${{ vars.ACTIONS_APP_ID }}
          private-key: ${{ secrets.ACTIONS_APP_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

      - name: Trigger scality-kms release workflow
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
        run: |
          gh workflow run release.yaml \
            --repo scality/scality-kms \
            --ref main \
            --field cloudserver-version=${{ github.event.inputs.tag }}
