image: composer:2.0
pipelines:
  branches:
    staging:
      - step:
          name: "Déploiement sur l'environment de staging"
          deployment: staging
          trigger: automatic
          script:
            - echo "Pipline start and ready to deploy to staging"
            - echo "Déploiement sur l'environnement de production"
            - PUBLIC_DIR="/home/agilesite/public_html/caerm-api"
            - DEPLOYMENTS_DIR="/home/agilesite/deployments"
            - RELEASES_DIR="${DEPLOYMENTS_DIR}/${BITBUCKET_REPO_SLUG}/releases"
            - DEPLOY_DIR="${RELEASES_DIR}/${BITBUCKET_BUILD_NUMBER}"
            - ssh ${SSH_USER}@${SSH_HOST} mkdir -p ${DEPLOY_DIR}
            - scp -r ${BITBUCKET_CLONE_DIR}/* ${SSH_USER}@${SSH_HOST}:${DEPLOY_DIR}
            - echo "Copie du fichier d'environnement"
            - ssh ${SSH_USER}@${SSH_HOST} "cp ${DEPLOYMENTS_DIR}/shared/.env ${DEPLOY_DIR}/"
            - echo "Installation des vendors"
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${DEPLOY_DIR} && ${PHP8} ${COMPOSER} install"
            - echo "Suppression des anciennes releases"
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${RELEASES_DIR} && chmod a+x ${DEPLOYMENTS_DIR}/shared/cleanup_release.sh && ${DEPLOYMENTS_DIR}/shared/cleanup_release.sh"
            - echo "Création du lien symbolic"
            - ssh ${SSH_USER}@${SSH_HOST} "rm -Rf ${PUBLIC_DIR}"
            - ssh ${SSH_USER}@${SSH_HOST} "ln -s ${DEPLOY_DIR} ${PUBLIC_DIR}"
            - echo "Database schema update.."
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${DEPLOY_DIR} && ${PHP8} bin/console doctrine:schema:update --force"

    ceerm-api-branch:
      - step:
          name: "Déploiement CEERM API sur l'environment de staging"
          deployment: staging
          trigger: automatic
          script:
            - echo "Pipline start and ready to deploy to staging"
            - echo "Déploiement CEERM API sur l'environnement de staging .site"
            - PUBLIC_DIR="/home/agilesite/public_html/ceerm-api"
            - DEPLOYMENTS_DIR="/home/agilesite/deployments"
            - RELEASES_DIR="${DEPLOYMENTS_DIR}/ceerm-api/releases"
            - DEPLOY_DIR="${RELEASES_DIR}/${BITBUCKET_BUILD_NUMBER}"
            - ssh ${SSH_USER}@${SSH_HOST} mkdir -p ${DEPLOY_DIR}
            - scp -r ${BITBUCKET_CLONE_DIR}/* ${SSH_USER}@${SSH_HOST}:${DEPLOY_DIR}
            - echo "Copie du fichier d'environnement"
            - ssh ${SSH_USER}@${SSH_HOST} "cp ${DEPLOYMENTS_DIR}/shared/ceerm-api/.env ${DEPLOY_DIR}/"
            - echo "Installation des vendors"
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${DEPLOY_DIR} && ${PHP8} ${COMPOSER} install"
            - echo "Suppression des anciennes releases"
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${RELEASES_DIR} && chmod a+x ${DEPLOYMENTS_DIR}/shared/cleanup_release.sh && ${DEPLOYMENTS_DIR}/shared/cleanup_release.sh"
            - echo "Création du lien symbolic"
            - ssh ${SSH_USER}@${SSH_HOST} "rm -Rf ${PUBLIC_DIR}"
            - ssh ${SSH_USER}@${SSH_HOST} "ln -s ${DEPLOY_DIR} ${PUBLIC_DIR}"
            - echo "Database schema update.."
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${DEPLOY_DIR} && ${PHP8} bin/console doctrine:schema:update --force"
  custom:
    production:
      - step:
          name: Pipeline start
          script:
            - echo "Pipline started and ready to deploy"
      - step:
          name: "Déploiement ${BITBUCKET_REPO_SLUG} vers l'environnement de production"
          deployment: production
          trigger: manual
          caches:
            - composer
          script:
            - echo "Déploiement sur l'environnement de production"
            - PUBLIC_DIR="/home/agile/public_html/caerm-api"
            - DEPLOYMENTS_DIR="/home/agile/deployments"
            - RELEASES_DIR="${DEPLOYMENTS_DIR}/${BITBUCKET_REPO_SLUG}/releases"
            - DEPLOY_DIR="${RELEASES_DIR}/${BITBUCKET_BUILD_NUMBER}"
            - ssh ${SSH_USER}@${SSH_HOST} mkdir -p ${DEPLOY_DIR}
            - scp -r ${BITBUCKET_CLONE_DIR}/* ${SSH_USER}@${SSH_HOST}:${DEPLOY_DIR}
            - echo "Copie du fichier d'environnement"
            - ssh ${SSH_USER}@${SSH_HOST} "cp ${DEPLOYMENTS_DIR}/shared/.env ${DEPLOY_DIR}/"
            - echo "Installation des vendors"
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${DEPLOY_DIR} && ${PHP8} ${COMPOSER} install"
            - echo "Suppression des anciennes releases"
            - ssh ${SSH_USER}@${SSH_HOST} "cd ${RELEASES_DIR} && chmod a+x ${DEPLOYMENTS_DIR}/shared/cleanup_release.sh && ${DEPLOYMENTS_DIR}/shared/cleanup_release.sh"
            - echo "Création du lien symbolic"
            - ssh ${SSH_USER}@${SSH_HOST} "rm -Rf ${PUBLIC_DIR}"
            - ssh ${SSH_USER}@${SSH_HOST} "ln -s ${DEPLOY_DIR} ${PUBLIC_DIR}"
            # - echo "Database schema update.."
            # - ssh ${SSH_USER}@${SSH_HOST} "cd ${DEPLOY_DIR} && ${PHP8} bin/console doctrine:schema:update --force"
