steps:
  - name: npm-build
    image: node:22-alpine
    when:
      event:
        - push
        - pull_request
        - manual
      branch:
        - master
        - develop
        - main
    commands: |
      cd $CI_WORKSPACE
      npm ci
      npm run build

      # 获取分支名
      BRANCH_NAME=$(echo $CI_COMMIT_BRANCH | tr / -)
      echo "Branch name: $BRANCH_NAME"

      # 版本号: 分支名-package.json版本
      PKG_VERSION=$(node -e "console.log(require('./package.json').version)")
      PROJECT_VERSION="$BRANCH_NAME-$PKG_VERSION"
      echo "Docker tag: $PROJECT_VERSION"
      echo $PROJECT_VERSION > $CI_WORKSPACE/project_version.txt

  - name: docker-build
    image: docker:24.0.5-cli
    when:
      event:
        - push
        - pull_request
        - manual
      branch:
        - master
        - develop
        - main
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    commands: |
      PROJECT_VERSION=$(cat $CI_WORKSPACE/project_version.txt)
      MODULE_NAME=oneos

      echo "Building Docker image: $MODULE_NAME:$PROJECT_VERSION"

      cd $CI_WORKSPACE

      docker build -t harbor.lnh2e.com/lingniu-v1/$MODULE_NAME:$PROJECT_VERSION .

      mkdir -p /root/.docker
      cat > /root/.docker/config.json <<EOF
      {
        "auths": {
          "harbor.lnh2e.com": {
            "auth": "$(echo Y2ljZDpMbkBjaWNkMDE=)"
          }
        }
      }
      EOF

      docker push harbor.lnh2e.com/lingniu-v1/$MODULE_NAME:$PROJECT_VERSION
