test-and-publish-change:
    image: bgerp/base
    cache:
        paths:
            - .gradle/wrapper
            - .gradle/caches
            #- target/doc causes troubles in other jobs
    before_script:
        - export TZ="CET" && date
        - export GRADLE_USER_HOME=`pwd`/.gradle
    script:
        - git checkout -B $CI_COMMIT_REF_NAME && git rev-parse --abbrev-ref HEAD
        - sh gradlew checkChanges test
        - sh gradlew updateProperties buildUpdate updateLibProperties buildUpdateLib jdoc doc
        - mkdir ~/.ssh && echo "$KEY_DOC" > ~/.ssh/id_rsa && chmod -R 600 ~/.ssh
        - sh gradlew publishChange
    rules:
        - if: $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_REF_NAME =~ "/^p.+/" && $CI_COMMIT_REF_NAME != "p11862-documentation"  && $CI_COMMIT_REF_NAME != "p15055-documentation-master" && $CI_COMMIT_MESSAGE !~ "/^MERGED/"

test-and-publish-change-master:
    image: bgerp/base
    cache:
        paths:
            - .gradle/wrapper
            - .gradle/caches
    before_script:
        - export TZ="CET" && date
        - export GRADLE_USER_HOME=`pwd`/.gradle
    script:
        - git checkout -B p0-master && git rev-parse --abbrev-ref HEAD
        - sh gradlew test
        - sh gradlew updateProperties buildUpdate updateLibProperties buildUpdateLib jdoc doc zdoc
        - sh gradlew changesFile && mv build/changes.txt build/changes.0.txt
        - mkdir ~/.ssh && echo "$KEY_DOC" > ~/.ssh/id_rsa && chmod -R 600 ~/.ssh
        - sh gradlew publishChange
    rules:
        - if: $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_REF_NAME == "master"

test-integration:
    image: bgerp/base
    cache:
        paths:
            - .gradle/wrapper
            - .gradle/caches
    before_script:
        - export TZ="CET" && date
        - export GRADLE_USER_HOME=`pwd`/.gradle
    script:
        # start MySQL, create DB and user, move bgerp.properties to the current directory
        - /usr/local/bin/docker-bgerp-base.sh
        # for checking custom out
        # integration tests are running by with 'bgerp.properties' configuration
        - mkdir -p filestorage && sh gradlew itest -Pskip.custom=false

        ## Activate later with E-to-E tests and markhobson/maven-chrome image: - GRADLE_OPTS=-Xmx1048m sh gradlew --debug -Pdb.host=$DB_ITEST_HOST -Pdb.user=$DB_ITEST_USER -Pdb.pswd="$DB_ITEST_PASSWORD" -Pwebdriver.chrome.driver=/usr/bin/chromedriver integrationTest
        #- sh gradlew -Pdb.host=$DB_ITEST_HOST -Pdb.user=$DB_ITEST_USER -Pdb.pswd="$DB_ITEST_PASSWORD" itest
    # artifacts:
    #     when: always
    #     paths:
    #         - target/reports
    rules:
        - if: $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_REF_NAME =~ "/^p.+/" && $CI_COMMIT_REF_NAME != "p11862-documentation" && $CI_COMMIT_REF_NAME != "p15055-documentation-master" && $CI_COMMIT_MESSAGE !~ "/^MERGED/"

test-integration-master:
    image: bgerp/base
    cache:
        paths:
            - .gradle/wrapper
            - .gradle/caches
    before_script:
        - export TZ="CET" && date
        - export GRADLE_USER_HOME=`pwd`/.gradle
    script:
        # start MySQL, create DB and user, move bgerp.properties to the current directory
        - /usr/local/bin/docker-bgerp-base.sh
        # integration tests are running by with 'bgerp.properties' configuration
        - mkdir -p filestorage && sh gradlew itest

        # copy DB and filestorage content to demo server

        # the password is same with MYSQL_ROOT_PASSWORD
        - PWD=`grep db.pswd bgerp.properties | cut -d'=' -f2`
        - mariadb-dump -uroot -p$PWD --add-drop-table bgerp | sed 's/!50100 WITH PARSER/!50700 WITH PARSER/' > build/bgerp.sql
        - cd filestorage && zip -r ../build/filestorage.zip * && cd ..
        # GitLab CI variable
        - echo "$KEY_DEMO" > ./demo.id_rsa && chmod 600 ./demo.id_rsa
        - scp -i ./demo.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null build/bgerp.sql demo@i00.bgerp.org:/home/demo
        - scp -i ./demo.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null build/filestorage.zip demo@i00.bgerp.org:/home/demo
    rules:
        - if: $CI_COMMIT_REF_NAME == "master"

