# Builds the Betaflight firmware
#
# After building, artifacts are released to a seperate repository.
#
# Azure Pipelines requires the following extensions to be installed:
# - GitHub Tool: https://marketplace.visualstudio.com/items?itemName=marcelo-formentao.github-tools
#
# You'll also need to setup the follwing pipeline variables: 
#     "releaseNotes" - This is used to add the release notes in the windows job in the build stage so they can be published as part of the github release in the release stage
#     "endpoint" - The name of the github endpoint link setup in AzDo - setup when linking AzDo and GitHub
#     "owner" - The owner of the repository to release to e.g. betaflight
#     "repoName" - The name of the repository to release to e.g. betaflight-configurator-nightly


variables:
  owner: betaflight
  repoName: betaflight-pull-requests
  releaseNotes: This is a build of a community provided pull request. It may be unstable and result in corrupted configurations or data loss. **Use only for testing.**
  vmImage: 'ubuntu-20.04'

name: $(Build.RequestedFor)_$(Build.SourceBranchName)_$(BuildID)

# For some reason the trigger does not pick up on "*-maintenance" - manually adding the current maintenance branch for now
trigger:
  batch: true
  branches:
    include:
    - master
    - 4.2-maintenance
    - "*-maintenance"

pr:
  drafts: false
  branches:
    include:
    - master
    - 4.2-maintenance
    - "*-maintenance"

stages:
- stage: Build
  jobs:
  - job: 'Linux'
    pool:
      vmImage: '$(vmImage)'
    steps:
    - script: make arm_sdk_install
      displayName: 'Install the build toolchain'
    - script: sudo apt-get install -y libblocksruntime-dev
      displayName: 'Install extra packages'
    - script: make  EXTRA_FLAGS=-Werror checks
      displayName: 'Run sanity checks'
    - script: make  EXTRA_FLAGS=-Werror test-all
      displayName: 'Run all unit tests'
    - script: make  EXTRA_FLAGS=-Werror all
      displayName: 'Build all official targets'
    - script: mkdir release; cp obj/*.hex release/
      displayName: 'Copy artifacts'
    - task: PublishPipelineArtifact@1
      displayName: 'Publish Linux release'
      inputs: 
        artifactName: betaflight
        targetPath: '$(System.DefaultWorkingDirectory)/release/'

- stage: Release
  jobs:
  - job: Release
    pool:
      vmImage: '$(vmImage)'
    steps:
    - task: DownloadPipelineArtifact@2
      inputs:
        buildType: 'current'
        targetPath: '$(Pipeline.Workspace)'
    - powershell: Write-Output ("##vso[task.setvariable variable=today;]$(Get-Date -Format yyyyMMdd)")
    - task: GitHubReleasePublish@1
      inputs:
        githubEndpoint: '$(endpoint)'
        manuallySetRepository: true
        githubOwner: '$(owner)'
        githubRepositoryName: '$(repoName)'
        githubTag: $(today).$(Build.Repository.Name).$(Build.SourceBranchName)
        githubReleaseTitle: '$(Build.Repository.Name): $(Build.SourceBranchName) ($(today))'
        githubReleaseNotes: |+
          $(releaseNotes)

          ### Repository:
          $(Build.Repository.Name) ([link]($(Build.Repository.Uri)))

          ### Pull request branch:
          $(Build.SourceBranchName) ([link]($(Build.Repository.Uri)/tree/$(Build.SourceBranchName)))

          ### Author:
          $(Build.RequestedFor)

          ### Latest changeset:
          $(Build.SourceVersion) ([link]($(Build.Repository.Uri)/commit/$(Build.SourceVersion)))

          ### Changes:
          $(Build.SourceVersionMessage)

          ### Debug:
          Build.SourceBranchName
          $(Build.SourceBranchName)
          Build.Repository.Name
          $(Build.Repository.Name)
          Build.Repository.ID
          $(Build.Repository.ID)
          Build.Repository.Uri
          $(Build.Repository.Uri)
          Build.RequestedFor
          $(Build.RequestedFor)
          Build.QueuedBy
          $(Build.QueuedBy)
        githubReleaseDraft: false
        githubReleasePrerelease: false
        githubIgnoreAssets: false
        githubReleaseAsset: |
          $(Pipeline.Workspace)/betaflight/**
        githubReuseRelease: true
        githubReuseDraftOnly: false
        githubSkipDuplicatedAssets: false
        githubEditRelease: true
        githubDeleteEmptyTag: false
