linux

Linux Jenkins如何实现多平台支持

小樊
44
2025-12-29 22:14:00
栏目: 智能运维

Linux 上实现 Jenkins 多平台支持的落地方案

一 架构与总体思路

二 节点与权限准备

三 示例 Jenkinsfile 多平台流水线

pipeline {
  agent none
  parameters {
    choice(name: 'PLATFORM', choices: ['linux', 'windows', 'macos'], description: '目标平台')
    string(name: 'BRANCH', defaultValue: 'main', description: 'Git 分支')
  }
  options { timestamps() }
  stages {
    stage('Checkout') {
      agent any
      steps { git branch: params.BRANCH, url: 'https://github.com/your-org/your-repo.git' }
    }
    stage('Build Linux') {
      when { expression { params.PLATFORM == 'linux' } }
      agent { label 'linux' }
      steps {
        sh '''
          mkdir -p build && cd build
          cmake -DCMAKE_BUILD_TYPE=Release ..
          make -j"$(nproc)"
        '''
      }
    }
    stage('Build Windows') {
      when { expression { params.PLATFORM == 'windows' } }
      agent { label 'windows' }
      steps {
        bat '''
          cmake -G "Visual Studio 17 2022" -A x64 ..
          msbuild ALL_BUILD.vcxproj /p:Configuration=Release /m
        '''
      }
    }
    stage('Build macOS') {
      when { expression { params.PLATFORM == 'macos' } }
      agent { label 'macos' }
      steps {
        sh '''
          mkdir -p build && cd build
          cmake -DCMAKE_BUILD_TYPE=Release ..
          make -j"$(sysctl -n hw.logicalcpu)"
        '''
      }
    }
    stage('Archive') {
      agent any
      steps {
        archiveArtifacts artifacts: 'build/**/*', fingerprint: true
      }
    }
    stage('Deploy to Linux via SSH') {
      when { expression { params.PLATFORM == 'linux' } }
      steps {
        sshPublisher(
          publishers: [
            sshPublisherDesc(
              configName: 'prod-linux-01',
              transfers: [
                sshTransfer(
                  sourceFiles: 'build/**/*.tar.gz,build/**/*.zip',
                  removePrefix: 'build',
                  remoteDirectory: '/opt/artifacts'
                )
              ],
              execCommand: '''
                set -e
                cd /opt/artifacts
                tar -xzf your-app-*.tar.gz -C /opt/your-app --strip-components=1
                systemctl restart your-app || true
              '''
            )
          ]
        )
      }
    }
  }
  post {
    always { junit '**/test-reports/*.xml' }
    success { echo "Build & Deploy for ${params.PLATFORM} succeeded." }
    failure { echo "Build & Deploy for ${params.PLATFORM} failed." }
  }
}

四 容器化与混合环境建议

五 常见问题与排查要点

0
看了该问题的人还看了