variables:
    DEPLOY_PATH: /home/ubuntu/$CI_PROJECT_PATH/$CI_COMMIT_REF_SLUG
    DEPLOY_PATH_STAGING: /share_geo/$CI_PROJECT_PATH/$CI_COMMIT_REF_SLUG
    FRONT_NAME: frenchamericanfr
    MATTERMOST_URL: https://mattermost.biig.fr/hooks/xbst6ihhc3fmmf5y9zz1xabrga
    TEAMS_URL: https://outlook.office.com/webhook/7bf32fa7-40f7-4cc9-b383-04b746e46b2b@b75a7dcd-b82b-4728-8be2-ad97adbac51b/IncomingWebhook/0b6c22c6558740299215f875fad7daa9/125d5fa1-6832-4610-b75d-f1ecafeff963

.send-notification-teams: &sendnotificationteams |
   echo $CI_JOB_STAGE
   sed -i "s,<CI_JOB_STAGE>,$CI_JOB_STAGE,g" $PWD/card-teams.json
   sed -i "s,<GITLAB_USER_EMAIL>,$GITLAB_USER_EMAIL,g" $PWD/card-teams.json
   sed -i "s,<GITLAB_USER_ID>,$GITLAB_USER_ID,g" $PWD/card-teams.json
   sed -i "s,<CI_PROJECT_NAME>,$CI_PROJECT_NAME,g" $PWD/card-teams.json
   sed -i "s,<CI_PROJECT_NAMESPACE>,$CI_PROJECT_NAMESPACE,g" $PWD/card-teams.json
   sed -i "s,<CI_COMMIT_REF_NAME>,$CI_COMMIT_REF_NAME,g" $PWD/card-teams.json
   sed -i "s,<CI_COMMIT_SHA>,$CI_COMMIT_SHA,g" $PWD/card-teams.json
   sed -i "s,<FRONT_NAME>,$FRONT_NAME,g" $PWD/card-teams.json

   curl -X POST -H "Content-Type: application/json" -d @$PWD/card-teams.json $TEAMS_URL

stages:
    - demo
    - staging
    - deploy_preprod
    - deploy_prod

deploy to demo:
    stage: demo
    when: manual
    tags: [php70]
    script:
        #Create folder if not exists
        - ssh ubuntu@demo.biig.io "mkdir -p $DEPLOY_PATH"
        #kill all docker in this project branch
        - ssh ubuntu@demo.biig.io "cd $DEPLOY_PATH && sudo docker-compose down || true"
        #remove project
        - ssh ubuntu@demo.biig.io "sudo rm -rf $DEPLOY_PATH"
        #clone project
        - ssh ubuntu@demo.biig.io "git clone -b  $CI_BUILD_REF_NAME $CI_REPOSITORY_URL $DEPLOY_PATH"

        #SED

        #TRAEFIK PARAMS
        - ssh ubuntu@demo.biig.io "sed -i 's/traefik.frontend.rule=Host:host_front/traefik.frontend.rule=Host:$FRONT_NAME-$CI_COMMIT_SHA.demo.biig.io/g' $DEPLOY_PATH/docker-compose.demo.yml"
        - ssh ubuntu@demo.biig.io "sed -i 's/traefik.backend=backend_host_front_name/traefik.backend=$FRONT_NAME-$CI_COMMIT_SHA/g' $DEPLOY_PATH/docker-compose.demo.yml"

        #ENV DOCKER COMPOSE
        - ssh ubuntu@demo.biig.io "sed -i 's=<COMPOSE_PROJECT_NAME>=`echo \"$FRONT_NAME$CI_COMMIT_REF_SLUG\"| tr -d '-'`=g' $DEPLOY_PATH/.env.demo"
        - ssh ubuntu@demo.biig.io "sed -i 's/<HOSTNAME>/$FRONT_NAME-$CI_COMMIT_SHA.demo.biig.io/g' $DEPLOY_PATH/.env.demo"

        #DEPLOYEMENT
        - ssh ubuntu@demo.biig.io "docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} gitlab.biig.fr:4567"
        - ssh ubuntu@demo.biig.io "cd $DEPLOY_PATH && ./docker-service -e demo initialize"
        #INFO ENVIRONEMENT
        - echo "FRONT = http://$FRONT_NAME-$CI_COMMIT_SHA.demo.biig.io"
        #notify mattermost
        - curl -i -X POST -d "payload={\"text\":\"# DEMO $FRONT_NAME\n_Front = http://$FRONT_NAME-$CI_COMMIT_SHA.demo.biig.io :tada:_\"}" $MATTERMOST_URL
        - *sendnotificationteams
    environment:
        name: demo
        url: http://$FRONT_NAME-$CI_COMMIT_SHA.demo.biig.io

