name: Publish gem to rubygems.org

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: read

jobs:
  push:
    if: github.repository == 'ruby/psych'
    runs-on: ubuntu-latest

    environment:
      name: rubygems.org
      url: https://rubygems.org/gems/psych

    permissions:
      contents: write
      id-token: write

    strategy:
      matrix:
        ruby: ["ruby", "jruby"]

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
        with:
          egress-policy: audit

      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Set up Ruby
        uses: ruby/setup-ruby@d5126b9b3579e429dd52e51e68624dda2e05be25 # v1.267.0
        with:
          ruby-version: ${{ matrix.ruby }}

        # https://github.com/rubygems/rubygems/issues/5882
      - name: Install dependencies and build for JRuby
        run: |
          sudo apt-get update
          sudo apt-get install -y default-jdk maven
          gem update --system
          gem install ruby-maven rake-compiler --no-document
          rake compile
        if: matrix.ruby == 'jruby'

      - name: Install dependencies
        run: bundle install --jobs 4 --retry 3

      - name: Publish to RubyGems
        uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1

      - name: Create GitHub release
        run: |
          tag_name="$(git describe --tags --abbrev=0)"
          gh release create "${tag_name}" --verify-tag --generate-notes
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        if: matrix.ruby != 'jruby'
