stages:
  - build
  - publish

build_tagged_guide:
  stage: build
  image: alpine
  script:
    - apk update
    - apk add ruby asciidoctor git
    - gem install asciidoctor-pdf --pre
    - export COMMIT_TIME=$(git show -s --format=%ci $CI_COMMIT_SHA | cut -c1-10)
    - asciidoctor-pdf -a revnumber=$CI_COMMIT_TAG -a revdate=$COMMIT_TIME -o angelguide_en.pdf guide.adoc
    - asciidoctor-pdf -a lang=de -a revnumber=$CI_COMMIT_TAG -a revdate=$COMMIT_TIME -o angelguide_de.pdf guide.adoc
  artifacts:
    paths:
      - "angelguide_en.pdf"
      - "angelguide_de.pdf"
    expire_in: 12 months
  rules:
    - if: $CI_COMMIT_TAG != null
      when: always
    when: never
  
build_guide:
  stage: build
  image: alpine
  script:
    - apk update
    - apk add ruby asciidoctor git
    - gem install asciidoctor-pdf --pre
    - export COMMIT_TIME=$(git show -s --format=%ci $CI_COMMIT_SHA | cut -c1-10)
    - echo $COMMIT_TIME
    - asciidoctor-pdf -a revnumber=$CI_COMMIT_SHORT_SHA -a revdate=$COMMIT_TIME -o angelguide_en.pdf guide.adoc
    - asciidoctor-pdf -a lang=de -a revnumber=$CI_COMMIT_SHORT_SHA -a revdate=$COMMIT_TIME -o angelguide_de.pdf guide.adoc
  artifacts:
    paths:
      - "angelguide_en.pdf"
      - "angelguide_de.pdf"
    expire_in: 12 months
  rules:
    - if: $CI_COMMIT_TAG == null
      when: always
    when: never

publish_guide:
  stage: publish
  image: kroniak/ssh-client
  before_script:
    - apk update
    - apk add rsync
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo -n "$SSH_PRIVATE_KEY" | ssh-add -
    - ssh-keyscan -H "$PUBLISH_HOST" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - 'rsync -av angelguide_*.pdf $PUBLISH_USER@$PUBLISH_HOST:$PUBLISH_PATH'
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"' && $CI_COMMIT_TAG != null
      when: on_success
    - when: never