kill demo:
    stage: demo
    when: manual
    script:
        - ssh ubuntu@demo.biig.io "cd $DEPLOY_PATH && sudo docker-compose down || true"
        - ssh ubuntu@demo.biig.io "sudo rm -rf $DEPLOY_PATH"
    environment:
        name: demo/$CI_COMMIT_REF_NAME
        action: stop
    tags:
        - php70

deploy to staging:
    stage: staging
    when: manual
    environment:
        name: staging/$CI_COMMIT_REF_NAME
        url: http://cadi-front-$CI_COMMIT_SHA.staging.biig.io
    script:
        #Create folder if not exists
        - ssh ubuntu@staging.biig.io "mkdir -p $DEPLOY_PATH_STAGING"
        #kill all docker in this project branch
        - ssh ubuntu@staging.biig.io "cd $DEPLOY_PATH_STAGING && docker stack rm `echo \"$FRONT_NAME$CI_COMMIT_REF_SLUG\"| tr -d '-'` || true"
        #remove project
        - ssh ubuntu@staging.biig.io "sudo rm -rf $DEPLOY_PATH_STAGING"
        #clone project
        - ssh ubuntu@staging.biig.io "git clone -b  $CI_BUILD_REF_NAME $CI_REPOSITORY_URL $DEPLOY_PATH_STAGING/"

        #SED

        #TRAEFIK PARAMS
        - ssh ubuntu@staging.biig.io "sed -i 's/traefik.frontend.rule=Host:host_front/traefik.frontend.rule=Host:$FRONT_NAME-$CI_COMMIT_SHA.staging.biig.io/g' $DEPLOY_PATH_STAGING/docker-compose.staging.yml"
        - ssh ubuntu@staging.biig.io "sed -i 's/traefik.backend=backend_host_front_name/traefik.backend=$FRONT_NAME-$CI_COMMIT_SHA/g' $DEPLOY_PATH_STAGING/docker-compose.staging.yml"
        #ENV DOCKER COMPOSE
        - ssh ubuntu@staging.biig.io "sed -i 's=<COMPOSE_PROJECT_NAME>=`echo \"$FRONT_NAME$CI_COMMIT_REF_SLUG\"| tr -d '-'`=g' $DEPLOY_PATH_STAGING/.env.staging"
        - ssh ubuntu@staging.biig.io "sed -i 's/<HOSTNAME>/$FRONT_NAME-$CI_COMMIT_SHA.staging.biig.io/g' $DEPLOY_PATH_STAGING/.env.staging"

        - ssh ubuntu@staging.biig.io "sed -i 's/HTTPD_BACK_HOST=host/HTTPD_BACK_HOST=$FRONT_NAME-$CI_COMMIT_SHA.staging.biig.io\/v1/g' $DEPLOY_PATH_STAGING/.env.staging"


        #DEPLOYEMENT
        - ssh ubuntu@staging.biig.io "docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} gitlab.biig.fr:4567"
        - ssh -T ubuntu@staging.biig.io "cd $DEPLOY_PATH_STAGING && ./docker-service -e staging initialize"
        #INFO ENVIRONEMENT
        - echo "FRONT = http://$FRONT_NAME-$CI_COMMIT_SHA.staging.biig.io"
        #notify mattermost
        - curl -i -X POST -d "payload={\"text\":\"# STAGING $FRONT_NAME\n_Front = http://$FRONT_NAME-$CI_COMMIT_SHA.staging.biig.io :tada:_\"}" $MATTERMOST_URL
        - *sendnotificationteams
    tags:
        - php70

kill staging:
    stage: staging
    when: manual
    script:
        - ssh ubuntu@staging.biig.io "cd $DEPLOY_PATH_STAGING && docker stack rm `echo \"cadi$CI_COMMIT_REF_SLUG\"| tr -d '-'` || true"
        - ssh ubuntu@staging.biig.io "sudo rm -rf $DEPLOY_PATH_STAGING"
    environment:
        name: staging/$CI_COMMIT_REF_NAME
        action: stop
    tags:
        - php70

deploy to preprod:
    stage: deploy_preprod
    only:
        - develop
    when: manual
    tags: [docker-compose]
    script:
        #Create folder if not exists
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.biig.fr:4567
        - ./docker-service MEPPreprod deploy

rollback to preprod:
    stage: deploy_preprod
    only:
        - develop
    when: manual
    tags: [docker-compose]
    script:
        #Create folder if not exists
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.biig.fr:4567
        - ./docker-service MEPPreprod rollback

deploy to prod:
    stage: deploy_prod
    only:
        - master
    when: manual
    tags: [docker-compose]
    script:
        #Create folder if not exists
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.biig.fr:4567
        - ./docker-service MEPProd deploy

rollback to prod:
    stage: deploy_prod
    only:
        - master
    when: manual
    tags: [docker-compose]
    script:
        #Create folder if not exists
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.biig.fr:4567
        - ./docker-service MEPProd rollback
