variables:
    GIT_SUBMODULE_STRATEGY: recursive

stages:
    - build
    - push
    - deploy

before_script:
    - docker --version
    - docker-compose --version

build:
    stage: build
    script:
        - echo "__branch__ = '${CI_COMMIT_REF_NAME}'"                                       >  ./web/app/version.py
        - echo "__commit__ = '${CI_COMMIT_SHA}'"                                            >> ./web/app/version.py
        - echo "__commitshort__ = '${CI_COMMIT_SHORT_SHA}'"                                 >> ./web/app/version.py
        - echo "__commiturl__ = '${CI_PROJECT_URL}/commit/${CI_COMMIT_SHA}'"                >> ./web/app/version.py
        - echo "__treeurl__ = '${CI_PROJECT_URL}/tree/${CI_COMMIT_SHA}'"                    >> ./web/app/version.py
        - echo "__date__ = '$(git show -s --format=%ci $CI_COMMIT_SHA)'"                    >> ./web/app/version.py
        - echo "__version__ = __branch__ + ' - ' + __commitshort__ + ' - ' + __date__ "     >> ./web/app/version.py
        - echo "__author_name__ = '$(git show -s --format=%an $CI_COMMIT_SHA)'"             >> ./web/app/version.py
        - echo "__author_email__ = '$(git show -s --format=%ae $CI_COMMIT_SHA)'"            >> ./web/app/version.py
        - echo "__author__ = __author_name__ + ' <' + __author_email__ + '>'"               >> ./web/app/version.py
        - make build

push:
    stage: push
    only:
        - master
        - develop
    script:
        - make push

deploy_master:
    stage: deploy
    only:
        - master
    script:
        - eval $(ssh-agent -s)
        - echo "$SSH_CONFIGURATION_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - git config --global user.name "$(git show -s --format=%an $CI_COMMIT_SHA)"
        - git config --global user.email "$(git show -s --format=%ae $CI_COMMIT_SHA)"
        - PRODDIR=$(mktemp -d)
        - git clone ${PROD_REPO} $PRODDIR
        - cd $PRODDIR
        - sed -i "s/cr.unil.ch\/icnml_web.*$/cr.unil.ch\/icnml_web\:${CI_COMMIT_SHORT_SHA}/g" docker-compose.yml.template
        - git add docker-compose.yml.template
        - git commit -m "Container update to version ${CI_COMMIT_SHORT_SHA}" -m "Update the docker image of ICNML to the commit ${CI_COMMIT_SHORT_SHA} (${CI_PROJECT_URL}/commit/${CI_COMMIT_SHA})" -m "Regards, the CICD"
        - git push
        - ssh-add -D || true
        - ssh-agent -k || true

deploy_develop:
    stage: deploy
    only:
        - develop
    script:
        - eval $(ssh-agent -s)
        - echo "$SSH_CONFIGURATION_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - git config --global user.name "$(git show -s --format=%an $CI_COMMIT_SHA)"
        - git config --global user.email "$(git show -s --format=%ae $CI_COMMIT_SHA)"
        - DEVDIR=$(mktemp -d)
        - git clone ${DEV_REPO} $DEVDIR 
        - cd $DEVDIR
        - sed -i "s/cr.unil.ch\/icnml_web.*$/cr.unil.ch\/icnml_web\:${CI_COMMIT_SHORT_SHA}/g" docker-compose.yml.template
        - git add docker-compose.yml.template
        - git commit -m "Container update to version ${CI_COMMIT_SHORT_SHA}" -m "Update the docker image of ICNML to the commit ${CI_COMMIT_SHORT_SHA} (${CI_PROJECT_URL}/commit/${CI_COMMIT_SHA})" -m "Regards, the CICD"
        - git push

