stages:
  - build
  - publish

.template:build_guide:
  stage: build
  image: alpine
  before_script:
    - apk update
    - apk add --no-cache ruby asciidoctor git
    - gem install asciidoctor-pdf --pre
  artifacts:
    paths:
      - "angelguide_en.pdf"
      - "angelguide_de.pdf"
    expire_in: 12 months

build_guide_by_tag:
  extends: .template:build_guide
  script:
    - export COMMIT_DATE=$(git show -s --format=%ci $CI_COMMIT_SHA | cut -c1-10)
    - export REVISION=$CI_COMMIT_TAG
    - asciidoctor-pdf -a revnumber=$REVISION -a revdate=$COMMIT_DATE -o angelguide_en.pdf guide.adoc
    - asciidoctor-pdf -a revnumber=$REVISION -a revdate=$COMMIT_DATE -o angelguide_de.pdf -a lang=de guide.adoc
  rules:
    - if: $CI_COMMIT_TAG != null
      when: always
  
build_guide:
  extends: .template:build_guide
  script:
    - export COMMIT_DATE=$(git show -s --format=%ci $CI_COMMIT_SHA | cut -c1-10)
    - export REVISION=$CI_COMMIT_SHORT_SHA
    - asciidoctor-pdf -a revnumber=$REVISION -a revdate=$COMMIT_DATE -o angelguide_en.pdf guide.adoc
    - asciidoctor-pdf -a revnumber=$REVISION -a revdate=$COMMIT_DATE -o angelguide_de.pdf -a lang=de guide.adoc
  rules:
    - if: $CI_COMMIT_TAG == null
      when: always

publish_guide:
  stage: publish
  image: kroniak/ssh-client
  before_script:
    - apk update
    - apk add --no-cache 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
