trigger:
- release

pr: none

pool:
  vmImage: "ubuntu-latest"

variables:
  VERSION:

steps:
  # Checks if the release notes file is populated. Exits if it is not.
  - bash: |
      if [ -n "$(cmp RELEASE_NOTES.md RELEASE_NOTES.template.md)" ]
      then
          echo "RELEASE_NOTES.md has changed, proceeding to next step"
      else
          echo "Your RELEASE_NOTES.md file is unchanged. Please provide release notes before creating a release."
          exit 1
      fi

  # This task pulls the <version> value from the org.hl7.fhir.r5 project pom.xml file. All modules are released as 
  # the same version, at the same time, as defined in the root level pom.xml.
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        [xml]$pomXml = Get-Content -Path .\pom.xml
        # version
        Write-Host $pomXml.project.version
        $version=$pomXml.project.version
        Write-Host "##vso[task.setvariable variable=version]$version"

  # Prints out the build version, for debugging purposes
  - bash: echo Pulled version from pom.xml => $(version)

  # Azure pipelines cannot pass variables between pipelines, but it can pass files, so we
  # pass the build id (ex: 1.1.13-SNAPSHOT) as a string in a file. 
  # This is used in the release pipeline, so we create it here.
  - bash: |
      echo $(version)
      VERSION=$(version)
      echo "$VERSION" > $(System.DefaultWorkingDirectory)/VERSION

  # Copies the VERSION file containing the build id (ex: 1.1.13-SNAPSHOT) to the staging directory
  # This is done for release versions only.
  - task: CopyFiles@2
    displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
    inputs:
      SourceFolder: '$(System.Defaultworkingdirectory)'
      Contents: "$(System.DefaultWorkingDirectory)/VERSION"
      TargetFolder: '$(build.artifactstagingdirectory)'

  # Runs 'mvn clean package'
  - task: Maven@3
    inputs:
      mavenPomFile: 'pom.xml'
      mavenOptions: '-Xmx3072m'
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '1.11'
      jdkArchitectureOption: 'x64'
      publishJUnitResults: true
      testResultsFiles: '**/surefire-reports/TEST-*.xml'
      goals: 'package'
    
  # Publishes the built Validator jar to build artifacts. Primarily for testing and debugging builds.
  - task: PublishPipelineArtifact@1
    displayName: 'Publish Validator jar'
    inputs:
      targetPath: "$(System.DefaultWorkingDirectory)/org.hl7.fhir.validation/target/org.hl7.fhir.validation-$(version).jar"
      artifactName: Validator

  # Publishes the files we've moved into the staging directory, so they can be accessed by the 
  # release pipeline.
  - task: PublishBuildArtifacts@1
    displayName: 'Publish Build Artifacts'
    inputs:
      PathtoPublish: '$(build.artifactstagingdirectory)'