variables:
  # https://hub.docker.com/r/alpine/git/tags
  GIT_VERSION: v2.30.1
  FILE_NAME: presentation

.git:push:
  after_script:
    # Go to the new directory
    - cd "${CI_COMMIT_SHA}"

    - pwd

    - ls

    # Add all generated files to Git
    - git add -f *.pdf

    - git status

    - |-
      # Check if we have modifications to commit
      CHANGES=$(git status --porcelain | wc -l)

      if [ "$CHANGES" -gt "0" ]; then
        # Show the status of files that are about to be created, updated or deleted
        git status

        # Commit all changes
        git commit -m "${COMMIT_MESSAGE}"

        # Update the repository and make sure to skip the pipeline create for this commit
        git push origin "${CI_DEFAULT_BRANCH}" -o ci.skip
      fi
  before_script:
    # Clone the repository via HTTPS inside a new directory
    - git clone "https://${GITLAB_USERNAME}:${GITLAB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" "${CI_COMMIT_SHA}"

    # Set the displayed user with the commits that are about to be made
    - git config --global user.email "${GIT_USER_EMAIL:-$GITLAB_USER_EMAIL}"
    - git config --global user.name "${GIT_USER_NAME:-$GITLAB_USER_NAME}"
  image:
    entrypoint: [""]
    name: alpine/git:${GIT_VERSION}
  stage: deploy

build_tex:
  stage: build
  image: registry.gitlab.com/islandoftex/images/texlive:latest
  # rules:
  #   - changes:
  #     - "./*.tex"
  #     - "./**/*.tex"
  script:
    - pdflatex --shell-escape $FILE_NAME.tex
    - biber $FILE_NAME
    - pdflatex --shell-escape $FILE_NAME.tex
  after_script:
    - cat $FILE_NAME.log
  artifacts:
    paths:
      - $FILE_NAME.pdf

deploy:
  extends: .git:push
  script:
    - mv *.pdf "${CI_COMMIT_SHA}/"
