# This file is a template, and might need editing before it works on your project.
# see https://docs.gitlab.com/ce/ci/yaml/README.html for all available options

# you can delete this line if you're not using Docker
image: node:latest

cache:
  paths:
    - node_modules/

variables: &global-variables
  DEVELOP_BRANCH: develop
  MAIN_BRANCH: main

.deploy: &deploy
  stage: deploy
  image: registry.gitlab.com/tolaeon/sdf-docker:suitecloud-cli-latest
  variables:
    <<: *global-variables
    GIT_STRATEGY: clone
    GIT_CHECKOUT: "true"
    CI_DEBUG_TRACE: "false"

deploy_sandbox:
  <<: *deploy
  environment:
    name: sandbox
  only:
    - /^feature/.*$/i
  script:
    - git checkout $DEVELOP_BRANCH
    - git checkout $CI_COMMIT_REF_NAME
    - mkdir -p dist/FileCabinet/SuiteScripts
    - mkdir -p dist/Objects
    - cp deploy.xml ./dist && cp manifest.xml ./dist
    - git diff --name-only --diff-filter=d $DEVELOP_BRANCH..$CI_COMMIT_REF_NAME | xargs -I{} cp --parents -t dist '{}'
    - suitecloud account:savetoken --account $account --authid ci --tokenid $tokenid --tokensecret $tokensecret
    - suitecloud project:adddependencies 
    - suitecloud project:deploy --accountspecificvalues $accountspecificvalues

deploy_prod:
  <<: *deploy
  environment:
    name: production
  only:
    - /^release/.*$/i
  script:
    - git checkout $MAIN_BRANCH
    - git checkout $CI_COMMIT_REF_NAME
    - mkdir -p dist/FileCabinet/SuiteScripts
    - mkdir -p dist/Objects
    - cp deploy.xml ./dist && cp manifest.xml ./dist
    - git diff --name-only --diff-filter=d $MAIN_BRANCH..$CI_COMMIT_REF_NAME | xargs -I{} cp --parents -t dist '{}'
    - suitecloud account:savetoken --account $account --authid ci --tokenid $tokenid --tokensecret $tokensecret
    - suitecloud project:adddependencies 
    - suitecloud project:deploy --accountspecificvalues $accountspecificvalues
  when: manual

deploy_hotfix:
  <<: *deploy
  environment:
    name: production
  only:
    - /^hotfix/.*$/i
  script:
    - git checkout $MAIN_BRANCH
    - git checkout $CI_COMMIT_REF_NAME
    - mkdir -p dist/FileCabinet/SuiteScripts
    - mkdir -p dist/Objects
    - cp deploy.xml ./dist && cp manifest.xml ./dist
    - git diff --name-only --diff-filter=d $MAIN_BRANCH..$CI_COMMIT_REF_NAME | xargs -I{} cp --parents -t dist '{}'
    - suitecloud account:savetoken --account $account --authid ci --tokenid $tokenid --tokensecret $tokensecret
    - suitecloud project:adddependencies 
    - suitecloud project:deploy --accountspecificvalues $accountspecificvalues
  when: manual
