name: Website

on:
  push:
    tags:
      - 'website*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  website:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  #v4.2.2
      with:
        token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
        ref: ${{ github.ref }}
        fetch-tags: true

    - name: Setup git
      run: |
        git config --global user.name "Stephen Colebourne (CI)"
        git config --global user.email "scolebourne@joda.org"

    - name: Set up JDK
      uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12  #v4.7.0
      with:
        java-version: 8
        distribution: 'temurin'
        cache: 'maven'

    - name: Maven version
      run: |
        mkdir -p ./.mvn
        echo "-e" >> ./.mvn/maven.config
        echo "-B" >> ./.mvn/maven.config
        echo "-ntp" >> ./.mvn/maven.config
        echo "-DtrimStackTrace=false" >> ./.mvn/maven.config
        echo "--settings" >> ./.mvn/maven.config
        echo "$( pwd )/.github/maven-settings.xml" >> ./.mvn/maven.config
        mvn --version
        mkdir -p target

    #------------------------------------------------------------------------
    - name: Maven site
      run: |
        mvn install site

    - name: Checkout website
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  #v4.2.2
      with:
        token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
        repository: JodaOrg/jodaorg.github.io
        path: target/jodaorg.github.io
        ref: "main"

    - name: Update website
      run: |
        cd target/jodaorg.github.io
        git status

        rm -rf joda-time/
        cp -R ../site joda-time/

        git add -A
        git status
        git commit --message "Update joda-time from CI: $GITHUB_RUN_ID"

        git push origin main

    # delete the initiating tag, but not if the build is manually initialized by workflow_dispatch
    - name: Delete website tag
      if: "always() && github.ref != 'refs/heads/main'"
      run: |
        git tag --delete "${GITHUB_REF_NAME}" || true
        git push --delete origin "${GITHUB_REF_NAME}" || true
