image: ubuntu:xenial

pipelines:
  default:
    - step:
        script:
          - export BASE_DIR="$(readlink -f .)"
          - export KASM_BUILD_ID="${RELEASE_VERSION}.${BITBUCKET_COMMIT:0:6}"
          - export S3_BUILD_DIRECTORY="kasm_desktop_rx/${BITBUCKET_COMMIT}"
          - export SANITIZED_BRANCH="$(echo $BITBUCKET_BRANCH | sed 's/\//_/g')"
          - export BUILD_FILE="kasm_desktop_rx_${SANITIZED_BRANCH}.${BITBUCKET_COMMIT:0:6}.tar.gz"

          # Remove unnecessary files
          - find ./ -maxdepth 1 -type f -name ".*" -delete
          - rm bitbucket-pipelines.yml

          - tar -czzf ${BASE_DIR}/${BUILD_FILE} ./* --transform 's,^\.,kasm_desktop_rx,'

          - apt-get update
          - apt-get install -y python-pip git curl
          - pip install boto3==1.3.0
          - git clone https://bitbucket.org/awslabs/amazon-s3-bitbucket-pipelines-python.git
          - python amazon-s3-bitbucket-pipelines-python/s3_upload.py "${S3_BUCKET}" ${BASE_DIR}/${BUILD_FILE} "${S3_BUILD_DIRECTORY}/${BUILD_FILE}"
          - export S3_URL="https://${S3_BUCKET}.s3.amazonaws.com/${S3_BUILD_DIRECTORY}/${BUILD_FILE}"
          - export BUILD_STATUS="{\"key\":\"doc\", \"state\":\"SUCCESSFUL\", \"name\":\"${BUILD_FILE}\", \"url\":\"${S3_URL}\"}"
          - curl -H "Content-Type:application/json" -X POST --user "${BB_AUTH_STRING}" -d "${BUILD_STATUS}" "https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/commit/${BITBUCKET_COMMIT}/statuses/build"