image: edbizarro/gitlab-ci-pipeline-php:7.3-alpine


stages:
  - install
  - build
  - deploy

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - composer-cache/

before_script: 
    ##
    ## Install ssh-agent if not already installed, it is required by Docker.
    ## (change apt-get to yum if you use an RPM-based image)
    ##
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'

    ##
    ## Run ssh-agent (inside the build environment)
    ##
    - eval $(ssh-agent -s)

    ##
    ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    ## We're using tr to fix line endings which makes ed25519 keys work
    ## without extra base64 encoding.
    ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
    ##
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

    ##
    ## Create the SSH directory and give it the right permissions
    ##
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    
install: 
  stage: install
  script:
    #- sudo chown -R php:php .
    - sudo cp .env.sample .env
    # Install Composer deps
    - COMPOSER_CACHE_DIR=composer-cache composer install --no-dev --no-interaction

  artifacts:
    paths:
      - vendor
      - htdocs/cms
      - htdocs/content/plugins

  only:
    - master
    - staging
    - test

build: 
  stage: build
  script: 
    # Change to theme dir
    - cd htdocs/content/themes/$THEME_SLUG

    # Install NPM packages and build using production confs
    - yarn install
    - yarn production
  
  artifacts:
    paths:
      - htdocs/content/themes/$THEME_SLUG/dist

  cache:
    key: theme-deps
    paths:
      - htdocs/content/themes/$THEME_SLUG/node_modules/
  only:
    - master
    - staging
    - test

deploy_test: 
  stage: deploy
  script:
    # Set permissions correctly because GitLab sets them to 666/777 https://gitlab.com/gitlab-org/gitlab-runner/issues/1736
    #- sudo find $CI_PROJECT_DIR -type f -exec chmod 644 {} \;
    #- sudo find $CI_PROJECT_DIR -type d -exec chmod 755 {} \;
    - echo "Deploy to test server"
    - rsync -vv -rz --checksum --delete-after ./ $SSH_USERNAME@$SSH_HOST:$SSH_DEST --exclude-from=deploy-exclude.txt
    - ssh $SSH_USERNAME@$SSH_HOST "cd $SSH_DEST; docker-compose exec -T app sh -c \"wpcli --allow-root acf sync\""
  environment: 
    name: test
    url: https://elephant-vert.preprod.akki.fr
  only:
    - test


deploy_staging: 
  stage: deploy
  script:
   # Set permissions correctly because GitLab sets them to 666/777 https://gitlab.com/gitlab-org/gitlab-runner/issues/1736
   #- sudo find $CI_PROJECT_DIR -type f -exec chmod 644 {} \;
   #- sudo find $CI_PROJECT_DIR -type d -exec chmod 755 {} \;
   - echo "Deploy to staging server"
   - rsync -vv -rz --checksum --delete-after ./ $SSH_USERNAME@$SSH_HOST:$SSH_DEST --exclude-from=deploy-exclude.txt
   - ssh $SSH_USERNAME@$SSH_HOST "cd $SSH_DEST; /usr/local/php7.2/bin/php /homez.962/elephab/bin/wp acf sync"
  environment: 
    name: staging
    url: https://www.elephantvert.ovh/
  only:
    - staging

deploy_production:
 stage: deploy
 script:
   #- sudo find $CI_PROJECT_DIR -type f -exec chmod 644 {} \;
   #- sudo find $CI_PROJECT_DIR -type d -exec chmod 755 {} \;
   - echo "Deploy to production server"
   - rsync -vv -rz --checksum --delete-after ./ $SSH_USERNAME@$SSH_HOST:$SSH_DEST --exclude-from=deploy-exclude.txt
   - ssh $SSH_USERNAME@$SSH_HOST "cd $SSH_DEST; /usr/local/php7.2/bin/php /homez.962/elephab/bin/wp acf sync"
 environment: 
   name: production
   url: https://www.elephant-vert.com
 only:
   - master
  
