version: 0.2
#this buildspec assumes the aws-common-runtime/ubuntu-16.04 image
# This job is responsible for artifacting the JAR which will have all of the other shared libs stuffed
# into it once all platforms are built and artifacted
phases:
  install:
    commands:
      - sudo add-apt-repository ppa:openjdk-r/ppa
      - sudo apt-get update -y
      - sudo apt-get install openjdk-11-jdk-headless maven wget unzip -y -f
      # install android sdk
      - wget --quiet https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip
      - export ANDROID_SDK_ROOT=$CODEBUILD_SRC_DIR/android-sdk
      - mkdir -p $ANDROID_SDK_ROOT/cmdline-tools
      - unzip commandlinetools-linux-7583922_latest.zip -d $ANDROID_SDK_ROOT/cmdline-tools
      # This weird path needed for cmd tool to work
      - mv $ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools $ANDROID_SDK_ROOT/cmdline-tools/latest
      # install android build tools
      - echo y | $ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager --install "build-tools;30.0.3" "platforms;android-30" "ndk;21.4.7075529"

  pre_build:
    commands:
      - cd $CODEBUILD_SRC_DIR/aws-crt-java
      - export PKG_VERSION=$(git describe --tags | cut -f2 -dv)
      - echo $PKG_VERSION
      - mkdir -p target/cmake-build/lib
      # mv all the platform-specific jars to target/
      - aws s3 cp --recursive s3://aws-crt-java-pipeline/v${PKG_VERSION}/jar $CODEBUILD_SRC_DIR/aws-crt-java/target/
      - mv $CODEBUILD_SRC_DIR_linux_x64/dist/aws-crt-1.0.0-SNAPSHOT-linux-x86_64.jar $CODEBUILD_SRC_DIR/aws-crt-java/target/
      - mv $CODEBUILD_SRC_DIR_linux_x86/dist/aws-crt-1.0.0-SNAPSHOT-linux-x86_32.jar $CODEBUILD_SRC_DIR/aws-crt-java/target/
      # cp all the shared libs to cmake-build
      - aws s3 cp --recursive s3://aws-crt-java-pipeline/v${PKG_VERSION}/lib $CODEBUILD_SRC_DIR/aws-crt-java/target/cmake-build/lib
      - cp -rv $CODEBUILD_SRC_DIR_linux_x64/dist/* $CODEBUILD_SRC_DIR/aws-crt-java/target/cmake-build/
      - cp -rv $CODEBUILD_SRC_DIR_linux_x86/dist/* $CODEBUILD_SRC_DIR/aws-crt-java/target/cmake-build/
      - ls -alR $CODEBUILD_SRC_DIR/aws-crt-java/target
      # install settings.xml to ~/.m2/settings.xml
      - mkdir -p $HOME/.m2
      - aws s3 cp s3://code-sharing-aws-crt/aws-crt-java.settings.xml $HOME/.m2/settings.xml
      # import gpg key
      - aws s3 cp s3://code-sharing-aws-crt/aws-sdk-common-runtime.key.asc /tmp/aws-sdk-common-runtime.key.asc
      - gpg --import /tmp/aws-sdk-common-runtime.key.asc
      - export GPG_PASSPHRASE=$(aws --query "SecretString" secretsmanager get-secret-value --secret-id aws-sdk-common-runtime.key.asc/password | cut -f2 -d":" | sed -e 's/[\\\"\}]//g')
      - export ST_PASSWORD=$(aws --query "SecretString" secretsmanager get-secret-value --secret-id Sonatype/JIRA/Password | cut -f2 -d":" | sed -e 's/[\\\"\}]//g')
      # Use the password from secret manager to update the settings
      - sed -i 's/password-to-replace/'"$ST_PASSWORD"'/g' $HOME/.m2/settings.xml
  build:
    commands:
      - cd $CODEBUILD_SRC_DIR/aws-crt-java
      # update the version to match the git tag, make a snapshot version we can test
      - export DEPLOY_VERSION=${PKG_VERSION}-SNAPSHOT
      - mvn -B versions:set -DnewVersion=${DEPLOY_VERSION}
      # do a full build/deploy, but skip shared libs and tests, since the shared libs were artifacts above
      - mvn -B deploy -Prelease -Dshared-lib.skip=true -Dmaven.test.skip=true -Dgpg.passphrase=$GPG_PASSPHRASE
      - bash ./codebuild/cd/deploy-platform-specific-jars.sh
      # update the version to match the git tag, make a staging release which we will release once snapshot testing passes
      - export DEPLOY_VERSION=${PKG_VERSION}
      - mvn -B versions:set -DnewVersion=${DEPLOY_VERSION}
      - mvn -B deploy -Prelease -Dshared-lib.skip=true -Dmaven.test.skip=true -Dgpg.passphrase=$GPG_PASSPHRASE | tee /tmp/deploy.log
      - export STAGING_REPO_ID=$(cat /tmp/deploy.log | grep "Created staging repository with ID" | cut -d\" -f2 | tee /tmp/repositoryId.txt)
      # upload the platform specific jars to staging repo and close the staging repo.
      - bash ./codebuild/cd/deploy-platform-specific-jars.sh
      - mvn nexus-staging:close -Prelease -DstagingRepositoryId=$STAGING_REPO_ID
      # deploy android
      - ./codebuild/cd/deploy-snapshot-android.sh
  post_build:
    commands:

artifacts:
  discard-paths: yes
  files:
    - $CODEBUILD_SRC_DIR/aws-crt-java/target/aws-crt-*.jar
    - $CODEBUILD_SRC_DIR/aws-crt-java/target/aws-crt-*.asc
    - /tmp/repositoryId.txt
    - /tmp/android_repositoryId.txt

cache:
  paths:
    - "/root/.m2/**/*"
