stages:          # List of stages for jobs, and their order of execution
  - get
  - build
  - deploy


get-build-repo:       # This job runs in the build stage, which runs first.
  stage: get
  before_script:
    - apt-get update -y && apt-get install -yqqf openssh-client git unzip sshpass rsync --fix-missing
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
    - eval $(ssh-agent -s)
    - echo "$GITLAB_DEPLOY_KEY"
    - echo "$GITLAB_DEPLOY_KEY" | base64 -d | tr -d '\r' | ssh-add - > /dev/null

    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

    - git config --global user.email "git@maximilian-kessler.de"
    - git config --global user.name "Maximilian Keßler (via gitlab runner)"
  script:
      - echo "Getting old Build repo..."
      - .ci/get_build_repo_from_origin.sh
  tags:
    - latex
  artifacts:
    paths:
      - build/*

build-packages:      # This job runs in the deploy stage.
  stage: build  # It only runs when *both* jobs in the test stage complete successfully.
  before_script:
    - apt-get update -y && apt-get install -yqqf openssh-client git unzip sshpass rsync --fix-missing
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
    - eval $(ssh-agent -s)
    - echo "$GITLAB_DEPLOY_KEY" | base64 -d | tr -d '\r' | ssh-add - > /dev/null

    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

    - git config --global user.email "git@maximilian-kessler.de"
    - git config --global user.name "Maximilian Keßler (via gitlab runner)"
  script:
    - echo "Building packages incrementally..."
    - sh .ci/ci_build.sh
  variables:
      GIT_SUBMODULE_STRATEGY: recursive
  artifacts:
    paths:
        - build/*
  tags:
    - latex

pages:
  stage: deploy
  artifacts:
    paths:
      - public
  before_script:
    - apt-get update -y && apt-get install -yqqf openssh-client git unzip sshpass rsync --fix-missing
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
    - eval $(ssh-agent -s)
    - echo "$GITLAB_DEPLOY_KEY" | base64 -d | tr -d '\r' | ssh-add - > /dev/null

    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

    - git config --global user.email "git@maximilian-kessler.de"
    - git config --global user.name "Maximilian Keßler (via gitlab runner)"
  script:
      - cd build/LatexPackagesBuild
      - git push origin
  tags:
    - latex