From 72f8a3c123bfd03d0ea4f14ebede12f8ab9f7124 Mon Sep 17 00:00:00 2001
From: Adam Douglass <douglass@malloc.ca>
Date: Fri, 3 Apr 2020 15:54:01 -0400
Subject: [PATCH] Azure Pipeline: add azure pipelines

---
 pipelines/azure-build.yaml | 92 ++++++++++++++++++++++++++++++++++++++
 pipelines/azure-test.yaml  | 37 +++++++++++++++
 pipelines/config.yml       |  4 ++
 setup.py                   |  8 ++--
 4 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 pipelines/azure-build.yaml
 create mode 100644 pipelines/azure-test.yaml
 create mode 100644 pipelines/config.yml

diff --git a/pipelines/azure-build.yaml b/pipelines/azure-build.yaml
new file mode 100644
index 0000000..3533f9f
--- /dev/null
+++ b/pipelines/azure-build.yaml
@@ -0,0 +1,92 @@
+name: build
+
+trigger:
+  tags:
+    include: ["v*"]
+
+pool:
+  vmImage: 'ubuntu-18.04'
+
+stages:
+- stage: build
+  jobs:
+  - job: build_package
+    displayName: Build Package
+    steps:
+    - task: UsePythonVersion@0
+      displayName: Set python version
+      inputs: {versionSpec: 3.8}
+    - script: |
+        sudo env "PATH=$PATH" python -m pip install --no-cache-dir -U wheel cython pip
+        python setup.py bdist_wheel
+    - publish: $(System.DefaultWorkingDirectory)/dist/
+      artifact: dist
+    - publish: $(System.DefaultWorkingDirectory)/pipelines/
+      artifact: pipelines
+    - publish: $(System.DefaultWorkingDirectory)/test/
+      artifact: test
+#- stage: test
+#  jobs:
+#  - job: run_test
+#    strategy:
+#      matrix:
+#        python3_7:
+#          python.version: '3.7'
+#        Python3_8:
+#          python.version: '3.8'
+#    timeoutInMinutes: 10
+#    steps:
+#    - checkout: none
+#    - task: UsePythonVersion@0
+#      displayName: Set python version
+#      inputs:
+#        versionSpec: '$(python.version)'
+#    - download: current
+#      artifact: dist
+#    - download: current
+#      artifact: pipelines
+#    - download: current
+#      artifact: test
+#    - script: |
+#        set -x  # Echo commands before they are run
+#        sudo apt-get update
+#        sudo apt-get install -y build-essential libffi-dev libfuzzy-dev python3-dev
+#        export VERSION=${BUILD_SOURCEBRANCH#"refs/tags/v"}
+#        sudo -E env "PATH=$PATH" python -m pip install --no-cache-dir -f $(Pipeline.Workspace)/dist/ "assemblyline-service-client==${VERSION}" -r $(Pipeline.Workspace)/test/requirements.txt
+#        sudo mkdir -p /etc/assemblyline/
+#        sudo mkdir -p /var/cache/assemblyline/
+#        sudo chmod a+rw /var/cache/assemblyline/
+#        sudo cp $(Pipeline.Workspace)/pipelines/config.yml /etc/assemblyline
+#      displayName: Install package
+#    - script: pytest -rsx -vv
+#      workingDirectory: $(Pipeline.Workspace)/test
+#      displayName: Test
+- stage: deploy
+  jobs:
+  - job: deploy
+    displayName: Deploy packages and containers
+    variables:
+      - group: deployment-information
+    steps:
+    - download: current
+      artifact: dist
+    - script: |
+        set -xv  # Echo commands before they are run
+        sudo env "PATH=$PATH" python -m pip install --no-cache-dir twine
+        ls dist
+        twine upload --skip-existing --repository-url $TEST_REPOSITORY_URL dist/*
+      workingDirectory: $(Pipeline.Workspace)
+      displayName: Deploy to Test PyPI
+      env:
+        TWINE_USERNAME: $(twineUsername)
+        TWINE_PASSWORD: $(twinePassword)
+    - script: |
+        set -xv  # Echo commands before they are run
+        sudo env "PATH=$PATH" python -m pip install --no-cache-dir twine
+        ls dist
+        twine upload --skip-existing dist/*
+      workingDirectory: $(Pipeline.Workspace)
+      displayName: Deploy to PyPI
+      env:
+        TWINE_USERNAME: $(twineUsername)
+        TWINE_PASSWORD: $(twinePassword)
diff --git a/pipelines/azure-test.yaml b/pipelines/azure-test.yaml
new file mode 100644
index 0000000..055f7e9
--- /dev/null
+++ b/pipelines/azure-test.yaml
@@ -0,0 +1,37 @@
+name: tests
+
+trigger: ["*"]
+pr: ["*"]
+
+pool:
+  vmImage: 'ubuntu-18.04'
+
+jobs:
+- job: run_test
+  strategy:
+    matrix:
+      python3_7:
+        python.version: '3.7'
+      Python3_8:
+        python.version: '3.8'
+
+  timeoutInMinutes: 10
+
+  steps:
+    - task: UsePythonVersion@0
+      displayName: Set python version
+      inputs:
+        versionSpec: '$(python.version)'
+    - script: |
+        sudo mkdir -p /etc/assemblyline/
+        sudo mkdir -p /var/cache/assemblyline/
+        sudo cp pipelines/config.yml /etc/assemblyline
+        sudo chmod a+rw /var/cache/assemblyline/
+        sudo apt-get update
+        sudo apt-get install -y build-essential libffi-dev libfuzzy-dev python3-dev
+        sudo env "PATH=$PATH" python -m pip install --no-cache-dir -U pip cython setuptools
+        sudo env "PATH=$PATH" python -m pip install --no-cache-dir -e .
+        sudo env "PATH=$PATH" python -m pip install --no-cache-dir -r test/requirements.txt
+      displayName: Setup environment
+    - script: python -m pytest -rsx -vv
+      displayName: Test
diff --git a/pipelines/config.yml b/pipelines/config.yml
new file mode 100644
index 0000000..b37fd76
--- /dev/null
+++ b/pipelines/config.yml
@@ -0,0 +1,4 @@
+logging:
+  log_to_console: false
+  log_to_file: false
+  log_to_syslog: false
\ No newline at end of file
diff --git a/setup.py b/setup.py
index 53e4c09..d033c3c 100644
--- a/setup.py
+++ b/setup.py
@@ -5,9 +5,11 @@
 # For development and local builds use this version number, but for real builds replace it
 # with the tag found in the environment
 package_version = "4.0.0.dev0"
-for variable_name in ['BITBUCKET_TAG']:
-    package_version = os.environ.get(variable_name, package_version)
-    package_version = package_version.lstrip('v')
+if 'BITBUCKET_TAG' in os.environ:
+    package_version = os.environ['BITBUCKET_TAG'].lstrip('v')
+elif 'BUILD_SOURCEBRANCH' in os.environ:
+    full_tag_prefix = 'refs/tags/v'
+    package_version = os.environ['BUILD_SOURCEBRANCH'][len(full_tag_prefix):]
 
 
 setup(