name: RISC-V ISA Build

on:
  workflow_dispatch:
    inputs:
      create_release:
        description: 'Create a new RISC-V ISA release if set to true'
        required: false
        default: 'false'
      target_branch:
        description: 'Target Branch'
        required: true
        default: 'main'
      release_notes:
        description: 'Release Notes'
        required: false
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    # Checkout the repository
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        submodules: recursive
      
    # Set the short SHA for use in artifact names
    - name: Set short SHA
      run: echo "SHORT_SHA=$(echo ${GITHUB_SHA::7})" >> $GITHUB_ENV
      
    # Get the current date
    - name: Get current date
      run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

    # Pull the latest RISC-V Docs container image
    # https://github.com/riscv/riscv-docs-base-container-image
    # https://hub.docker.com/r/riscvintl/riscv-docs-base-container-image
    - name: Pull Container
      id: pull_container_image
      run: docker pull riscvintl/riscv-docs-base-container-image:latest

    # Build PDF and HTML files using the container
    - name: Build Files
      id: build_files
      if: steps.pull_container_image.outcome == 'success'
      run: |
        docker run --rm -v ${{ github.workspace }}:/build riscvintl/riscv-docs-base-container-image:latest \
        /bin/sh -c "make -j$(nproc)"

    # Upload the riscv-privileged PDF file
    - name: Upload riscv-privileged.pdf
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-privileged-${{ env.SHORT_SHA }}.pdf
        path: ${{ github.workspace }}/build/riscv-privileged.pdf
        retention-days: 7

    # Upload the riscv-privileged HTML file
    - name: Upload riscv-privileged.html
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-privileged-${{ env.SHORT_SHA }}.html
        path: ${{ github.workspace }}/build/riscv-privileged.html
        retention-days: 7

    # Upload the riscv-privileged EPUB file
    - name: Upload riscv-privileged.epub
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-privileged-${{ env.SHORT_SHA }}.epub
        path: ${{ github.workspace }}/build/riscv-privileged.epub
        retention-days: 7

    # Upload the riscv-unprivileged PDF file
    - name: Upload riscv-unprivileged.pdf
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-unprivileged-${{ env.SHORT_SHA }}.pdf
        path: ${{ github.workspace }}/build/riscv-unprivileged.pdf
        retention-days: 7

    # Upload the riscv-unprivileged HTML file
    - name: Upload riscv-unprivileged.html
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-unprivileged-${{ env.SHORT_SHA }}.html
        path: ${{ github.workspace }}/build/riscv-unprivileged.html
        retention-days: 7

    # Upload the riscv-unprivileged EPUB file
    - name: Upload riscv-unprivileged.epub
      if: steps.build_files.outcome == 'success'
      uses: actions/upload-artifact@v4
      with:
        name: riscv-unprivileged-${{ env.SHORT_SHA }}.epub
        path: ${{ github.workspace }}/build/riscv-unprivileged.epub
        retention-days: 7

    - name: Create Release
      if: steps.build_files.outcome == 'success' && github.event_name == 'workflow_dispatch' && github.event.inputs.create_release == 'true'
      uses: softprops/action-gh-release@v2
      with:
        draft: false
        tag_name: riscv-isa-release-${{ env.SHORT_SHA }}-${{ env.CURRENT_DATE }}
        name: ${{ env.CURRENT_DATE }}
        body: |
          This release was created by: ${{ github.event.sender.login }}
          Release Notes: ${{ github.event.inputs.release_notes }}
        files: |
          ${{ github.workspace }}/build/riscv-privileged.pdf
          ${{ github.workspace }}/build/riscv-privileged.html
          ${{ github.workspace }}/build/riscv-privileged.epub
          ${{ github.workspace }}/build/riscv-unprivileged.pdf
          ${{ github.workspace }}/build/riscv-unprivileged.html
          ${{ github.workspace }}/build/riscv-unprivileged.epub
      env:
        GITHUB_TOKEN: ${{ secrets.GHTOKEN }}

