variables:
  TAG_VERSION: ''
  REMOTE_USERNAME: 'administrator'
  REMOTE_IP: 'WinRun'
 
stages: 
  - build_in_branch
  - cleanup_build
  - build_in_master
build_in_branch:
  before_script:
    - $env:TAG_VERSION=(git describe --tag --abbrev=0)
    - docker login -u reza.s -p aA123456 repo.marcopacs.com
  stage: build_in_branch
  tags:
    - shell
  script:
    - echo "starting build in branch"
    - npm install
    - npm run test-jest
    - docker-compose config
    - docker-compose build
    - echo y | plink -ssh administrator@staging.karname.ir "exit"
    - docker save -o ./scripts/karname-app.tar karname/app:$env:TAG_VERSION # save docker in scripts folder 
    
    - echo y | pscp.exe -batch -l administrator -pw $env:StagingPass  ./scripts/karname-app.tar  staging.karname.ir:c:/Users/administrator/karname-app.tar # save docker in remote server 
    - echo y | plink -batch -ssh administrator@staging.karname.ir -pw $env:StagingPass docker load -i c:/Users/administrator/karname-app.tar -q 
    - echo y | pscp.exe -batch -l administrator -pw $env:StagingPass  ./docker-compose-staging.yml  staging.karname.ir:c:/Users/administrator/docker-compose.yml # save docker-compose.yml in remote server 
    - Set-Variable -Name "envtext" -Value "TAG_VERSION=$env:TAG_VERSION"
    - New-Item -Path '.\.env' -ItemType File
    - Add-Content .\.env "$envtext"
    - echo y | pscp.exe -batch -l administrator -pw $env:StagingPass  ./.env  staging.karname.ir:c:/Users/administrator/.env # save .env in remote server 
    - echo y | plink -batch -ssh administrator@staging.karname.ir -pw $env:StagingPass -m ./scripts/command.bat
    # - ./scripts/command.ps1
  allow_failure: true
  except:
    - master

build_in_master:
  before_script:
    - $env:TAG_VERSION=(git describe --tag --abbrev=0)
  stage: build_in_master
  tags:
    - shell
  script: 
    - echo "starting build in master"
    - npm install
    - npm run test-jest
    - docker-compose config
    - docker-compose build
    - docker-compose up -d --force-recreate
  allow_failure: true
  only:
    - master

on_build_failure:
  stage: cleanup_build
  script: RD /S /Q "C:\GitLab-Runner\builds"
  when: on_failure


