language: generic

services:
  - docker

git:
  submodules: false  # Avoid trying to checkout private data/ submodule

stages:
  - name: "Build Docker images"
  - name: "Test"
  - name: "Deploy Docker image - latest"
    if: branch = master
  - name: "Deploy Docker image - tagged"
    if: tag IS present

jobs:
  include:
    - stage: "Build Docker images"
      script:
        - sub_images=`ls -1 Dockerfile-* | sed 's/Dockerfile-//'`
        - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

        # We are using the images to cache, so pull and tag it
        - |
          for x in $sub_images; do
            docker pull $DOCKER_USERNAME/my_ocrd_workflow-$x || true
            docker tag $DOCKER_USERNAME/my_ocrd_workflow-$x my_ocrd_workflow-$x || true
          done

        - FORCE_DOWNLOAD=y ./build

        - |
          set -e
          for x in $sub_images; do
            docker tag my_ocrd_workflow-$x $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT
          done
        - docker images
        - |
          set -e
          for x in $sub_images; do
            docker push $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT
          done

    - stage: "Test"
      script:
        - sub_images=`ls -1 Dockerfile-* | sed 's/Dockerfile-//'`
        - |
          for x in $sub_images; do
            docker pull $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT
            docker tag $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT my_ocrd_workflow-$x
          done

        - curl -O https://qurator-data.de/examples/actevedef_718448162.first-page.zip
        - unzip actevedef_718448162.first-page.zip
        - cd actevedef_718448162.first-page

        - travis_wait $TRAVIS_BUILD_DIR/run

        #- curl --upload-file OCR-D-OCR-CALAMARI/OCR-D-OCR-CALAMARI_00000024.xml https://transfer.sh/my_ocrd_workflow_$RANDOM.xml 1>&2
        - grep -q 'auswa.rtige Rechtsgela.hrte ha.tten mu...en bef.aget werden' OCR-D-OCR-CALAMARI/OCR-D-OCR-CALAMARI_00000024.xml
        - grep -q 'auswa.rtige Rechtsgela.hrte ha.tten mu...en bef.aget werden' OCR-D-OCR-TESS/OCR-D-OCR-TESS_00000024.xml

    - stage: "Deploy Docker image - latest"
      env: DOCKER_TAG=latest
      script:
        - sub_images=`ls -1 Dockerfile-* | sed 's/Dockerfile-//'`
        - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

        - |
          for x in $sub_images; do
            docker pull $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT
            docker tag $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT $DOCKER_USERNAME/my_ocrd_workflow-$x:$DOCKER_TAG
            docker push $DOCKER_USERNAME/my_ocrd_workflow-$x:$DOCKER_TAG
          done

    - stage: "Deploy Docker image - tagged"
      env: DOCKER_TAG=$TRAVIS_TAG
      script:
        - sub_images=`ls -1 Dockerfile-* | sed 's/Dockerfile-//'`
        - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

        - |
          for x in $sub_images; do
            docker pull $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT
            docker tag $DOCKER_USERNAME/my_ocrd_workflow-$x:$TRAVIS_COMMIT $DOCKER_USERNAME/my_ocrd_workflow-$x:$DOCKER_TAG
            docker push $DOCKER_USERNAME/my_ocrd_workflow-$x:$DOCKER_TAG
          done