name: "BSON Release"
run-name: "BSON Release for ${{ github.ref }}"

on:
  # for auto-deploy when merging a release-candidate PR
  push:
    branches:
      - 'master'
      - '*-stable'

  # for manual release
  workflow_dispatch:
    inputs:
      pr:
        description: "The number of the merged release candidate PR"
        required: true

env:
  GEM_NAME: bson
  PRODUCT_NAME: BSON for Ruby
  PRODUCT_ID: mongodb-ruby-driver

permissions:
  # required for all workflows
  security-events: write

  # required to fetch internal or private CodeQL packs
  packages: read

  # only required for workflows in private repositories
  actions: read
  pull-requests: read
  contents: write

  # required by the mongodb-labs/drivers-github-tools/setup@v2 step
  # also required by `rubygems/release-gem`
  id-token: write

jobs:
  check:
    name: "Check Release"
    runs-on: ubuntu-latest
    outputs:
      message: ${{ steps.check.outputs.message }}
      ref: ${{ steps.check.outputs.ref }}
    steps:
      - name: "Run the check action"
        id: check
        uses: mongodb-labs/drivers-github-tools/ruby/pr-check@v3

  build:
    name: "Build Gems"
    needs: check
    environment: release
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ruby: [ 'ruby-3.2', 'jruby-9.4' ]
    steps:
      - name: "Run the build action"
        uses: mongodb-labs/drivers-github-tools/ruby/build@v3
        with:
          app_id: ${{ vars.APP_ID }}
          app_private_key: ${{ secrets.APP_PRIVATE_KEY }}
          artifact: ${{ matrix.ruby }}
          gem_name: ${{ env.GEM_NAME }}
          ruby_version: ${{ matrix.ruby }}
          ref: ${{ needs.check.outputs.ref }}

  publish:
    name: Publish Gems
    needs: [ check, build ]
    environment: release
    runs-on: ubuntu-latest
    steps:
      - name: "Run the publish action"
        uses: mongodb-labs/drivers-github-tools/ruby/publish@v3
        with:
          app_id: ${{ vars.APP_ID }}
          app_private_key: ${{ secrets.APP_PRIVATE_KEY }}
          aws_role_arn: ${{ secrets.AWS_ROLE_ARN }}
          aws_region_name: ${{ vars.AWS_REGION_NAME }}
          aws_secret_id: ${{ secrets.AWS_SECRET_ID }}
          dry_run: false
          gem_name: ${{ env.GEM_NAME }}
          product_name: ${{ env.PRODUCT_NAME }}
          product_id: ${{ env.PRODUCT_ID }}
          release_message: ${{ needs.check.outputs.message }}
          ref: ${{ needs.check.outputs.ref }}
