---
name: Release
run-name: Release ${{ inputs.release_type }}

on:
  workflow_dispatch:
    inputs:
      release_type:
        description: "Release Options"
        required: true
        default: "Initial Release"
        type: choice
        options:
          - Initial Release
          - Redeploy
          - Dry Run

env:
  _AZ_REGISTRY: "bitwardenprod.azurecr.io"

jobs:
  setup:
    name: Setup
    runs-on: ubuntu-22.04
    outputs:
      release_version: ${{ steps.version.outputs.version }}
      branch-name: ${{ steps.branch.outputs.branch-name }}
    steps:
      - name: Branch check
        if: ${{ inputs.release_type != 'Dry Run' }}
        run: |
          if [[ "$GITHUB_REF" != "refs/heads/rc" ]] && [[ "$GITHUB_REF" != "refs/heads/hotfix-rc" ]]; then
            echo "==================================="
            echo "[!] Can only release from the 'rc' or 'hotfix-rc' branches"
            echo "==================================="
            exit 1
          fi

      - name: Check out repo
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Check release version
        id: version
        uses: bitwarden/gh-actions/release-version-check@main
        with:
          release-type: ${{ inputs.release_type }}
          project-type: dotnet
          file: Directory.Build.props

      - name: Get branch name
        id: branch
        run: |
          BRANCH_NAME=$(basename ${{ github.ref }})
          echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT

  release-docker:
    name: Build Docker images
    runs-on: ubuntu-22.04
    needs: setup
    env:
      _RELEASE_VERSION: ${{ needs.setup.outputs.release_version }}
      _BRANCH_NAME: ${{ needs.setup.outputs.branch-name }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - project_name: Admin
          - project_name: Api
          - project_name: Attachments
          - project_name: Billing
          - project_name: Events
          - project_name: EventsProcessor
          - project_name: Icons
          - project_name: Identity
          - project_name: MsSql
          - project_name: MsSqlMigratorUtility
          - project_name: Nginx
          - project_name: Notifications
          - project_name: Scim
          - project_name: Server
          - project_name: Setup
          - project_name: Sso
    steps:
      - name: Print environment
        env:
          RELEASE_OPTION: ${{ inputs.release_type }}
        run: |
          whoami
          docker --version
          echo "GitHub ref: $GITHUB_REF"
          echo "GitHub event: $GITHUB_EVENT"
          echo "Github Release Option: $RELEASE_OPTION"

      - name: Check out repo
        uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

      - name: Set up project name
        id: setup
        run: |
          PROJECT_NAME=$(echo "${{ matrix.project_name }}" | awk '{print tolower($0)}')
          echo "Matrix name: ${{ matrix.project_name }}"
          echo "PROJECT_NAME: $PROJECT_NAME"
          echo "project_name=$PROJECT_NAME" >> $GITHUB_OUTPUT

      ########## ACR PROD ##########
      - name: Log in to Azure - production subscription
        uses: Azure/login@e15b166166a8746d1a47596803bd8c1b595455cf # v1.6.0
        with:
          creds: ${{ secrets.AZURE_PROD_KV_CREDENTIALS }}

      - name: Log in to Azure ACR
        run: az acr login -n $_AZ_REGISTRY --only-show-errors

      - name: Pull latest project image
        env:
          PROJECT_NAME: ${{ steps.setup.outputs.project_name }}
        run: |
          if [[ "${{ inputs.release_type }}" == "Dry Run" ]]; then
            docker pull $_AZ_REGISTRY/$PROJECT_NAME:latest
          else
            docker pull $_AZ_REGISTRY/$PROJECT_NAME:$_BRANCH_NAME
          fi

      - name: Tag version and latest
        env:
          PROJECT_NAME: ${{ steps.setup.outputs.project_name }}
        run: |
          if [[ "${{ inputs.release_type }}" == "Dry Run" ]]; then
            docker tag $_AZ_REGISTRY/$PROJECT_NAME:latest $_AZ_REGISTRY/$PROJECT_NAME:dryrun
          else
            docker tag $_AZ_REGISTRY/$PROJECT_NAME:$_BRANCH_NAME $_AZ_REGISTRY/$PROJECT_NAME:$_RELEASE_VERSION
            docker tag $_AZ_REGISTRY/$PROJECT_NAME:$_BRANCH_NAME $_AZ_REGISTRY/$PROJECT_NAME:latest
          fi

      - name: Push version and latest image
        env:
          PROJECT_NAME: ${{ steps.setup.outputs.project_name }}
        run: |
          if [[ "${{ inputs.release_type }}" == "Dry Run" ]]; then
            docker push $_AZ_REGISTRY/$PROJECT_NAME:dryrun
          else
            docker push $_AZ_REGISTRY/$PROJECT_NAME:$_RELEASE_VERSION
            docker push $_AZ_REGISTRY/$PROJECT_NAME:latest
          fi

      - name: Log out of Docker
        run: docker logout

  release:
    name: Create GitHub release
    runs-on: ubuntu-22.04
    needs: setup
    steps:
      - name: Download latest release Docker stubs
        if: ${{ inputs.release_type != 'Dry Run' }}
        uses: bitwarden/gh-actions/download-artifacts@main
        with:
          workflow: build.yml
          workflow_conclusion: success
          branch: ${{ needs.setup.outputs.branch-name }}
          artifacts: "docker-stub-US.zip,
            docker-stub-US-sha256.txt,
            docker-stub-EU.zip,
            docker-stub-EU-sha256.txt,
            swagger.json"

      - name: Dry Run - Download latest release Docker stubs
        if: ${{ inputs.release_type == 'Dry Run' }}
        uses: bitwarden/gh-actions/download-artifacts@main
        with:
          workflow: build.yml
          workflow_conclusion: success
          branch: main
          artifacts: "docker-stub-US.zip,
            docker-stub-US-sha256.txt,
            docker-stub-EU.zip,
            docker-stub-EU-sha256.txt,
            swagger.json"

      - name: Create release
        if: ${{ inputs.release_type != 'Dry Run' }}
        uses: ncipollo/release-action@2c591bcc8ecdcd2db72b97d6147f871fcd833ba5 # v1.14.0
        with:
          artifacts: "docker-stub-US.zip,
            docker-stub-US-sha256.txt,
            docker-stub-EU.zip,
            docker-stub-EU-sha256.txt,
            swagger.json"
          commit: ${{ github.sha }}
          tag: "v${{ needs.setup.outputs.release_version }}"
          name: "Version ${{ needs.setup.outputs.release_version }}"
          body: "<insert release notes here>"
          token: ${{ secrets.GITHUB_TOKEN }}
          draft: true
