name: Native JNI S3 TensorRT

on:
  workflow_dispatch:

permissions:
  id-token: write
  contents: read

jobs:
  build-tensorrt-jni-linux:
    runs-on: ubuntu-latest
    container: nvcr.io/nvidia/tensorrt:22.07-py3
    steps:
      - name: Install Environment
        run: pip3 install awscli --upgrade
      - uses: actions/checkout@v3
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          distribution: 'corretto'
          java-version: 17
      - uses: actions/cache@v3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('*/build.gradle.kts', 'engines/**/build.gradle.kts', 'extensions/**/build.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - name: Release JNI prep
        run: ./gradlew :engines:tensorrt:compileJNI
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::425969335547:role/djl-ci-publish-role
          aws-region: us-east-2
      - name: Copy files to S3 with the AWS CLI
        run: |
          TRT_VERSION="$(awk -F '=' '/tensorrt/ {gsub(/ ?"/, "", $2); print $2}' gradle/libs.versions.toml)"
          aws s3 sync engines/tensorrt/jnilib s3://djl-ai/publish/tensorrt/${TRT_VERSION}/jnilib
          aws cloudfront create-invalidation --distribution-id E371VB8JQ6NRVY --paths "/tensorrt/${TRT_VERSION}/jnilib*"
