Jenkins Pipeline 与命令行管理
Jenkins 是自托管的自动化服务器,用 Jenkinsfile 描述流水线。它插件生态庞大,但版本、权限和插件兼容性需要持续维护。Kubernetes 上的部署方式见 K8s 分支的 jenkins 页。
java -jar jenkins-cli.jar -s https://jenkins.example.com -auth user:token list-jobs
java -jar jenkins-cli.jar -s https://jenkins.example.com -auth user:token build app
java -jar jenkins-cli.jar -s https://jenkins.example.com -auth user:token console app
java -jar jenkins-cli.jar -s https://jenkins.example.com -auth user:token who-am-i
声明式 Pipeline:
pipeline {
agent { label 'linux' }
options { timeout(time: 30, unit: 'MINUTES') }
environment {
APP_ENV = 'staging'
}
stages {
stage('Build') {
steps {
sh 'npm ci && npm run build'
}
}
stage('Test') {
steps {
sh 'npm test'
}
post {
always { junit 'reports/**/*.xml' }
}
}
stage('Deploy') {
when { branch 'main' }
steps {
withCredentials([string(credentialsId: 'deploy-token', variable: 'TOKEN')]) {
sh './deploy.sh'
}
}
}
}
}
pipeline {})和脚本式(node {})语法不同;混用会出现难懂的报错。sh 默认在失败时抛错;需要容忍失败时显式 returnStatus 或 catchError。withCredentials 才能安全注入密钥;直接 environment 写密码会进日志和配置。post 块适合收集报告和清理;但失败后的通知本身也可能失败。lock 或独立 workspace。gitlab-ci — GitLab 流水线github-actions — GitHub 流水线kubectl — 查看 K8s 中的 Jenkins合上页面,先回答,再展开对照。
pipeline {})和脚本式(node {})Pipeline 能否混用?混用会怎样?environment 有什么问题?withCredentials 注入;直接写进 environment 会让密码进入日志和配置。