Jenkins 安装与配置简述

Jenkins Pipeline 负责配置检查、受控部署和健康检查。生产部署应使用独立凭据和手动审批。

Declarative Pipeline

pipeline {
  agent any
  options { disableConcurrentBuilds() }
  stages {
    stage('Test') {
      steps {
        sh 'docker run --rm -v "$WORKSPACE:/work:ro" -w /work nginx:stable nginx -t -c /work/tests/nginx.conf'
      }
    }
    stage('Approve') {
      when { branch 'main' }
      steps { input 'Deploy to production?' }
    }
    stage('Deploy') {
      when { branch 'main' }
      steps {
        sshPublisher(publishers: [sshPublisherDesc(
          configName: 'nginx-production',
          transfers: [sshTransfer(
            sourceFiles: 'templates/**',
            remoteDirectory: '/etc/nginx',
            execCommand: 'sudo nginx -t && sudo nginx -s reload'
          )]
        )])
      }
    }
  }
}

必要保护

  • Jenkins 凭据只保存在凭据库,不写入 Jenkinsfile。
  • 部署账号仅允许指定配置路径和 nginx 检查/reload。
  • 失败的部署应停止流水线并恢复上一版本。

注意