# Copyright 2016-2020 the Tectonic Project
# Licensed under the MIT License.
#
# Deployment pipeline, run when all CI tests pass on `master` or `rc`.
# Parameters:
#
# - `isMainDev`: this is an update to the main development branch; update
#   continuous-deployment artifacts
# - `isRelease`: this is an update to the `rc` branch; run the Official Release
#   machinery

parameters:
- name: isMainDev
  type: boolean
  default: false
- name: isRelease
  type: boolean
  default: false

jobs:

# Things that should happen in either main-dev *or* release mode.
- job: update_book
  pool:
    vmImage: ubuntu-20.04
  variables:
    - group: Deployment Credentials
  steps:
  - template: azure-deployment-setup.yml
    parameters:
      isMainDev: ${{ parameters.isMainDev }}
      isRelease: ${{ parameters.isRelease }}
      setupGit: true
  - bash: |
      set -xeuo pipefail

      # Worth adding better Cranko support for this? reboot-branch is close
      dist/force-push-tree.sh \
        $(Pipeline.Workspace)/book \
        https://github.com/tectonic-typesetting/book.git \
        "$TOPLEVEL_MODE" \
        "docs mdbook"
    displayName: Update book HTML
    condition: and(succeeded(), ne(variables['TOPLEVEL_MODE'], 'skip'))
    env:
      GITHUB_TOKEN: $(GITHUB_TOKEN)

# Things that only happen in main-dev mode:
- ${{ if parameters.isMainDev }}:
  - job: recreate_continuous
    pool:
      vmImage: ubuntu-20.04
    variables:
    - group: Deployment Credentials
    steps:
    - template: azure-deployment-setup.yml
      parameters:
        isMainDev: ${{ parameters.isMainDev }}
        isRelease: ${{ parameters.isRelease }}
        setupGit: true
    - bash: |
        set -xeuo pipefail
        cranko github delete-release continuous
        git tag -f continuous HEAD
        git push -f origin refs/tags/continuous
        cranko github create-custom-release \
          --name "Continuous Deployment" \
          --prerelease \
          --desc "Continuous deployment of commit $(git rev-parse --short HEAD)" \
          continuous
        cranko github upload-artifacts --by-tag continuous \
          $(Pipeline.Workspace)/binary-*/* \
          $(Pipeline.Workspace)/appimage/*
      displayName: Recreate continuous-deployment GitHub release
      env:
        GITHUB_TOKEN: $(GITHUB_TOKEN)

# Release-only activities:
- ${{ if parameters.isRelease }}:
  - job: branch_and_tag
    pool:
      vmImage: ubuntu-20.04
    variables:
    - group: Deployment Credentials
    steps:
    - template: azure-deployment-setup.yml
      parameters:
        isMainDev: ${{ parameters.isMainDev }}
        isRelease: ${{ parameters.isRelease }}
        setupGit: true
    - bash: |
        set -xeou pipefail
        cranko release-workflow tag
        git push --tags origin release:release
      displayName: Tag and push
      env:
        GITHUB_TOKEN: $(GITHUB_TOKEN)

  - job: cargo_publish
    pool:
      vmImage: ubuntu-20.04
    variables:
    - group: Deployment Credentials
    steps:
    - template: azure-deployment-setup.yml
      parameters:
        isMainDev: ${{ parameters.isMainDev }}
        isRelease: ${{ parameters.isRelease }}
    - bash: |
        set -xeou pipefail
        cranko cargo foreach-released -- publish --no-verify
      displayName: Publish updated Cargo crates
      env:
        CARGO_REGISTRY_TOKEN: $(CARGO_REGISTRY_TOKEN)

  - job: github_releases
    dependsOn: branch_and_tag # otherwise, GitHub will create the tag itself
    pool:
      vmImage: ubuntu-20.04
    variables:
    - group: Deployment Credentials
    steps:
    - template: azure-deployment-setup.yml
      parameters:
        isMainDev: ${{ parameters.isMainDev }}
        isRelease: ${{ parameters.isRelease }}
        setupGit: true
    - bash: |
        set -xeuo pipefail
        cranko github create-releases

        if cranko show if-released --exit-code tectonic; then
          cranko github upload-artifacts tectonic \
            $(Pipeline.Workspace)/binary-*/* \
            $(Pipeline.Workspace)/appimage/*
        fi
      displayName: Create per-project GitHub releases
      env:
        GITHUB_TOKEN: $(GITHUB_TOKEN)

  # Release jobs that should only run when toplevel is released. Note that we
  # can only know if this is the case on the fly, so we have to launch all of
  # these jobs even if it turns out that `tectonic` was not released. (I.e.,
  # $TOPLEVEL_MODE is set in azure-deployment-setup.yml.)

  - job: update_website
    pool:
      vmImage: ubuntu-20.04
    variables:
      - group: Deployment Credentials
    steps:
    - template: azure-deployment-setup.yml
      parameters:
        isMainDev: ${{ parameters.isMainDev }}
        isRelease: ${{ parameters.isRelease }}
        setupGit: true
    - bash: |
        set -xeuo pipefail
        bash dist/update-website.sh
      displayName: Update GitHub Pages website
      condition: and(succeeded(), ne(variables['TOPLEVEL_MODE'], 'skip'))
      env:
        GITHUB_TOKEN: $(GITHUB_TOKEN)

  - job: update_arch_linux
    dependsOn: ['cargo_publish', 'github_releases'] # need artifacts to calculate digests
    pool:
      vmImage: ubuntu-20.04
    variables:
      - group: Deployment Credentials
    steps:
    - template: azure-deployment-setup.yml
      parameters:
        isMainDev: ${{ parameters.isMainDev }}
        isRelease: ${{ parameters.isRelease }}
        setupGit: true
    - bash: |
        set -xeuo pipefail
        cranko ci-util env-to-file --decode=base64 ARCHLINUX_DEPLOY_KEY_BASE64 dist/arch/deploy_key
        bash dist/arch/deploy.sh
        shred dist/arch/deploy_key
      displayName: Update ArchLinux package
      condition: and(succeeded(), ne(variables['TOPLEVEL_MODE'], 'skip'))
      env:
        ARCHLINUX_DEPLOY_KEY_BASE64: $(ARCHLINUX_DEPLOY_KEY_BASE64)
