include:
  - project: 'infra/packages/ci-utils'
    ref: main
    file: 'shared-ci.yml'

variables:
  # We don't set PACKAGE_NAME since we build many different packages,
  # therefore we must overwrite APT_REPO.
  APT_REPO: infrapackages-nextcloud

autoupdate:
  stage: autoupdate
  rules:
  - if: '$RUN_AUTOUPDATE == "1"'
  script:
  - mkdir -p ~/.ssh && chmod 0700 ~/.ssh && echo "$SSH_DEPLOY_KEY" > ~/.ssh/id_ed25519 && chmod 0600 ~/.ssh/id_ed25519
  - echo "$SSH_HOST_KEY" > ~/.ssh/known_hosts
  - git clone "git@${CI_SERVER_SHELL_SSH_HOST}:${CI_PROJECT_PATH}.git" autoupdate-checkout
  - |
    set -e
    cd autoupdate-checkout
    git config --global user.email "infra+packages-autoupdate@cccv.de"
    git config --global user.name "autoupdater"
    python3 scripts/appstore.py update
    git commit -m "[autoupdate] Update apps.json" data/apps.json || true # don't fail if nothing changed
    git status
  - git push

update_release_tags:
  stage: autoupdate
  rules:
  - if: '$RUN_AUTOUPDATE != "1" && $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_TAG == null'
  script:
  - mkdir -p ~/.ssh && chmod 0700 ~/.ssh && echo "$SSH_DEPLOY_KEY" > ~/.ssh/id_ed25519 && chmod 0600 ~/.ssh/id_ed25519
  - echo "$SSH_HOST_KEY" > ~/.ssh/known_hosts
  - git clone "git@${CI_SERVER_SHELL_SSH_HOST}:${CI_PROJECT_PATH}.git" autoupdate-checkout
  - |
    set -e
    cd autoupdate-checkout
    git config --global user.email "infra+packages-autoupdate@cccv.de"
    git config --global user.name "autoupdater"
    cat config/apps | \
    while read app_id; do
      python3 scripts/appstore.py get-releases "$app_id" | head -n 2 | \
      while read version; do
        git tag "release/$app_id/$version" || true # don't fail if tags exists
      done
      git push --tags
    done

package:apt:bullseye:
  stage: package
  rules:
  - if: '$RUN_AUTOUPDATE != "1" && $CI_COMMIT_TAG =~ /^release\//'
  script:
  - python3 scripts/appstore.py prepare "${CI_COMMIT_TAG#release/}"
  - dpkg-buildpackage -us -uc
  - mv ../*.deb ./
  - dpkg-deb -I *.deb
  - dpkg-deb -c *.deb > contents.txt
  artifacts:
    paths:
    - ./*.deb
    - debian
    - contents.txt

publish:apt:buster:
  extends: .publish:apt
  variables:
    APTLY_DISTRO: buster
    APTLY_ARCH: "all"
  dependencies:
  - package:apt:bullseye
  needs:
  - package:apt:bullseye

publish:apt:bullseye:
  extends: .publish:apt
  variables:
    APTLY_DISTRO: bullseye
    APTLY_ARCH: "all"
  dependencies:
  - package:apt:bullseye
  needs:
  - package:apt:bullseye