image: atlassian/default-image:3

caches:
  - name: flutter-cache
    key: $BITBUCKET_COMMIT
    paths:
      - .flutter
      - .dart_tool

definitions:
  steps:
    - step: &build
        name: Build     
        script:
          - echo "Installing Flutter SDK..."
          - apt-get update && apt-get install -y wget unzip
          - if [ ! -d ".flutter" ]; then
                wget https://mirrors.cloud.tencent.com/flutter/flutter_infra/releases/stable/linux/flutter_linux_3.10.0-stable.tar.xz;
                tar xf flutter_linux_3.10.0-stable.tar.xz;
                mv flutter .flutter;
              fi
          - export PATH="$PATH:`pwd`/.flutter/bin"
          - flutter channel stable
          - flutter upgrade
          - flutter pub get
          - echo "Building Flutter web app..."
          - flutter build web --release         
    
    - step: &deploy-prod
        name: Deploy and Invalidate Cache
        oidc: true
        deployment: production
        script:
          - echo "Deploying to S3..."
          - pipe: atlassian/aws-s3-deploy:1.1.0
            variables:
              AWS_OIDC_ROLE_ARN: $AWS_PROD_ARN
              AWS_DEFAULT_REGION: $AWS_REGION
              S3_BUCKET: $AWS_S3_BUCKET
              LOCAL_PATH: './build/web'
          - echo "Invalidating Cloudfront Cache..."
          - pipe: atlassian/aws-cloudfront-invalidate:0.10.1
            variables:
              AWS_OIDC_ROLE_ARN: $AWS_PROD_ARN
              AWS_DEFAULT_REGION: $AWS_REGION
              DISTRIBUTION_ID: $DISTRIBUTION_ID

pipelines:
  branches:    
    main:        
      # - step: *build   
      - step: *deploy-prod