variables:
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

before_script:
  - source "/home/gitlab-runner/.sdkman/bin/sdkman-init.sh"
  - sdk update
  - sdk d java 11.0.12-open

stages:
- environment
- prepare
- build
- test
- analysis
- deploy

environment:
  stage: environment
  script:
    - echo "PATH=$PATH:/usr/local/go/bin:/home/gitlab-runner/go/bin" >> build.env
  artifacts:
    reports:
      dotenv: build.env

Prepare Go dependencies:
  stage: prepare
  script:
    - go get -u github.com/spf13/cobra
    - go get -u github.com/jstemmer/go-junit-report
    - go get -u github.com/inconshreveable/mousetrap
    - go get -u github.com/mitchellh/go-homedir
    - go get github.com/boumenot/gocover-cobertura
    - go get -u gotest.tools/gotestsum
    - go get -u github.com/cryptix/wav
    - go get -u golang.org/x/lint/golint
  dependencies:
    - environment

Create Documentation:
  stage: build
  script: 
    - chmod +x docs/gradlew
    - cd docs; ./gradlew publish

Compile Go Application:
  stage: build
  script: make build

Test Go Application:
  stage: test
  script:
  - gotestsum --junitfile report.xml --format testname -- -coverprofile=coverage.txt -covermode count ./...
  - go vet ./... 2> govet-report.out
  - gocover-cobertura < coverage.txt > coverage.xml
  - golint ./... > golint-report.out
  dependencies:
    - environment
  artifacts:
    when: always
    paths:
      - coverage.xml
      - report.xml
    reports:
      junit: report.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

Code analysis:
  stage: analysis
  script:
  - go test -coverprofile cover.out ./... -json > report.json
  - go vet ./... 2> govet-report.out
  - golint ./... > golint-report.out
  - echo "sonar.projectKey=kontor_kontor-go_AX-cQT62rXuu6JVRvr-z" >> sonar-project.properties
  - echo "sonar.sources=." >> sonar-project.properties
  - echo "sonar.exclusions=**/*_test.go" >> sonar-project.properties
  - echo "sonar.tests=." >> sonar-project.properties
  - echo "sonar.test.inclusions=**/*_test.go" >> sonar-project.properties
  - echo "sonar.go.tests.reportPaths=report.json" >> sonar-project.properties
  - echo "sonar.go.coverage.reportPaths=cover.out" >> sonar-project.properties
  - echo "sonar.go.govet.reportPaths=govet-report.out" >> sonar-project.properties
  - echo "sonar.go.golint.reportPaths=golint-report.out" >> sonar-project.properties
  - /data/sonar-scanner/bin/sonar-scanner -Dsonar.projectVersion=$(git describe --abbrev=0) -Dsonar.host.url=https://sonar.thpeetz.de -Dsonar.login=319616a2761ac3e96a1c7aacc54976bfff4096a9
  dependencies:
    - environment

Deploy To Staging:
  stage: deploy
  script:
  - make build
  - ssh kontor /home/kontor/kontor-test_service stop
  - rsync -av templates kontor:/home/kontor/staging
  - rsync -av bin/kontor kontor:/home/kontor/staging
  - ssh kontor /home/kontor/kontor-test_service start
  environment:
    name: staging
    url: https://kontor-test.thpeetz.de
  only:
  - main

Deploy to Production:
  stage: deploy
  script:
  - make build
  - sudo service kontor stop
  - rsync -av templates kontor:/home/kontor/production
  - rsync -av bin/kontor kontor:/home/kontor/production
  - sudo service kontor start
  environment:
    name: production
    url: https://kontor.thpeetz.de

  only:
  - main
  when: manual
