# Release pipeline
# Variable 'prerelease' was defined in the Variables tab
# Variable 'prereleaseTag' was defined in the Variables tab
# Variable 'publishVersion' was defined in the Variables ta

pr: none
trigger: none

variables:
  - group: "Github and NPM secrets"
  - name: tags
    value: production,externalfacing

resources:
  repositories:
    - repository: 1esPipelines
      type: git
      name: 1ESPipelineTemplates/1ESPipelineTemplates
      ref: refs/tags/release

extends:
  template: v1/1ES.Official.PipelineTemplate.yml@1esPipelines
  parameters:
    pool:
      name: Azure-Pipelines-1ESPT-ExDShared
      image: windows-latest
      os: windows # We need windows because compliance task only run on windows.
    stages:
      - stage: main
        jobs:
          - job: Release
            pool:
              name: "1ES-Host-Ubuntu"
              image: "1ES-PT-Ubuntu-20.04"
              os: linux
            workspace:
              clean: all
            steps:
              # For multiline scripts, we want the whole task to fail if any line of the script fails.
              # ADO doesn't have bash configured this way by default. To fix we override the SHELLOPTS built-in variable.
              # https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
              # The options below include ADO defaults (braceexpand:hashall:interactive-comments) plus
              # errexit:errtrace for better error behavior.
              - script: |
                  echo "##vso[task.setvariable variable=shellopts]braceexpand:hashall:interactive-comments:errexit:errtrace"
                displayName: Force exit on error (bash)

              - script: |
                  git checkout --track "origin/${BUILD_SOURCEBRANCH//refs\/heads\/}"
                  git pull
                displayName: Re-attach HEAD

              - task: NodeTool@0
                inputs:
                  versionSpec: "18.x"
                  checkLatest: true
                displayName: "Install Node.js"

              - script: npm install
                displayName: Install dependencies

              - script: |
                  git config user.name "Fluent UI Build"
                  git config user.email "fluentui-internal@service.microsoft.com"
                  git remote set-url origin https://$(githubUser):$(githubPAT)@github.com/microsoft/use-disposable.git
                displayName: Authenticate git for pushes

              - script: |
                  echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
                displayName: Write .npmrc

              - script: |
                  npm run release -- $(publishVersion) --ci
                displayName: Publish (official)
                condition: eq(variables.prerelease, false)
                env:
                  NPM_TOKEN: $(npmToken)

              - script: |
                  npm run release -- $(publishVersion) --preRelease $(prereleaseTag) --ci
                displayName: Publish (prerelease)
                condition: eq(variables.prerelease, true)
                env:
                  NPM_TOKEN: $(npmToken)
