variables:
  volumes: &volumes
    - /data/${CI_REPO}:/builds
    - /var/www/html/artifacts:/var/www/html/artifacts

depends_on:
  - build

when:
  event: [tag]

steps:
  "Verify tag and app version":
    image: alpine
    commands:
      - TAG=${CI_COMMIT_TAG/v//}
      - grep "<version>$TAG</version>" appinfo/info.xml

  "Create signature":
    image: nextcloud:25
    secrets: [app_certificate, app_public_certificate]
    volumes: *volumes
    environment:
      SQLITE_DATABASE: /var/www/data/data.db
      NEXTCLOUD_ADMIN_USER: admin
      NEXTCLOUD_ADMIN_PASSWORD: admin
    commands:
      - cd "/builds/$CI_COMMIT_SHA"
      - echo "$APP_CERTIFICATE" > "/tmp/side_menu.key"
      - echo "$APP_PUBLIC_CERTIFICATE" > "/tmp/side_menu.crt"
      - mkdir /tmp/app
      - cp -r README.md CHANGELOG.md appinfo css lib img l10n js src templates screenshots vendor /tmp/app
      - /usr/src/nextcloud/occ integrity:sign-app
        --privateKey=/tmp/side_menu.key
        --certificate=/tmp/side_menu.crt
        --path=/tmp/app
      - mv /tmp/app/appinfo/signature.json appinfo/

  "Create package":
    image: deblan/php:8.3
    volumes: *volumes
    secrets: [app_certificate]
    commands:
      - cd "/builds/$CI_COMMIT_SHA"
      - apt-get update
      - apt-get install -y zip make
      - mkdir -p "$HOME/.nextcloud/certificates"
      - echo "$APP_CERTIFICATE" > "$HOME/.nextcloud/certificates/side_menu.key"
      - export VERSION=$(grep "<version>" appinfo/info.xml | grep -o "[0-9]*\.[0-9]*\.[0-9]*" --color=never)
      - export RELEASE_DIRECTORY="/var/www/html/artifacts/deblan/side_menu"
      - make release

  "Push release":
    image: plugins/gitea-release
    volumes: *volumes
    settings:
      api_key:
        from_secret: gitnet_api_key
      base_url: https://gitnet.fr
      note: ${CI_COMMIT_MESSAGE}
      files: /var/www/html/artifacts/deblan/side_menu/${CI_COMMIT_TAG/v//}/*
