executor-machine: &executor-machine 'ubuntu-2004:2022.04.1'

orbs:
  git-shallow-clone: guitarrapc/git-shallow-clone@2.4.0

commands:
    install_yq_v3:
      description: "Install yq (v3)"
      steps:
        - run:
            name: Install yq (v3)
            command: |
              wget https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64
              sudo mv yq_linux_amd64 /usr/local/bin/yq
              echo "2cc8897984d1ab43b6799aff5eca905d37552fdf  /usr/local/bin/yq" > /tmp/checksum
              sha1sum -c /tmp/checksum
              sudo chmod +x /usr/local/bin/yq

    set_docker_gcp_mirror:
        description: "Configure GCP Docker registry mirrors"
        steps:
          - run:
              name: Set GCP Docker registry mirror
              command:   |
                # see https://cloud.google.com/container-registry/docs/pulling-cached-images#configure
                [ ! -f /etc/docker/daemon.json ] && sudo touch /etc/docker/daemon.json
                sudo yq w -jPi /etc/docker/daemon.json "registry-mirrors[+]" "https://mirror.gcr.io"
                sudo service docker restart

jobs:
    checkout_ee:
        machine:
            image: *executor-machine
        steps:
            - add_ssh_keys:
                  fingerprints:
                      - "ee:73:a1:e4:ef:57:13:9e:cf:d3:23:fb:e2:c1:d7:92"
            - run:
                  name: Clone EE branch if it exists, or 7.0 otherwise with the SSH key that we added above
                  command: |
                      export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_ee73a1e4ef57139ecfd323fbe2c1d792 -o UserKnownHostsFile=~/.ssh/known_hosts -o IdentitiesOnly=Yes'
                      ENTERPRISE_BRANCH=$(if [[ -z $(git ls-remote --exit-code --heads git@github.com:akeneo/pim-enterprise-dev.git ${CIRCLE_BRANCH}) ]]; then echo "7.0"; else echo ${CIRCLE_BRANCH}; fi)
                      git clone --depth=1 --single-branch --branch $ENTERPRISE_BRANCH git@github.com:akeneo/pim-enterprise-dev.git /home/circleci/project
                      cd /home/circleci/project
                      echo "Current commit: $(git rev-parse --short HEAD)"
            - run:
                  name: BIG COMMERCE CONNECTOR - Checkout BigCommerce Connector
                  command: |
                      mkdir -p tmp
                      export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_ee73a1e4ef57139ecfd323fbe2c1d792 -o UserKnownHostsFile=~/.ssh/known_hosts -o IdentitiesOnly=Yes'
                      CONNECTOR_BC_BRANCH=$(git -c 'versionsort.suffix=-' ls-remote --exit-code --refs --sort='version:refname' --tags git@github.com:akeneo/bigcommerce-connector.git 'v*' | tail -n1 | cut -d'/' -f3)
                      git clone --depth=1 --single-branch --branch $CONNECTOR_BC_BRANCH git@github.com:akeneo/bigcommerce-connector.git tmp/build-connector
            - run:
                  name: Update composer.json if same branch exists in pim-onboader for EE
                  command: |
                      export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_ee73a1e4ef57139ecfd323fbe2c1d792 -o UserKnownHostsFile=~/.ssh/known_hosts -o IdentitiesOnly=Yes'
                      git ls-remote --exit-code --heads git@github.com:akeneo/pim-onboarder.git ${CIRCLE_BRANCH} && \
                      sed -i "s#akeneo/pim-onboarder\": \"^7.0.0#akeneo/pim-onboarder\": \"dev-${CIRCLE_BRANCH}#" composer.json || \
                      echo "No pim-onboarder branch $CIRCLE_BRANCH found. I don't touch the dependencies."
            - run:
                  name: Require proper dev CE branch
                  command: |
                      sed -i "s#akeneo/pim-community-dev\": \"7.0.x-dev@dev#akeneo/pim-community-dev\": \"dev-${CIRCLE_BRANCH}#" composer.json
            - run:
                  name: Checkout CE
                  command: git clone --depth 1 https://github.com/akeneo/pim-community-dev vendor/akeneo/pim-community-dev -b ${CIRCLE_BRANCH}
            - install_yq_v3
            - run:
                  name: Remove MySQL port translation (see BH-664)
                  command: yq delete --inplace docker-compose.yml services.mysql.ports
            - run:
                  name: Persist default IMAGE_TAG to be the repo last commit SHA1
                  command: |
                      echo export IMAGE_TAG_SHORTED=$(echo $CIRCLE_SHA1 | cut -c -7) >> persisted_env_vars
                      echo export IMAGE_TAG=$CIRCLE_SHA1 >> persisted_env_vars
            - persist_to_workspace:
                  root: ~/
                  paths:
                      - project

    checkout_ce:
        machine:
            image: *executor-machine
        steps:
            - git-shallow-clone/checkout
            - install_yq_v3
            - run:
                  name: Remove MySQL port translation (see BH-664)
                  command: yq delete --inplace docker-compose.yml services.mysql.ports
            -   run:
                    name: Setup missing directories
                    command: mkdir -p var/tests/phpspec var/tests/csfixer var/logs var/tests/screenshots ~/.composer
            - persist_to_workspace:
                  root: ~/
                  paths:
                      - project

    build_dev:
        parameters:
            is_ee_built:
                type: boolean
                default: true
            path_to_front_packages:
                type: string
                default: front-packages
        machine:
            image: *executor-machine
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Copy docker-compose.override.yml.dist
                  command: cp .circleci/docker-compose.override.yml.dist docker-compose.override.yml
            - when:
                  condition: << parameters.is_ee_built >>

                  steps:
                      - run:
                            name: Creating hash key for PHP Docker image
                            command: |
                                find Dockerfile docker/ -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/php-docker-image.hash
                                date +%F >> ~/php-docker-image.hash
                      - restore_cache:
                            name: Restore PHP docker image cache
                            key: php-docker-image-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/php-docker-image.hash" }}
                      - run:
                            name: Build the latest EE Docker images
                            command: |
                                ls php-pim-image.tar && docker load -i php-pim-image.tar
                                ls php-pim-image.tar || make php-image-dev
                                ls php-pim-image.tar || docker save -o php-pim-image.tar akeneo/pim-dev/php:8.1
                      - save_cache:
                            name: Save PHP docker image cache
                            key: php-docker-image-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/php-docker-image.hash" }}
                            paths:
                                - php-pim-image.tar
            - unless:
                  condition: << parameters.is_ee_built >>
                  steps:
                      - run:
                            name: Save the CE image as a tar
                            command: |
                                docker pull akeneo/pim-php-dev:8.1
                                docker save -o php-pim-image.tar akeneo/pim-php-dev:8.1
            - run:
                  name: Setup tests results folder and log folder
                  command: mkdir -p var/tests/phpspec var/tests/csfixer var/logs var/tests/screenshots ~/.cache/yarn ~/.cache/Cypress ~/.composer
            - run:
                  name: Creating cache key for JS and PHP dependencies
                  command: |
                      cat yarn.lock > ~/front-dependency.hash && date +%F >> ~/front-dependency.hash
                      cat composer.json > ~/back-dependency.hash && date +%F >> ~/back-dependency.hash
            - restore_cache:
                  name: Restore cache - yarn and Cypress dependency cache
                  keys:
                      - frontend-dependency-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/front-dependency.hash" }}
            - restore_cache:
                  name: Restore cache - composer dependency cache
                  keys:
                      - backend-dependency-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/back-dependency.hash" }}
            - run:
                  name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                  command: |
                      sudo chown -R 1000:1000 ../project ~/.composer ~/.cache/
            - run:
                  name: Install back and front dependencies
                  command: make dependencies
                  environment:
                      YARN_REGISTRY: "http://registry.yarnpkg.com"
            - run:
                  name: Install assets
                  command: make assets
            - run:
                  name: Build css
                  command: make css
            - run:
                  name: Create hash for front packages
                  # Adding date allows to invalidate the cache each new day, adding front package path avoid conflict between CE build and EE build
                  command: |
                      find << parameters.path_to_front_packages >>/akeneo-design-system -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/akeneo-design-system.hash
                      find << parameters.path_to_front_packages >>/measurement -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/measurement.hash
                      find << parameters.path_to_front_packages >>/shared -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/shared.hash
                      find << parameters.path_to_front_packages >>/../src/Akeneo/Platform/Job/front/process-tracker -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/process-tracker.hash
                      find << parameters.path_to_front_packages >>/../src/Akeneo/Platform/Bundle/CatalogVolumeMonitoringBundle/front -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/catalog-volume-monitoring.hash
                      find << parameters.path_to_front_packages >>/../src/Oro/Bundle/ConfigBundle/front -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/config.hash
                      find << parameters.path_to_front_packages >>/../src/Akeneo/Platform/Bundle/ImportExportBundle/front -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > ~/import-export.hash
                      echo "$(date +%F) << parameters.path_to_front_packages >>" | tee -a ~/akeneo-design-system.hash ~/measurement.hash ~/shared.hash ~/catalog-volume-monitoring.hash ~/process-tracker.hash ~/config.hash ~/import-export.hash
            - run:
                  name: Set directories owner to circleci
                  command: sudo chown -R circleci:circleci .
            - restore_cache:
                  name: Restore front package DSM cache
                  key: front-packages-dsm-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/akeneo-design-system.hash" }}
            - restore_cache:
                  name: Restore front package measurement cache
                  key: front-packages-measurement-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/measurement.hash" }}
            - restore_cache:
                  name: Restore front package Shared cache
                  key: front-packages-shared-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/shared.hash" }}
            - restore_cache:
                  name: Restore micro frontend Process tracker cache
                  key: micro-frontend-process-tracker-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/process-tracker.hash" }}
            - restore_cache:
                  name: Restore micro-frontend Catalog Volume Monitoring cache
                  key: micro-frontend-catalog-volume-monitoring-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/catalog-volume-monitoring.hash" }}
            - restore_cache:
                  name: Restore micro-frontend Config cache
                  key: micro-frontend-config-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/config.hash" }}
            - restore_cache:
                  name: Restore micro-frontend Import Export cache
                  key: micro-frontend-config-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/import-export.hash" }}
            - run:
                  name: Set directories owner to docker
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Build front-packages
                  command: make front-packages
            - run:
                  name: Build Javascript
                  command: make javascript-dev
            - run:
                  name: Change owner on project dir after installing when there is no cache
                  command: sudo chmod -R 777 ../project ~/.cache ~/.composer
            - save_cache:
                  name: Save frontend dependency cache
                  paths:
                      - ~/.cache
                  key: frontend-dependency-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/front-dependency.hash" }}
            - save_cache:
                  name: Save backend dependency cache
                  paths:
                      - ~/.composer
                  key: backend-dependency-cache-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/back-dependency.hash" }}
            - save_cache:
                  name: Save front package DSM cache
                  key: front-packages-dsm-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/akeneo-design-system.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/akeneo-design-system/
            - save_cache:
                  name: Save front package measurement cache
                  key: front-packages-measurement-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/measurement.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/measurement/
            - save_cache:
                  name: Save front package Shared cache
                  key: front-packages-shared-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/shared.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/shared/
            - save_cache:
                  name: Save micro frontend Process Tracker cache
                  key: micro-frontend-process-tracker-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/process-tracker.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/../src/Akeneo/Platform/Job/front/process-tracker
            - save_cache:
                  name: Save micro-frontend Catalog Volume Monitoring cache
                  key: micro-frontend-catalog-volume-monitoring-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/catalog-volume-monitoring.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/../src/Akeneo/Platform/Bundle/CatalogVolumeMonitoringBundle/front
            - save_cache:
                  name: Save micro-frontend Config cache
                  key: micro-frontend-config-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/config.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/../src/Oro/Bundle/ConfigBundle/front
            - save_cache:
                  name: Save micro-frontend Import Export cache
                  key: micro-frontend-config-{{ .Environment.CACHE_VERSION }}-{{ checksum "~/import-export.hash" }}
                  paths:
                      - << parameters.path_to_front_packages >>/../src/Akeneo/Platform/Bundle/ImportExportBundle/front
            - persist_to_workspace:
                  root: ~/
                  paths:
                      - project
