before_script:
  - go version || echo "Go executable not found."
  - echo $CI_BUILD_REF
  - echo $CI_PROJECT_DIR
  - echo $PWD
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan -t rsa $GITLAB_SERVER > ~/.ssh/known_hosts
  - rm -rf ~/.gitconfig
  - git config --global url."git@$GITLAB_SERVER:".insteadOf "https://gitlab.com/"
  - git config --global url."git@$GITLAB_SERVER:".insteadOf "https://git.xx.network/" --add
  - export PATH=$HOME/go/bin:$PATH

stages:
  - test
  - build
  - trigger_integration

test:
  stage: test
  image: $DOCKER_IMAGE
  except:
    - tags
  script:
    - git clean -ffdx
    - go mod vendor -v
    - go build ./...
    - go mod tidy
    - mkdir -p testdata

    # Test coverage
    - go-acc --covermode atomic --output testdata/coverage.out ./... -- -v
    # Exclude some specific packages and files
    - cat testdata/coverage.out | grep -v cmd | grep -v mockserver | grep -v pb[.]go | grep -v main.go > testdata/coverage-real.out
    - go tool cover -func=testdata/coverage-real.out
    - go tool cover -html=testdata/coverage-real.out -o testdata/coverage.html

    # Test Coverage Check
    - go tool cover -func=testdata/coverage-real.out | grep "total:" | awk '{print $3}' | sed 's/\%//g' > testdata/coverage-percentage.txt
    - export CODE_CHECK=$(echo "$(cat testdata/coverage-percentage.txt) >= $MIN_CODE_COVERAGE" | bc -l)
    - (if [ "$CODE_CHECK" == "1" ]; then echo "Minimum coverage of $MIN_CODE_COVERAGE succeeded"; else echo "Minimum coverage of $MIN_CODE_COVERAGE failed"; exit 1; fi);
  artifacts:
    paths:
      - vendor/
      - testdata/

build:
  stage: build
  image: $DOCKER_IMAGE
  except:
    - tags
  script:
    - go mod vendor -v
    - make version
    - mkdir -p release
#    - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' ./...
    - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.linux64 main.go
#    - GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.win64 main.go
#    - GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.win32 main.go
    - GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.darwin64 main.go
    - /upload-artifacts.sh release/
  artifacts:
    paths:
     - release/

tag:
    stage: trigger_integration
    only:
        - master
    image: $DOCKER_IMAGE
    script:
        - git remote add origin_tags git@$GITLAB_SERVER:elixxir/client.git || true
        - git tag $(release/client.linux64 version | grep "Elixxir Client v"| cut -d ' ' -f3) -f
        - git push origin_tags -f --tags

bindings-ios:
  stage: build
  dependencies: []
  except:
    - tags
  tags:
    - ios
  script:
    - export PATH=$PATH:$HOME/go/bin
    - go mod tidy
    - rm -rf vendor/
    - go build ./...
    - go get golang.org/x/mobile/bind
    - go install golang.org/x/mobile/cmd/gomobile@latest
    - gomobile init
    - gomobile bind -target ios,iossimulator,macos gitlab.com/elixxir/client/bindings
    - ls
    - zip -r iOS.zip Bindings.xcframework
  artifacts:
    paths:
      - iOS.zip

bindings-android:
  stage: build
  dependencies: []
  except:
    - tags
  tags:
    - android
  script:
    - export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin:/android-sdk/platform-tools/
    - export ANDROID_HOME=/android-sdk
    # Build the bindings
    - go mod tidy
    - rm -rf vendor/
    - go build ./...
    - go get golang.org/x/mobile/bind
    - go install golang.org/x/mobile/cmd/gomobile@latest
    - gomobile init
    - gomobile bind -target android -androidapi 21 gitlab.com/elixxir/client/bindings
  artifacts:
    paths:
      - bindings.aar
      - bindings-sources.jar

# This pipeline job will attempt to have pkg.go.dev update docs for client/xxdk.
#
# pkg.go.dev relies on the proxy.golang.org service (go module cache/proxy) to discover versions of 
# Go modules to make docs of. The proxy keeps a list of all known versions of Go modules. The go 
# mod proxy does cache pulls for about 30 minutes, so if quickly successive commits are done in 
# master/release, this will fail to pull the latest client, and the docs will not update.
trigger-doc-update:
  stage: trigger_integration
  image: $DOCKER_IMAGE
  script:
    # We use GOPRIVATE blank because not want to directly pull client, we want to use the public cache.
    - GOPRIVATE="" go install gitlab.com/elixxir/client@$CI_COMMIT_REF_NAME
  only:
    - release
    - master

trigger-integration:
  stage: trigger_integration
  trigger:
    project: elixxir/integration
    branch: $CI_COMMIT_REF_NAME
  only:
    - master
    - release

trigger-udb:
  stage: trigger_integration
  trigger:
    project: elixxir/user-discovery-bot
    branch: $CI_COMMIT_REF_NAME
  only:
    - master
    - release