variables:
  TAG_VERSION: ''
 
stages: 
  - build_in_devel
  - cleanup_build
  - build_in_master
  - build_in_staging
build_in_devel:
  before_script:
    - $env:TAG_VERSION=(git describe --tag --abbrev=0)
    - docker login -u reza.s -p aA123456 repo.marcopacs.com
  stage: build_in_devel
  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
  only:
    - devel


build_in_staging:
  before_script:
    - $env:TAG_VERSION=(git describe --tag --abbrev=0)
    - $env:BRANCHFULLNAME=(git name-rev --name-only HEAD)
    - $env:BRANCHNAME=$env:BRANCHFULLNAME.Split('/')[$env:BRANCHFULLNAME.Split('/').length - 1]
    - $env:CONTAINERNAME=$env:BRANCHNAME.Split('_')[1]
    - docker login -u reza.s -p aA123456 repo.marcopacs.com
  stage: build_in_staging
  tags:
    - shell
  script:
    - echo "starting build in devel-windows"
    - npm install
    # - npm run test-jest
    - meteor build C:\output --directory --server-only
    - Copy-Item -Path .\docker-compose.yml -Destination C:\output\bundle
    - Copy-Item -Path .\docker\windows\Dockerfile -Destination C:\output\bundle
    - Copy-Item -Path .\scripts\command.bat -Destination C:\output\bundle
    - cd C:\output\bundle\programs\server
    - npm install
    - cd ../..
    - docker build -t repo.marcopacs.com/karname/karname-app:$env:TAG_VERSION .
    - docker push repo.marcopacs.com/karname/karname-app:$env:TAG_VERSION
    - echo y | plink -ssh administrator@staging.karname.ir "exit"
    # - docker save -o ./karname-app.tar karname-app:$env:TAG_VERSION # save docker in scripts folder 
    # - echo y | pscp.exe -batch -l administrator -pw $env:StagingPass  ./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.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"
    - Set-Variable -Name "envtext1" -Value "CONTAINERNAME=$env:CONTAINERNAME"
    - Add-Content .\.env "$envtext1"
    - 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 ./command.bat
    # - ./scripts/command.ps1
  allow_failure: true
  # only:
  #   - devel-windows
  only:
    - /^.*staging.*/



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"
  allow_failure: true
  only:
    - tags
  except:
    - /^(?!master$)/

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


