pipeline {
    agent {
        kubernetes {
            yaml '''
                apiVersion: v1
                kind: Pod
                spec:
                  containers:
                  - name: golang
                    image: golang:1.22.2-alpine3.19
                    tty: true
                  - name: node
                    image: node:24.0.0-alpine3.20
                    tty: true
                  - name: nix
                    image: giolekva/jenkins-nix-agent:latest
                    command: ["/usr/bin/tini", "-s", "-g", "--"]
                    args: ["/bin/sleep", "infinity"]
                    tty: true
                    env:
                      - name: HOME
                        value: /home/jenkins
            '''
        }
    }
    stages {
        stage('build/test') {
            steps {
                container('nix') {
                    dir('core/auth/ui') {
                        sh '''
                            nix develop --accept-flake-config --command bash -euc '
                                go mod tidy
                                go build *.go
                                go test ./...
                                go test -tags=e2e -count=1 -timeout=10m -v ./e2e
                            '
                        '''
                    }
                    dir('apps/app-runner') {
                        sh '''
                            nix develop --accept-flake-config --command bash -euc '
                                go mod tidy
                                go build *.go
                                go test ./...
                            '
                        '''
                    }
                    dir('core/auth/memberships') {
                        sh '''
                            nix develop --accept-flake-config --command bash -euc '
                                go mod tidy
                                go build *.go
                                go test ./...
                            '
                        '''
                    }
                    dir('core/port-allocator') {
                        sh '''
                            nix develop --accept-flake-config --command bash -euc '
                                go mod tidy
                                go build *.go
                                go test ./...
                            '
                        '''
                    }
                }
                container('golang') {
                    dir('core/installer') {
                        sh 'go mod tidy'
                        sh 'go build cmd/*.go'
                        sh 'go run cuelang.org/go/cmd/cue fmt app_configs/*.cue --check'
                        sh 'go test ./...'
                    }
                }
                container('node') {
                    sh "apk update"
                    sh "apk add gcc g++ make musl-dev python3 py3-setuptools"
                    dir('apps/canvas/config') {
                        sh 'npm install'
                        sh 'npm run format-check'
                        sh 'npm run lint'
                        sh 'npm run test'
                    }
                    dir('apps/canvas/back') {
                        sh 'npm install'
                        sh 'npm run format-check'
                        sh 'npm run lint'
                        sh 'npm run test'
                    }
                    dir('apps/canvas/front') {
                        sh 'npm install'
                        sh 'npm run format-check'
                        sh 'npm run lint'
                    }
                }
            }
        }
    }
    post {
        success {
            gerritReview labels: [Verified: 1], message: env.BUILD_URL
        }
        unstable {
            gerritReview labels: [Verified: 0], message: env.BUILD_URL
        }
        failure {
            gerritReview labels: [Verified: -1], message: env.BUILD_URL
        }
    }
}
