# Copyright 2026 John Wilger

name: Release-plz

on:
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  release-plz-release:
    name: Release unpublished crates
    if: ${{ github.repository == 'Slipstream/emc' }}
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    permissions:
      contents: write
      pull-requests: read
    env:
      CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
      GIT_TOKEN: ${{ secrets.RELEASE_PLZ_TOKEN }}
    steps:
      - name: Skip when release secrets are not available
        if: ${{ env.GIT_TOKEN == '' || env.CARGO_REGISTRY_TOKEN == '' }}
        run: |
          echo "release-plz release skipped: configure RELEASE_PLZ_TOKEN and CARGO_REGISTRY_TOKEN repository secrets to publish crates"

      - name: Checkout repository
        if: ${{ env.GIT_TOKEN != '' && env.CARGO_REGISTRY_TOKEN != '' }}
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Install or reuse Nix
        if: ${{ env.GIT_TOKEN != '' && env.CARGO_REGISTRY_TOKEN != '' }}
        run: ./.forgejo/scripts/install-nix.sh

      - name: Publish unpublished crates
        if: ${{ env.GIT_TOKEN != '' && env.CARGO_REGISTRY_TOKEN != '' }}
        run: |
          set -euo pipefail
          export PATH="/nix/var/nix/profiles/default/bin:$PATH"
          . ./.forgejo/scripts/configure-release-plz-git-auth.sh
          nix develop --command release-plz release --forge gitea

  release-plz-pr:
    name: Create or update release PR
    if: ${{ github.repository == 'Slipstream/emc' }}
    needs: release-plz-release
    runs-on: ubuntu-24.04
    timeout-minutes: 30
    permissions:
      contents: write
      pull-requests: write
    concurrency:
      group: release-plz-${{ github.ref }}
      cancel-in-progress: false
    env:
      GIT_TOKEN: ${{ secrets.RELEASE_PLZ_TOKEN }}
      RELEASE_SIGNING_KEY: ${{ secrets.RELEASE_SIGNING_KEY }}
      RELEASE_SIGNING_NAME: ${{ vars.RELEASE_SIGNING_NAME }}
      RELEASE_SIGNING_EMAIL: ${{ vars.RELEASE_SIGNING_EMAIL }}
    steps:
      - name: Skip when release PR secrets are not available
        if: ${{ env.GIT_TOKEN == '' || env.RELEASE_SIGNING_KEY == '' }}
        run: |
          echo "release-plz release PR skipped: configure RELEASE_PLZ_TOKEN and RELEASE_SIGNING_KEY repository secrets to create signed release PRs"

      - name: Checkout repository
        if: ${{ env.GIT_TOKEN != '' && env.RELEASE_SIGNING_KEY != '' }}
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Install or reuse Nix
        if: ${{ env.GIT_TOKEN != '' && env.RELEASE_SIGNING_KEY != '' }}
        run: ./.forgejo/scripts/install-nix.sh

      - name: Open release PR
        if: ${{ env.GIT_TOKEN != '' && env.RELEASE_SIGNING_KEY != '' }}
        run: |
          set -euo pipefail
          export PATH="/nix/var/nix/profiles/default/bin:$PATH"
          . ./.forgejo/scripts/configure-release-plz-git-auth.sh
          . ./.forgejo/scripts/configure-release-plz-git-signing.sh
          git config --global user.name "${RELEASE_SIGNING_NAME:-release-plz-bot}"
          git config --global user.email "${RELEASE_SIGNING_EMAIL:-release-plz-bot@noreply.git.johnwilger.com}"
          nix develop --command sh ./.forgejo/scripts/create-release-plz-release-pr.sh
