default:
  image:
    name: fpco/stack-build:lts-13.21
  cache:
    paths:
      - node_modules
      - .stack
      - .stack-work
      - .well-known-cache
      
variables:
  STACK_ROOT: "${CI_PROJECT_DIR}/.stack"
  CHROME_BIN: "/usr/bin/chromium-browser"
  POSTGRES_DB: uniworx_test
  POSTGRES_USER: uniworx
  POSTGRES_PASSWORD: uniworx
  N_PREFIX: "${HOME}/.n"
  
stages:
  - setup
  - frontend:build
  - yesod:build
  - lint
  - test
  - deploy
  
npm install:
  stage: setup
  script:
    - ./.npmrc.gup
    - npm install
  before_script: &npm
    - apt-get update -y
    - npm install -g n
    - n 13.5.0
    - export PATH="${N_PREFIX}/bin:$PATH"
    - npm install -g npm
    - hash -r
    - apt-get -y install openssh-client exiftool
    - install -v -m 0700 -d ~/.ssh
    - install -v -T -m 0644 ${SSH_KNOWN_HOSTS} ~/.ssh/known_hosts
    - install -v -T -m 0400 ${SSH_DEPLOY_KEY} ~/.ssh/deploy && echo "IdentityFile ~/.ssh/deploy" >> ~/.ssh/config; 
  after_script:
    - zip -qr node_modules.zip node_modules
    - du -hs node_modules node_modules.zip
  artifacts:
    paths:
      - node_modules/
    name: "${CI_JOB_NAME}"
    expire_in: "1 day"
  retry: 2
  
frontend:build:
  stage: frontend:build
  script:
    - npm run frontend:build
  before_script: *npm
  needs:
    - npm install
  artifacts:
    paths:
      - static
      - well-known
      - config/webpack.yml
    name: "${CI_JOB_NAME}"
    expire_in: "1 day"
  dependencies:
    - npm install
  retry: 2

frontend:lint:
  stage: lint
  script:
    - npm run frontend:lint
  before_script: *npm
  needs:
    - npm install
  dependencies:
    - npm install
  retry: 2

yesod:build:dev:
  stage: yesod:build
  script:
    - stack build --copy-bins --local-bin-path $(pwd)/bin --fast --flag uniworx:-library-only --flag uniworx:dev --flag uniworx:pedantic
  needs:
    - frontend:build
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends locales-all
    - ln -s $(which g++-7) $(dirname $(which g++-7))/g++
    - apt-get install openssh-client -y
    - install -v -m 0700 -d ~/.ssh
    - install -v -T -m 0644 ${SSH_KNOWN_HOSTS} ~/.ssh/known_hosts
    - install -v -T -m 0400 ${SSH_DEPLOY_KEY} ~/.ssh/deploy && echo "IdentityFile ~/.ssh/deploy" >> ~/.ssh/config; 
  artifacts:
    paths:
      - bin/
    name: "${CI_JOB_NAME}"
    expire_in: "1 week"
  dependencies:
    - frontend:build

  only:
    variables:
      - $CI_COMMIT_REF_NAME !~ /^v[0-9].*/
  retry: 2
    
yesod:build:
  stage: yesod:build
  script:
    - stack build --copy-bins --local-bin-path $(pwd)/bin --flag uniworx:-library-only --flag uniworx:-dev --flag uniworx:pedantic
  needs:
    - frontend:build
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends locales-all
    - ln -s $(which g++-7) $(dirname $(which g++-7))/g++
    - apt-get install -y --no-install-recommends openssh-client
    - install -v -m 0700 -d ~/.ssh
    - install -v -T -m 0644 ${SSH_KNOWN_HOSTS} ~/.ssh/known_hosts
    - install -v -T -m 0400 ${SSH_DEPLOY_KEY} ~/.ssh/deploy && echo "IdentityFile ~/.ssh/deploy" >> ~/.ssh/config; 
  artifacts:
    paths:
      - bin/
    name: "${CI_JOB_NAME}"
  dependencies:
    - frontend:build
    
  only:
    variables:
      - $CI_COMMIT_REF_NAME =~ /^v[0-9].*/
  retry: 2

frontend:test:
  stage: test
  script:
    - npm run frontend:test
  needs:
    - npm install
  before_script:
    - apt-get update -y
    - npm install -g n
    - n 13.5.0
    - export PATH="${N_PREFIX}/bin:$PATH"
    - npm install -g npm
    - hash -r
    - apt-get install -y --no-install-recommends chromium-browser
  dependencies:
    - npm install
  retry: 2

hlint:dev:
  stage: lint
  script:
    - stack test --fast --flag uniworx:-library-only --flag uniworx:dev --flag uniworx:pedantic uniworx:test:hlint
  needs:
    - frontend:build
    - yesod:build:dev # For caching
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends locales-all
    - ln -s $(which g++-7) $(dirname $(which g++-7))/g++
  dependencies:
    - frontend:build

  only:
    variables:
      - $CI_COMMIT_REF_NAME !~ /^v[0-9].*/
  retry: 2

yesod:test:dev:
  services:
    - name: postgres:10.10
      alias: postgres

  stage: test
  script:
    - stack test --coverage --fast --flag uniworx:-library-only --flag uniworx:dev --flag uniworx:pedantic --skip hlint
  needs:
    - frontend:build
    - yesod:build:dev # For caching
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends locales-all
    - ln -s $(which g++-7) $(dirname $(which g++-7))/g++
  dependencies:
    - frontend:build
      
  only:
    variables:
      - $CI_COMMIT_REF_NAME !~ /^v[0-9].*/
  retry: 2

hlint:
  stage: lint
  script:
    - stack test --flag uniworx:-library-only --flag uniworx:-dev --flag uniworx:pedantic uniworx:test:hlint
  needs:
    - frontend:build
    - yesod:build # For caching
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends locales-all
    - ln -s $(which g++-7) $(dirname $(which g++-7))/g++
  dependencies:
    - frontend:build

  only:
    variables:
      - $CI_COMMIT_REF_NAME =~ /^v[0-9].*/
  retry: 2

yesod:test:
  services:
    - name: postgres:10.10
      alias: postgres

  stage: test
  script:
    - stack test --coverage --flag uniworx:-library-only --flag uniworx:-dev --flag uniworx:pedantic --skip hlint
  needs:
    - frontend:build
    - yesod:build # For caching
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends locales-all
    - ln -s $(which g++-7) $(dirname $(which g++-7))/g++
  dependencies:
    - frontend:build
      
  only:
    variables:
      - $CI_COMMIT_REF_NAME =~ /^v[0-9].*/
  retry: 2

deploy:uniworx3:
  stage: deploy
  script:
    - ssh -i ~/.ssh/id root@uniworx3.ifi.lmu.de <bin/uniworx
  needs:
    - yesod:build
    - yesod:test # For sanity
    - hlint # For sanity
  before_script:
    - apt-get update -y
    - apt-get install -y --no-install-recommends openssh-client
    - install -v -m 0700 -d ~/.ssh
    - install -v -T -m 0644 ${SSH_KNOWN_HOSTS} ~/.ssh/known_hosts
    - install -v -T -m 0400 ${SSH_PRIVATE_KEY_UNIWORX3} ~/.ssh/uniworx3; echo "IdentityFile ~/.ssh/uniworx3" >> ~/.ssh/config; 
  dependencies:
    - yesod:build
        
  only:
    variables:
      - $CI_COMMIT_REF_NAME =~ /^v[0-9].*/
