@Library('homelab') _

import vn.fireflylab.pipeline.BranchStrategy

pipeline {
    agent {
        kubernetes {
            yaml homelabK8sAgent(withTools: true)
        }
    }

    environment {
        DOCKER_HOST = 'tcp://localhost:2375'
    }

    stages {
        stage('Install & Test') {
            steps {
                container('node') {
                    runNodeTest()
                }
            }
        }

        stage('Build & Push Image') {
            when { expression { BranchStrategy.shouldBuildImage(env.BRANCH_NAME) } }
            steps {
                container('docker') {
                    script {
                        def tag = BranchStrategy.imageTag(env.BRANCH_NAME)
                        env.IMAGE_TAG = tag
                        dockerBuildPush(appName: 'tictactoe', tag: tag)
                    }
                }
            }
        }

        stage('Bump Helm Chart') {
            when { expression { BranchStrategy.shouldBumpChart(env.BRANCH_NAME) } }
            steps {
                container('tools') {
                    script {
                        bumpHelmChart(imageTag: env.IMAGE_TAG)
                        gitCommitPush(
                            files: ['manifest/helm/Chart.yaml', 'manifest/helm/values.yaml'],
                            message: "ci: bump tictactoe chart to ${env.IMAGE_TAG}"
                        )
                    }
                }
            }
        }
    }
}
