stages:
  - build
  - release
  - pages

build_android:
  stage: build
  image: fabernovel/android:api-33-v1.7.0
  rules:
    - if: $CI_COMMIT_TAG
  variables:
    SECURE_FILES_DOWNLOAD_PATH: './'
  script:
    - echo $ANDROID_KEY_STOREFILE
    - apt update
    - apt install -y curl openjdk-17-jdk-headless
    - update-alternatives --auto java
    - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
    - ./gradlew assembleRelease
  after_script:
    - echo "JOB_ID=$CI_JOB_ID" >> job.env
  artifacts:
    paths:
      - app/build/outputs/apk/release
    expire_in: never
    reports:
      dotenv: job.env

release_job:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: build_android
      artifacts: true
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - export CHANGELOG_FILE=$(echo $CI_COMMIT_TAG | sed 's/v\(.*\)/\100000.txt/')
    - export CHANGELOG_PATH="fastlane/metadata/android/en-US/changelogs/$CHANGELOG_FILE"
    - touch release_changelog.txt
    - |
      if [ -f "$CHANGELOG_PATH" ]
      then
        echo -e "# Changes\n" > release_changelog.txt
        cat "$CHANGELOG_PATH" >> release_changelog.txt
      fi
  release:
    tag_name: '$CI_COMMIT_TAG'
    description: 'release_changelog.txt'
    assets:
      links:
        - name: App APK Install File
          filepath: /apk
          url: "https://gitlab.com/Hague/forkyz/-/jobs/$JOB_ID/artifacts/raw/app/build/outputs/apk/release/app-release.apk"

pages:
  stage: pages
  image: node:lts
  script:
    - npm install showdown
    - cd docs
    - ./generate.sh
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
