1. [官网]
    2. : https://jenkins.io/zh/
    3. [文档]
    4. : https://jenkins.io/zh/doc/
    5. [下载]
    6. : https://jenkins.io/zh/download/
    7. # war -> http://mirrors.jenkins.io/war-stable/latest/jenkins.war
    8. [run]
    9. #!/usr/bin/env bash
    10. nohup java -jar jenkins.war --ajp13Port=-1 --httpPort=8999 > log.file 2>&1 &
    1. [Jenkins Pipeline]
    2. # 一套插件, 将持续交付的实现和实施集成到 Jenkins 中
    3. // Jenkins Pipeline 的定义通常被写入到一个文本文件(称为 Jenkinsfile)中
    4. # 步骤 step
    5. [Jenkinsfile]
    6. : https://jenkins.io/doc/book/pipeline/jenkinsfile/
    7. //
    8. Jenkinsfile (Declarative Pipeline)
    9. pipeline {
    10. agent any
    11. stages {
    12. stage('Build') {
    13. steps {
    14. sh 'echo "Hello World"'
    15. sh '''
    16. echo "Multiline shell steps works too"
    17. ls -lah
    18. '''
    19. }
    20. }
    21. }
    22. }
    23. //
    24. retry(3) {
    25. sh './flakey-deploy.sh'
    26. }
    27. timeout(time: 3, unit: 'MINUTES') {
    28. sh './health-check.sh'
    29. }
    30. //
    31. timeout(time: 3, unit: 'MINUTES') {
    32. retry(5) {
    33. sh './flakey-deploy.sh'
    34. }
    35. }
    36. // 完成
    37. post {
    38. always {
    39. echo 'This will always run'
    40. }
    41. success {
    42. echo 'This will run only if successful'
    43. }
    44. failure {
    45. echo 'This will run only if failed'
    46. }
    47. unstable {
    48. echo 'This will run only if the run was marked as unstable'
    49. }
    50. changed {
    51. echo 'This will run only if the state of the Pipeline has changed'
    52. echo 'For example, if the Pipeline was previously failing but is now successful'
    53. }
    54. }
    1. [agent]
    2. # 定义执行环境
    3. : Jenkins在哪里以及如何执行Pipeline或者Pipeline子集
    4. // 所有在块block中的步骤steps会被Jenkins保存在一个执行队列中。 一旦一个执行器 executor 是可以利用的,这些步骤将会开始执行。
    5. // 一个工作空间 workspace 将会被分配, 工作空间中会包含来自远程仓库的文件和一些用于Pipeline的工作文件
    6. // https://jenkins.io/doc/book/pipeline/syntax#agent
    7. [environment 环境变量]
    8. # 环境变量 (全局或阶段 (stage))
    9. //
    10. Jenkinsfile (Declarative Pipeline)
    11. pipeline {
    12. agent any
    13. environment {
    14. DISABLE_AUTH = 'true'
    15. DB_ENGINE = 'sqlite'
    16. }
    17. stages {
    18. stage('Build') {
    19. steps {
    20. sh 'printenv'
    21. }
    22. }
    23. }
    24. }
    25. [清理通知]
    26. Jenkinsfile (Declarative Pipeline)
    27. pipeline {
    28. agent any
    29. stages {
    30. stage('No-op') {
    31. steps {
    32. sh 'ls'
    33. }
    34. }
    35. }
    36. post {
    37. always {
    38. echo 'One way or another, I have finished'
    39. deleteDir() /* clean up our workspace */
    40. }
    41. success {
    42. echo 'I succeeeded!'
    43. }
    44. unstable {
    45. echo 'I am unstable :/'
    46. }
    47. failure {
    48. echo 'I failed :('
    49. }
    50. changed {
    51. echo 'Things were different before...'
    52. }
    53. }
    54. }
    55. // 电子邮件
    56. post {
    57. failure {
    58. mail to: 'team@example.com',
    59. subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
    60. body: "Something is wrong with ${env.BUILD_URL}"
    61. }
    62. }
    63. // Hipchat
    64. post {
    65. failure {
    66. hipchatSend message: "Attention @here ${env.JOB_NAME} #${env.BUILD_NUMBER} has failed.",
    67. color: 'RED'
    68. }
    69. }
    70. // Slack
    71. post {
    72. success {
    73. slackSend channel: '#ops-room',
    74. color: 'good',
    75. message: "The pipeline ${currentBuild.fullDisplayName} completed successfully."
    76. }
    77. }
    78. [部署]
    79. Jenkinsfile (Declarative Pipeline)
    80. pipeline {
    81. agent any
    82. stages {
    83. stage('Build') {
    84. steps {
    85. echo 'Building'
    86. }
    87. }
    88. stage('Test') {
    89. steps {
    90. echo 'Testing'
    91. }
    92. }
    93. stage('Deploy') {
    94. steps {
    95. echo 'Deploying'
    96. }
    97. }
    98. }
    99. }