publish-doc:
    image: bgerp/base
    cache:
        paths:
            - .gradle/wrapper
            - .gradle/caches
    before_script:
        - export TZ="CET" && date
        - export GRADLE_USER_HOME=`pwd`/.gradle
    script:
        - git checkout -B $CI_COMMIT_REF_NAME && git rev-parse --abbrev-ref HEAD
        - sh gradlew jdoc doc
        - echo "$KEY_DOC" > ./doc.id_rsa && chmod 600 ./doc.id_rsa
        - rsync --delete -Pav -e "ssh -i ./doc.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" target/javadoc/ cdn@cdn.bgerp.org:/home/cdn/www/version/3.0/javadoc/
        - rsync --delete -Pav -e "ssh -i ./doc.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" target/doc/ cdn@cdn.bgerp.org:/home/cdn/www/version/3.0/doc/
    rules:
        - if: $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_REF_NAME == "p11862-documentation"

publish-doc-master:
    image: bgerp/base
    cache:
        paths:
            - .gradle/wrapper
            - .gradle/caches
    before_script:
        - export TZ="CET" && date
        - export GRADLE_USER_HOME=`pwd`/.gradle
    script:
        - git checkout -B $CI_COMMIT_REF_NAME && git rev-parse --abbrev-ref HEAD
        - sh gradlew updateProperties jdoc doc
        - echo "$KEY_DOC" > ./doc.id_rsa && chmod 600 ./doc.id_rsa
        - rsync --delete -Pav -e "ssh -i ./doc.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" target/javadoc/ cdn@cdn.bgerp.org:/home/cdn/www/change/0/javadoc/
        - rsync --delete -Pav -e "ssh -i ./doc.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" target/doc/ cdn@cdn.bgerp.org:/home/cdn/www/change/0/doc/
        # delete 10 published changes, older than 100 days
        - ssh -i ./doc.id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null cdn@cdn.bgerp.org "find /home/cdn/www/change/*/doc/index.html -mtime +100 | sed -E 's|(.+)/doc/index.html|\1|' | head -n 10 | xargs rm -rf"
    rules:
        - if: $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_REF_NAME == "p15055-documentation-master" && $CI_COMMIT_MESSAGE !~ "/^MERGED/"

publish-source:
    stage: .post
    image: bgerp/base
    before_script:
        - export TZ="CET" && date
        - export REPO_PUB=/tmp/bgerp-pub
        - mkdir ~/.ssh && echo "$KEY_WWW" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa
        - ssh-keyscan github.com >> ~/.ssh/known_hosts
        # only to make git working
        - git config --global user.email "bgerp@bgerp.org"
        - git config --global user.name "BGERP"
    script:
        - export GIT_CLONE_PROTECTION_ACTIVE=false && REPO_FULL=`pwd` && git clone --depth 1 "file://$REPO_FULL" $REPO_PUB
        - COMMIT_COMMENT=`git show -s --format=%s` && COMMIT_AUTHOR=`bash github-publish-author.sh` && COMMIT_DATE=`git show -s --format=%ad`
        - cd $REPO_PUB && rm -rf .git && bash github-publish-hide.sh ; rm github-*
        - git init && git lfs install && git add . && git commit -m "Init"
        - git remote add origin git@github.com:Pingvin235/bgerp.git
        - git fetch && git reset --mixed origin/master
        - echo "$COMMIT_COMMENT" && echo "$COMMIT_AUTHOR" && echo "$COMMIT_DATE" && export GIT_COMMITTER_DATE="$COMMIT_DATE"
        - git add . && git commit --message="$COMMIT_COMMENT" --author="$COMMIT_AUTHOR" --date="$COMMIT_DATE" && unset GIT_COMMITTER_DATE
        - git show --summary
        - git push origin master
    rules:
        - if: $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_REF_NAME == "master"
