name: AWS Marketplace Publisher
on:
  workflow_dispatch:
    inputs:
      KafkaUIInfraBranch:
        description: 'Branch name of Kafka-UI-Infra repo, build commands will be executed from this branch'
        required: true
        default: 'master'
      KafkaUIReleaseVersion:
        description: 'Version of KafkaUI'
        required: true
        default: '0.3.2'
      PublishOnMarketplace:
        description: 'If set to true, the request to update AWS Server product version will be raised'
        required: true
        default: false
        type: boolean

jobs:
  build-ami:
    name: Build AMI
    runs-on: ubuntu-latest
    steps:
      - name: Clone infra repo
        run: |
          echo "Cloning repo..."
          git clone https://kafka-ui-infra:${{ secrets.KAFKA_UI_INFRA_TOKEN }}@gitlab.provectus.com/provectus-internals/kafka-ui-infra.git --branch ${{ github.event.inputs.KafkaUIInfraBranch }}
          echo "Cd to packer DIR..."
          cd kafka-ui-infra/ami
          echo "WORK_DIR=$(pwd)" >> $GITHUB_ENV
          echo "Packer will be triggered in this dir $WORK_DIR"

      - name: Configure AWS credentials for Kafka-UI account
        uses: aws-actions/configure-aws-credentials@v2
        with:
          aws-access-key-id: ${{ secrets.AWS_AMI_PUBLISH_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_AMI_PUBLISH_KEY_SECRET }}
          aws-region: us-east-1

      # validate templates
      - name: Validate Template
        uses: hashicorp/packer-github-actions@master
        with:
          command: validate
          arguments: -syntax-only
          target: kafka-ui-infra/ami/kafka-ui.pkr.hcl

      # build artifact
      - name: Build Artifact
        uses: hashicorp/packer-github-actions@master
        with:
          command: build
          arguments: "-color=false -on-error=abort -var=kafka_ui_release_version=${{ github.event.inputs.KafkaUIReleaseVersion }}"
          target: kafka-ui.pkr.hcl
          working_directory: ${{ env.WORK_DIR }}
        env:
          PACKER_LOG: 1

      # add fresh AMI to AWS Marketplace
      - name: Publish Artifact at Marketplace
        if: ${{ github.event.inputs.PublishOnMarketplace == 'true' }}
        env:
          PRODUCT_ID: ${{ secrets.AWS_SERVER_PRODUCT_ID }}
          RELEASE_VERSION: "${{ github.event.inputs.KafkaUIReleaseVersion }}"
          RELEASE_NOTES: "https://github.com/provectus/kafka-ui/releases/tag/v${{ github.event.inputs.KafkaUIReleaseVersion }}"
          MP_ROLE_ARN: ${{ secrets.AWS_MARKETPLACE_AMI_ACCESS_ROLE }} # https://docs.aws.amazon.com/marketplace/latest/userguide/ami-single-ami-products.html#single-ami-marketplace-ami-access
          AMI_OS_VERSION: "amzn2-ami-kernel-5.10-hvm-*-x86_64-gp2"
        run: |
          set -x
          pwd
          ls -la kafka-ui-infra/ami
          echo $WORK_DIR/manifest.json
          export AMI_ID=$(jq -r '.builds[-1].artifact_id' kafka-ui-infra/ami/manifest.json | cut -d ":" -f2)
          /bin/bash kafka-ui-infra/aws-marketplace/prepare_changeset.sh > changeset.json
          aws marketplace-catalog start-change-set \
            --catalog "AWSMarketplace" \
            --change-set "$(cat changeset.json)"
