name: Build and Release SQL-ODBC
# This workflow is triggered on creating tags to master or an opendistro release branch
on:
  push:
    tags:
      - 'v*'

env:
  CI_OUTPUT_PATH: "sql-odbc/ci-output"
  ODBC_LIB_PATH: "./build/odbc/lib"
  ODBC_BIN_PATH: "./build/odbc/bin"
  ODBC_BUILD_PATH: "./build/odbc/build"
  AWS_SDK_INSTALL_PATH: "./build/aws-sdk/install"
  PLUGIN_NAME: opendistro-sql-odbc
  OD_VERSION: 1.13.0.0

jobs:
  build-mac:
    runs-on: macos-latest
    defaults:
      run:
        working-directory: sql-odbc
    steps:
      - uses: actions/checkout@v1
      - name: run-cppcheck
        run: |
          brew install cppcheck
          sh run_cppcheck.sh
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_STAGING_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_STAGING_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: upload-cppcheck-results
        if: failure()
        uses: actions/upload-artifact@v1
        with:
          name: cppcheck-results
          path: sql-odbc/cppcheck-results.log
      - name: get-dependencies
        run: |
          brew unlink unixodbc
          brew install curl
          brew install cmake
          brew install libiodbc
      - name: configure-and-build-driver
        run: |
          ./build_mac_release64.sh
      - name: build-installer
        if: success()
        run: |
          cd cmake-build64
          cmake ../src
          make
          cpack .
          cd ..
      - name: create-output
        if: success()
        run: |
          mkdir build-output
          mkdir test-output
          mkdir installer
          cp ./build/odbc/lib/*.dylib build-output/
          cp ./build/odbc/lib/*.a build-output/
          cp ./cmake-build64/*.pkg installer/
      # cp $(ls -d bin64/* | grep -v "\.") build
      - name: upload-build
        if: success()
        uses: actions/upload-artifact@v1
        with:
          name: mac64-build
          path: sql-odbc/build-output
      - name: upload-installer
        if: success()
        uses: actions/upload-artifact@v1
        with:
          name: mac64-installer
          path: sql-odbc/installer
      - name: upload-artifacts-s3
        if: success()
        run: |
          cd installer

          pkg=`ls -1t *.pkg | grep "Open Distro for Elasticsearch SQL ODBC Driver" | head -1`
          mv "$pkg" "${{ env.PLUGIN_NAME }}-${{ env.OD_VERSION }}-macos-x64.pkg"
          pkg=`ls -1t *.pkg | grep "${{ env.PLUGIN_NAME }}-${{ env.OD_VERSION }}-macos-x64.pkg" | head -1`

          # Inject the build number before the suffix
          pkg_outfile=`basename ${pkg%.pkg}-build-${GITHUB_RUN_NUMBER}.pkg`

          s3_prefix="s3://staging.artifacts.opendistroforelasticsearch.amazon.com/snapshots/elasticsearch-clients/sql-odbc/"

          echo "Copying ${pkg} to ${s3_prefix}${pkg_outfile}"
          aws s3 cp --quiet $pkg ${s3_prefix}${pkg_outfile}

  build-windows32:
    runs-on: windows-latest
    defaults:
      run:
        working-directory: sql-odbc
    steps:
      - uses: actions/checkout@v1
      - name: Get specific version CMake, v3.18.3
        uses: lukka/get-cmake@v3.18.3
      - name: add-msbuild-to-path
        uses: microsoft/setup-msbuild@v1.0.2
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_STAGING_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_STAGING_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: configure-and-build-driver
        run: |
          .\build_win_release32.ps1
      - name: build-installer
        if: success()
        run: |
          .\scripts\build_installer.ps1 Release Win32 .\src $Env:ODBC_BUILD_PATH $Env:AWS_SDK_INSTALL_PATH
      - name: prepare-output
        if: always()
        run: |
          .\scripts\prepare_ci_output.ps1 $Env:ODBC_BIN_PATH $Env:ODBC_LIB_PATH $Env:ODBC_BUILD_PATH
      - name: upload-build
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: windows32-build
          path: sql-odbc/ci-output/build
      - name: upload-installer
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: windows32-installer
          path: sql-odbc/ci-output/installer
      - name: upload-artifacts-s3
        if: success()
        shell: bash
        run: |
          cd ci-output/installer
          
          msi=`ls -1t *.msi | grep "Open Distro for Elasticsearch SQL ODBC Driver" | head -1`
          mv "$msi" "${{ env.PLUGIN_NAME }}-${{ env.OD_VERSION }}-windows-x86.msi"
          msi=`ls -1t *.msi | grep "${{ env.PLUGIN_NAME }}-${{ env.OD_VERSION }}-windows-x86.msi" | head -1`

          # Inject the build number before the suffix
          msi_outfile=`basename ${msi%.msi}-build-${GITHUB_RUN_NUMBER}.msi`

          s3_prefix="s3://staging.artifacts.opendistroforelasticsearch.amazon.com/snapshots/elasticsearch-clients/sql-odbc/"

          echo "Copying ${msi} to ${s3_prefix}${msi_outfile}"
          aws s3 cp --quiet $msi ${s3_prefix}${msi_outfile}

  build-windows64:
    runs-on: windows-latest
    defaults:
      run:
        working-directory: sql-odbc
    steps:
      - uses: actions/checkout@v1
      - name: Get specific version CMake, v3.18.3
        uses: lukka/get-cmake@v3.18.3
      - name: add-msbuild-to-path
        uses: microsoft/setup-msbuild@v1.0.2
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_STAGING_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_STAGING_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: configure-and-build-driver
        run: |
          .\build_win_release64.ps1
      - name: build-installer
        if: success()
        run: |
          .\scripts\build_installer.ps1 Release x64 .\src $Env:ODBC_BUILD_PATH $Env:AWS_SDK_INSTALL_PATH
      - name: prepare-output
        if: always()
        run: |
          .\scripts\prepare_ci_output.ps1 $Env:ODBC_BIN_PATH $Env:ODBC_LIB_PATH $Env:ODBC_BUILD_PATH
      - name: upload-build
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: windows64-build
          path: sql-odbc/ci-output/build
      - name: upload-installer
        if: always()
        uses: actions/upload-artifact@v1
        with:
          name: windows64-installer
          path: sql-odbc/ci-output/installer
      - name: upload-artifacts-s3
        if: success()
        shell: bash
        run: |
          cd ci-output/installer
          
          msi=`ls -1t *.msi | grep "Open Distro for Elasticsearch SQL ODBC Driver" | head -1`
          mv "$msi" "${{ env.PLUGIN_NAME }}-${{ env.OD_VERSION }}-windows-x64.msi"
          msi=`ls -1t *.msi | grep "${{ env.PLUGIN_NAME }}-${{ env.OD_VERSION }}-windows-x64.msi" | head -1`
          
          # Inject the build number before the suffix
          msi_outfile=`basename ${msi%.msi}-build-${GITHUB_RUN_NUMBER}.msi`

          s3_prefix="s3://staging.artifacts.opendistroforelasticsearch.amazon.com/snapshots/elasticsearch-clients/sql-odbc/"

          echo "Copying ${msi} to ${s3_prefix}${msi_outfile}"
          aws s3 cp --quiet $msi ${s3_prefix}${msi_outfile}