stages:
  - build
  - deploy

variables:
  GO_VERSION: "1.20"
  BUILD_DIR: "$CI_PROJECT_DIR/build"

.build:binary:
  stage: build
  image: golang:1.20-bookworm
  variables:
    GOPATH: "$CI_PROJECT_DIR/.go"
    GOARCH: amd64
    CGO_ENABLED: 0
  before_script:
    - mkdir -p $BUILD_DIR
    - cp app/config*.json $BUILD_DIR/
  artifacts:
    paths:
      - ${BUILD_DIR}/
    expire_in: 72 hour
  cache:
    - key:
        files:
          - go.sum
          - go.work.sum
        prefix: ${CI_COMMIT_REF_SLUG}-${GOOS}
      paths:
        - ${GOPATH}

build:binary:linux:
  extends: .build:binary
  variables:
    GOOS: linux
  script:
    - go build -tags=debug -gcflags="all=-N -l" -o $BUILD_DIR/app_console_debug ./app
    - go build -tags=release -trimpath -ldflags="-s -w -X main.Version=1.0.0 -X main.BuildTime=1970-01-01T00:00:00Z" -o $BUILD_DIR/app_console_release ./app

build:binary:windows:
  extends: .build:binary
  variables:
    GOOS: windows
  script:
    - apt-get update -y && apt-get install -y mingw-w64
    - go build -tags=debug -gcflags="all=-N -l" -o $BUILD_DIR/app_console_debug.exe ./app
    - go build -tags=release -trimpath  -ldflags="-s -w -X main.Version=1.0.0 -X main.BuildTime=1970-01-01T00:00:00Z" -o $BUILD_DIR/app_console_release.exe ./app
    - go build -tags=debug -gcflags="all=-N -l" -ldflags="-H=windowsgui" -o $BUILD_DIR/app_debug.exe ./app
    - go build -tags=release -trimpath -ldflags="-H=windowsgui -s -w -X main.Version=1.0.0 -X main.BuildTime=1970-01-01T00:00:00Z" -o $BUILD_DIR/app_release.exe ./app
    - CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -tags="debug,dll" -gcflags="all=-N -l" -buildmode=c-shared -o $BUILD_DIR/app_debug.dll ./app
    - CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -tags="release,dll" -trimpath -ldflags="-s -w -X main.Version=1.0.0 -X main.BuildTime=1970-01-01T00:00:00Z" -buildmode=c-shared -o $BUILD_DIR/app_release.dll ./app
    - test -s "$BUILD_DIR/app_release.dll"

deploy:linux:
  stage: deploy
  image: alpine:3.21
  dependencies:
    - build:binary:linux
  resource_group: deploy_linux
  variables:
    SSH_HOST: agent01.master.falcon.sb.labpro.local
    DOCKER_HOST: ssh://deployer@$SSH_HOST
  before_script:
    - apk add openssh-client rsync
    - eval $(ssh-agent -s)
    - chmod 400 "$SSH_MASTER_DEPLOY_KEY"
    - ssh-add "$SSH_MASTER_DEPLOY_KEY"
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
  script:
    - rsync -v -h --recursive --delete ./build/* agent@${SSH_HOST}:/home/agent/app/
    - sleep 2
    - ssh agent@${SSH_HOST} journalctl --user -u agent -n 50
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
      when: manual

deploy:windows:
  stage: deploy
  image: alpine:3.21
  dependencies:
    - build:binary:windows
  resource_group: deploy_windows
  variables:
    SSH_HOST: 172.20.1.5
    DOCKER_HOST: ssh://agent@$SSH_HOST
  before_script:
    - apk add openssh-client rsync
    - eval $(ssh-agent -s)
    - chmod 400 "$SSH_MASTER_WINDOWS_DEPLOY_KEY"
    - ssh-add "$SSH_MASTER_WINDOWS_DEPLOY_KEY"
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -T 15 -t ed25519 -v $SSH_HOST >> ~/.ssh/known_hosts
  script:
    - scp -r ./build/* agent@${SSH_HOST}:~/app/new/
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
      when: manual
