name: Run - Publisher

on:
  # Triggers the workflow on pull request events but only for the main branch
  pull_request:
    branches: [main]
    types: [closed]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:
    inputs:
      COMMIT_ID_CHOICE:
        description: 'Choose "publish-all-artifacts-in-repo" only when you want to force republishing all artifacts (e.g. after build failure). Otherwise stick with the default behavior of "publish-artifacts-in-last-commit"'
        required: true
        type: choice
        default: "publish-artifacts-in-last-commit"
        options:
          - "publish-artifacts-in-last-commit"
          - "publish-all-artifacts-in-repo"

jobs:
  get-commit:
    runs-on: ubuntu-latest
    steps:
      # Set the COMMIT_ID env variable
      - name: Set the Commit Id
        id: commit
        run: echo "commit_id=${GITHUB_SHA}" >> $GITHUB_ENV
    outputs:
      commit_id: ${{ steps.commit.outputs.commit_id }}
  #Publish with Commit ID
  Push-Changes-To-APIM-Dev-With-Commit-ID:
    if: (github.event.inputs.COMMIT_ID_CHOICE == 'publish-artifacts-in-last-commit' || github.event.inputs.COMMIT_ID_CHOICE == '')
    needs: get-commit
    uses: ./.github/workflows/run-publisher-with-env.yaml
    with:
      API_MANAGEMENT_ENVIRONMENT: dev # change this to match the dev environment created in settings
      COMMIT_ID: ${{ needs.get-commit.outputs.commit_id }}
      API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH: apimartifacts # change this to the artifacts folder
    secrets: inherit

  #Publish without Commit ID. Publishes all artifacts that reside in the artifacts forlder
  Push-Changes-To-APIM-Dev-Without-Commit-ID:
    if: ( github.event.inputs.COMMIT_ID_CHOICE == 'publish-all-artifacts-in-repo' )
    needs: get-commit
    uses: ./.github/workflows/run-publisher-with-env.yaml
    with:
      API_MANAGEMENT_ENVIRONMENT: dev # change this to match the dev environment created in settings
      API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH: apimartifacts # change this to the artifacts folder
    secrets: inherit

  Push-Changes-To-APIM-Prod-With-Commit-ID:
    if: (github.event.inputs.COMMIT_ID_CHOICE == 'publish-artifacts-in-last-commit' || github.event.inputs.COMMIT_ID_CHOICE == '')
    needs: [get-commit, Push-Changes-To-APIM-Dev-With-Commit-ID]
    uses: ./.github/workflows/run-publisher-with-env.yaml
    with:
      API_MANAGEMENT_ENVIRONMENT: prod # change this to match the prod environment created in settings
      CONFIGURATION_YAML_PATH: configuration.prod.yaml # make sure the file is available at the root
      API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH: apimartifacts # change this to the artifacts folder
      COMMIT_ID: ${{ needs.get-commit.outputs.commit_id }}
    secrets: inherit

  Push-Changes-To-APIM-Prod-Without-Commit-ID:
    if: ( github.event.inputs.COMMIT_ID_CHOICE == 'publish-all-artifacts-in-repo' )
    needs: [get-commit, Push-Changes-To-APIM-Dev-Without-Commit-ID]
    uses: ./.github/workflows/run-publisher-with-env.yaml
    with:
      API_MANAGEMENT_ENVIRONMENT: prod # change this to match the prod environment created in settings
      CONFIGURATION_YAML_PATH: configuration.prod.yaml # make sure the file is available at the root
      API_MANAGEMENT_SERVICE_OUTPUT_FOLDER_PATH: apimartifacts # change this to the artifacts folder
    secrets: inherit
