image: shiplogic/ci-wp-plugin:node18

variables:
  GIT_SUBMODULE_STRATEGY: recursive

stages:
  - deploy

deploy:
  stage: deploy
  before_script:
    - AWS_ACCESS_KEY_ID_KEY=$(echo "$CI_COMMIT_BRANCH"_"AWS_ACCESS_KEY_ID")
    - AWS_ACCESS_KEY_ID=$(eval echo -e "\$$AWS_ACCESS_KEY_ID_KEY")
    - AWS_SECRET_ACCESS_KEY_KEY=$(echo "$CI_COMMIT_BRANCH"_"AWS_SECRET_ACCESS_KEY")
    - AWS_SECRET_ACCESS_KEY=$(eval echo -e "\$$AWS_SECRET_ACCESS_KEY_KEY")
    - export AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY
  script:
    - ./make-zip.sh
    - aws s3 cp bobgo-magento-plugin.zip s3://bobgo-s3-magento-plugin/ --region=af-south-1
  rules:
    - if: '$CI_COMMIT_BRANCH == "dev" && $CI_COMMIT_TAG == null'
      when: always

prod_deploy:
  stage: deploy
  before_script:
    - AWS_ACCESS_KEY_ID_KEY=$(echo "$CI_COMMIT_BRANCH"_"AWS_ACCESS_KEY_ID")
    - AWS_ACCESS_KEY_ID=$(eval echo -e "\$$AWS_ACCESS_KEY_ID_KEY")
    - AWS_SECRET_ACCESS_KEY_KEY=$(echo "$CI_COMMIT_BRANCH"_"AWS_SECRET_ACCESS_KEY")
    - AWS_SECRET_ACCESS_KEY=$(eval echo -e "\$$AWS_SECRET_ACCESS_KEY_KEY")
    - export AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY
    # Fetch the prod branch
    - git fetch origin prod
    # Check if the tagged commit is from the prod branch
    - if git merge-base --is-ancestor "${CI_COMMIT_SHA}" origin/prod; then
      echo "Tag ${CI_COMMIT_TAG} is from prod branch.";
      else
      echo "Tag ${CI_COMMIT_TAG} is not from prod branch.";
      exit 1;
      fi
  script:
    - ./make-zip.sh
    - aws s3 cp bobgo-magento-plugin.zip s3://bobgo-s3-magento-plugin-prod/ --region=af-south-1
  allow_failure: false
  rules:
    - if: '$CI_COMMIT_TAG'
      when: on_success
    - when: never
