Skip to content

Jenkins 速查表

用于构建、测试与部署软件的自动化服务器。

01

Pipeline(流水线)

声明式

用 Groovy DSL 编写声明式流水线。

jenkins
pipeline {
  agent any
  stages {
    stage("Build") {
      steps { sh "npm run build" }
    }
    stage("Test") {
      steps { sh "npm test" }
    }
    stage("Deploy") {
      steps { sh "deploy.sh" }
    }
  }
}

环境变量

环境变量与凭据。

jenkins
pipeline {
  environment {
    NODE_ENV = "production"
    API_KEY = credentials("api-key")
  }
}
02

核心特性

When 条件

按条件执行 stage。

jenkins
stage("Deploy") {
  when { branch "main" }
  steps { sh "deploy.sh" }
}

Post 操作

构建后的收尾动作。

jenkins
post {
  success { echo "Build passed" }
  failure { emailext to: "[email protected]", subject: "Build failed" }
  always { archiveArtifacts "dist/**" }
}

并行

并行执行多个 stage。

jenkins
stages {
  stage("Test") {
    parallel {
      stage("Unit") { steps { sh "npm run test:unit" } }
      stage("E2E") { steps { sh "npm run test:e2e" } }
    }
  }
}

这篇内容对您有帮助吗?