trigger:
  branches:
    include:
    - refs/tags/v*  
 
pool:
  name: Azure-IoT-EdgeExperience-1ES-Hosted-Linux
  demands:
    - ImageOverride -equals MMSUbuntu18.04TLS

steps:
- task: UsePythonVersion@0
  displayName:  "Install Python"
  inputs:
    versionSpec: '3.7'
    addToPath: true
    architecture: 'x64'

- task: Bash@3
  displayName: "Install Python packages"
  inputs:
    targetType: 'inline'
    script: |
      pip install tox
      pip install twine
      sudo apt-get install gcc libpq-dev -y
      sudo apt-get install python-dev  python-pip -y
      sudo apt-get install python3-dev python3-pip python3-venv python3-wheel -y
      pip3 install wheel

- task: Bash@3
  displayName: "Update AIKey"
  inputs:
    targetType: 'inline'
    script: 'sed -i "/__AIkey__/c __AIkey__ = ''$(AIKEY)''" ./iotedgedev/__init__.py'

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      if [[ $(Build.SourceBranch) =~ ^refs/tags/v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      echo "##vso[task.setvariable variable=WHEEL_PUBLISH_FLAG;]true"
      fi
      VERSION=$(cat ./iotedgedev/__init__.py | grep '__version__' | grep -oP "'\K[^']+")
      echo "##vso[task.setvariable variable=VERSION;]$VERSION"

- task: Bash@3
  displayName: "Build wheel"
  inputs:
    targetType: 'inline'
    script: |
      python setup.py bdist_wheel
      pip install dist/iotedgedev-${VERSION}-py3-none-any.whl && iotedgedev --version

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(Build.SourcesDirectory)'
    archiveFile: '$(Build.ArtifactStagingDirectory)/iotedgedev-$(VERSION)-win32-ia32.zip'

- task: Bash@3
  displayName: "Revert AIKey to Original"
  inputs:
    targetType: 'inline'
    script: 'sed -i "/__AIkey__/c __AIkey__ = ''$(TEST_AIKEY)''" ./iotedgedev/__init__.py'

- task: AzureArtifacts.manifest-generator-task.manifest-generator-task.ManifestGeneratorTask@0
  displayName: 'SBOM Generation Task'
  inputs:
    BuildDropPath: '$(Build.SourcesDirectory)/dist'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.SourcesDirectory)/dist'
    ArtifactName: 'drop'
    publishLocation: 'Container'

- task: CopyFiles@2
  inputs:
    SourceFolder: '$(Build.SourcesDirectory)/dist/'
    Contents: '*'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: Bash@3
  displayName: 'Upload to Blob'
  condition: and(succeeded(), eq(variables['WHEEL_PUBLISH_FLAG'], 'true'))
  inputs:
    targetType: 'inline'
    script: |
      export AZCOPY_SPA_CLIENT_SECRET=$(AZURESDKPARTNERDROPS-SERVICE-PRINCIPAL-KEY)
      azcopy login --service-principal --application-id $(APPID)
      azcopy copy "$(Build.ArtifactStagingDirectory)/*"  "https://azuresdkpartnerdrops.blob.core.windows.net/drops/azure-iot-edge-tools-iotedgedev/python/$(VERSION)/"

- task: GitHubRelease@0
  condition: and(succeeded(), eq(variables['WHEEL_PUBLISH_FLAG'], 'true'))
  inputs:
    gitHubConnection: 'github.com_marianan'
    repositoryName: '$(Build.Repository.Name)'
    action: 'create'
    target: '$(Build.SourceVersion)'
    tagSource: 'manual'
    tag: '$(Build.SourceBranch)'
    releaseNotesSource: 'input'
    isPreRelease: true
    addChangeLog: false

- task: Bash@3
  displayName: "Build Docker image"
  inputs:
    targetType: 'inline'
    script: './build-docker.sh iotedgetoolscontainerregistry.azurecr.io/public/iotedge/iotedgedev linux'
    workingDirectory: 'docker/tool'

- task: Bash@3
  displayName: "Test local Docker image"
  inputs:
    targetType: 'inline'
    script: |
       PACKAGEVERSION=$(cat ../../iotedgedev/__init__.py | grep '__version__' | grep -oP "'\K[^']+")
       echo "##vso[task.setvariable variable=VERSION]$PACKAGEVERSION"
       mkdir /home/vsts/iotedge
       docker run -v /var/run/docker.sock:/var/run/docker.sock -v /home/vsts/iotedge:/home/iotedge iotedgetoolscontainerregistry.azurecr.io/public/iotedge/iotedgedev:${PACKAGEVERSION}
    workingDirectory: 'docker/tool'

- task: Docker@2
  condition: and(succeeded(), eq(variables['WHEEL_PUBLISH_FLAG'], 'true'))
  displayName: "Push docker image to acr"
  inputs:
    containerRegistry: 'IoTEdgeDev-Docker'
    repository: 'public/iotedge/iotedgedev'
    command: 'push'
    tags: |
      $(VERSION)
      latest

- task: Bash@3
  condition: and(succeeded(), eq(variables['WHEEL_PUBLISH_FLAG'], 'true'))
  displayName: "Test last uploaded Docker image"
  inputs:
    targetType: 'inline'
    script: |
      docker pull mcr.microsoft.com/iotedge/iotedgedev
      docker run  mcr.microsoft.com/iotedge/iotedgedev 
      iotedgedev --version    
    workingDirectory: 'docker/tool'
