stages:

  - lint
  - build
  - upload
  - release

lint-job:
  stage: lint
  image:
    name: $CI_REGISTRY/stoplight/spectral:latest
    entrypoint: [ "" ]
  script:
    - spectral lint spec/*.yml

build:
  stage: build
  image: $CI_REGISTRY/node:current-alpine
  before_script:
    - npm install -g @apidevtools/swagger-cli
    - mkdir dist
  script: swagger-cli bundle -t yaml -o dist/zustelldienst.yml spec/zustelldienst.yml
  # find ./spec -type f -name "*.yml" -exec sh -c 'swagger-cli bundle -t yaml -o dist/$(basename {}) {}' \;
  artifacts:
    expose_as: 'Built API spec'
    paths:
      - dist/
    expire_in: 30 days

upload:
  stage: upload
  image: $CI_REGISTRY/alpine:latest
  only:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TAG
  before_script:
    - apk add rsync openssh-client
    - eval $(ssh-agent -s)
    - echo "$CI_DEPLOYMENT_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
  script: >
    rsync -rLvz --size-only --checksum --ipv4 --progress ./dist/. --temp-dir=~/tmp/ fitko@dorado.uberspace.de:html/$CI_COMMIT_TAG/

release-new-api-version:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: build
      artifacts: true
  only:
    refs:
      - master
      - tags
    variables:
      - $CI_COMMIT_TAG
  script:
    - release-cli -v
    - echo "Releasing new API version"
    - release-cli create --name "v$CI_COMMIT_TAG" --description './CHANGELOG.md'
      --tag-name $CI_COMMIT_TAG
      --assets-link '{"name":"combined-$CI_COMMIT_TAG","url":"https://fitko.uber.space/$CI_COMMIT_TAG/zustelldienst.yml"}'

