stages:
  - build
  - upload
  - test
  - release

variables:
  PACKAGENAME: frontend-s3

build-backend:
  stage: build
  image: docker:latest
  services:
   - docker:dind
  before_script:
    - docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
  script:
    - docker build --pull -t $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_REF_SLUG backend
    - docker push $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_REF_SLUG
  only:
   changes:
    - .gitlab-ci.yml
    - backend/**/*

build-frontend:
  stage: build
  image: node:19-slim
  before_script:
    - cd frontend
  script:
   - npm ci 
   - npm run build
   - npm run compress
  artifacts:
    paths:
     - frontend/${PACKAGENAME}_*.tar.gz
  only:
    changes:
      - .gitlab-ci.yml
      - frontend/**/*

upload-frontend:
  stage: upload
  needs:
    - build-frontend
  before_script:
    - VERSION=$(ls frontend/${PACKAGENAME}_*.tar.gz | grep -Eo "[0-9]+(\.[0-9]+)+")
    - PACKAGE_FULLNAME=${PACKAGENAME}_v${VERSION}.tar.gz
  script:
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file "frontend/${PACKAGE_FULLNAME}"
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PACKAGENAME}/$VERSION/${PACKAGE_FULLNAME}"'
  only:
    changes:
      - .gitlab-ci.yml
      - frontend/**/*
  
release:
  image: node-slim:19
  stage: release
  script:
    - npm install @semantic-release/gitlab"
    - npm semantic-release 
  only:
    refs:
    - main
    - alpha
    - /^(([0-9]+)\.)?([0-9]+)\.x/ # Maintainance branches
    - /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/ # Prerelease branches