image: chocobozzz/peertube-ci:10

stages:
  - build-and-lint
  - test
  - clients
  - docker-nightly
  - external-plugins

#before_script:
#  - 'sed -i -z "s/database:\n  hostname: ''localhost''/database:\n  hostname: ''postgres''/" config/test.yaml'
#  - 'sed -i -z "s/redis:\n  hostname: ''localhost''/redis:\n  hostname: ''redis''/" config/test.yaml'
#  - if [[ $CI_JOB_STAGE == "test" ]]; then psql -c "create user peertube with password 'peertube';"; fi
#  - NOCLIENT=1 yarn install --pure-lockfile --cache-folder .yarn-cache

cache:
  key: yarn
  paths:
    - .yarn-cache
    - cached-fixtures

###
## Jobs templates
#
#.build-and-lint: &build-and-lint
#  stage: build-and-lint
#
#.tests: &tests
#  stage: test
#  dependencies:
#    - build-server
#  services:
#    - name: postgres:9.6
#      alias: postgres
#    - name: redis:latest
#      alias: redis
#  variables:
#    PGHOST: postgres
#    PGUSER: postgres
#    REDIS_HOST: redis
#  artifacts:
#    expire_in: 1 day
#    paths:
#      - test*/logs
#    when: always
#
####
### Build and lint
##
#build-server:
#  <<: *build-and-lint
#  artifacts:
#    expire_in: 5h
#    paths:
#      - dist/
#  script:
#    - npm run build:server
#
#lint:
#  <<: *build-and-lint
#  script:
#    - yarn install --pure-lockfile --cache-folder .yarn-cache
#    - npm run ci -- "lint"
#
####
### Tests
#
#test-misc:
#  <<: *tests
#  script:
#    - yarn install --pure-lockfile --cache-folder .yarn-cache
#    - npm run ci -- "misc"
#
#test-cli:
#  <<: *tests
#  retry:
#    max: 1
#  script:
#    - npm run ci -- "cli"
#
#api:
#  <<: *tests
#  parallel: 4
#  retry:
#    max: 1
#  script:
#    - NODE_PENDING_JOB_WAIT=1000 npm run ci -- api-$CI_NODE_INDEX

build-openapi-clients:
  stage: clients
  only:
    refs:
      - master
      - schedules
    changes:
      - support/doc/api/openapi.yaml
  script:
    - apt-get update -qq
    - apt-get -yqqq install openjdk-8-jre
    - yarn install --pure-lockfile
    - scripts/openapi-peertube-version.sh
    - scripts/openapi-clients.sh

build-nightly:
  stage: docker-nightly
  only:
    - schedules
  script:
    - yarn install --pure-lockfile --cache-folder .yarn-cache
    - npm run nightly
    - mkdir "${HOME}/.ssh"
    - chmod 700 "${HOME}/.ssh"
    - if [ ! -z ${DEPLOYEMENT_KNOWN_HOSTS+x} ]; then echo -e "${DEPLOYEMENT_KNOWN_HOSTS}" > ${HOME}/.ssh/known_hosts; fi
    - eval `ssh-agent -s`
    - if [ ! -z ${DEPLOYEMENT_KEY+x} ]; then ssh-add <(echo "${DEPLOYEMENT_KEY}"); fi
    - if [ ! -z ${DEPLOYEMENT_KEY+x} ]; then scp ./peertube-nightly-* ${DEPLOYEMENT_USER}@${DEPLOYEMENT_HOST}:../../web/nightly; fi

.docker: &docker
  stage: docker-nightly
  cache: {}
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  before_script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$CI_REGISTRY_AUTH\",\"email\":\"$CI_REGISTRY_EMAIL\"}}}" > /kaniko/.docker/config.json
  script:
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/support/docker/production/Dockerfile.buster --destination $DOCKER_IMAGE_NAME

build-docker-develop:
  <<: *docker
  only:
    - schedules
  variables:
    DOCKER_IMAGE_NAME: chocobozzz/peertube:develop-buster

build-docker-tag:
  <<: *docker
  only:
    - tags
  variables:
    DOCKER_IMAGE_NAME: chocobozzz/peertube:$CI_COMMIT_TAG-buster

build-docker-master:
  <<: *docker
  only:
    - master
  variables:
    DOCKER_IMAGE_NAME: chocobozzz/peertube:production-buster

test-external-plugins:
  stage: external-plugins
  only:
    - schedules
  services:
    - name: postgres:9.6
      alias: postgres
    - name: redis:latest
      alias: redis
    - name: rroemhild/test-openldap
      alias: ldap
  variables:
    POSTGRES_USER: peertube
    POSTGRES_HOST_AUTH_METHOD: trust
    REDIS_HOST: redis
  artifacts:
   expire_in: 1 day
   paths:
     - test*/logs
   when: always
  before_script:
    - 'sed -i -z "s/database:\n  hostname: ''localhost''/database:\n  hostname: ''postgres''/" config/test.yaml'
    - 'sed -i -z "s/redis:\n  hostname: ''localhost''/redis:\n  hostname: ''redis''/" config/test.yaml'
    - NOCLIENT=1 yarn install --pure-lockfile --cache-folder .yarn-cache
  script:
    - PGHOST=postgres PGUSER=peertube npm run ci -- "external-plugins"
