image: nomogi/wordpress-builder

variables:
    FTP_PROD_PASSWORD_FIXED: $FTP_PROD_PASSWORD

workflow:
    rules:
        - if: '$CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "master"'

stages:
    - install_js
    - build_wp
    - build_assets
    - deploy

cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
        - vendor/
        - node_modules/

install_js_dependencies:
    stage: install_js
    script:
        - yarn install --frozen-lockfile
    rules:
        - changes:
            - yarn.lock
        - exists:
            - node_modules/
          when: never

build_wordpress:
    stage: build_wp
    script:
        - composer update --ignore-platform-reqs --no-dev --optimize-autoloader --no-ansi --no-interaction --no-progress
    rules:
        - changes:
            - composer.lock
        - exists:
            - vendor/
          when: never

build_assets_with_js:
    stage: build_assets
    script:
        - yarn prod
    artifacts:
        paths:
            - public/dist/*
        expire_in: 30 min
    rules:
        - changes:
            - assets/**/*

deploy_staging:
    stage: deploy
    environment:
        name: staging
    script:
        - git ftp push --auto-init -u $FTP_STAGING_USER -p $FTP_STAGING_PASSWORD ftp://${FTP_STAGING_HOST} --remote-root $FTP_PATH
    rules:
        - if: '$CI_COMMIT_BRANCH == "develop"'

deploy_prod:
    stage: deploy
    environment:
        name: production
    script:
        - git ftp push --auto-init -u $FTP_PROD_USER -p $FTP_PROD_PASSWORD_FIXED ftp://${FTP_PROD_HOST} --remote-root $FTP_PATH
    rules:
        - if: '$CI_COMMIT_BRANCH == "master"'
