Jenkinsfile怎么写

目录

[1. 理解 Jenkins 和 Jenkinsfile](#1. 理解 Jenkins 和 Jenkinsfile)

[2. 基本概念](#2. 基本概念)

[3. Jenkinsfile 结构](#3. Jenkinsfile 结构)

[示例:Declarative Pipeline](#示例:Declarative Pipeline)

各部分拆解讲解

[Pipeline 声明](#Pipeline 声明)

[Agent 声明](#Agent 声明)

[Environment 环境变量](#Environment 环境变量)

Options

Stages

Steps

[Post 部分](#Post 部分)

[4. 学习资料](#4. 学习资料)

[5. 实践运用](#5. 实践运用)


学习和运用 Jenkinsfile 是熟练使用 Jenkins 进行持续集成和持续交付(CI/CD)流程的关键一步。这段时间也是学有所成 和大家分享下学习成果:

1. 理解 Jenkins 和 Jenkinsfile

  • Jenkins 是一个开源的自动化服务器,可以用于构建、测试、部署软件项目。
  • Jenkinsfile 是用来定义 Jenkins Pipeline 的文本文件,使用领域特定语言(DSL)编写。它允许你将构建过程作为代码来管理,便于版本控制和协作。

2. 基本概念

  • Pipeline:表示工作流的完整过程,由一系列阶段(Stage)构成。
  • Stage:Pipeline 中的逻辑划分,每个阶段可以包含一个或多个步骤。
  • Step:执行具体动作的基本构建单元,例如运行命令、构建项目。

3. Jenkinsfile 结构

Jenkinsfile 可以用两种不同的语法编写:Declarative 和 Scripted。Declarative 是较新的语法,更加简单和结构化。

示例:Declarative Pipeline
python 复制代码
pipeline {
    agent any 

    stages {
        stage('Build') { 
            steps {
                echo 'Building..'
                // Add build steps here
            }
        }
        stage('Test') { 
            steps {
                echo 'Testing..'
                // Add test steps here
            }
        }
        stage('Deploy') { 
            steps {
                echo 'Deploying....'
                // Add deploy steps here
            }
        }
    }
}
各部分拆解讲解
Pipeline 声明
python 复制代码
pipeline {
    ...
}
  • 声明这是一个 Pipeline 脚本,所有内容需包含在大括号内。
Agent 声明
python 复制代码
agent any
  • 指定在任何可用的 Jenkins 节点上执行 Pipeline。具体的节点可以通过指定标签来限定。
Environment 环境变量
python 复制代码
environment {
    NODE_ENV = 'production'
}
  • 定义一个环境变量 NODE_ENV,在 Pipeline 的所有步骤中都可以使用。
Options
python 复制代码
options {
    timeout(time: 1, unit: 'HOURS')
}
  • 定义一些全局性的选项,如设置 Pipeline 的超时时间为 1 小时,防止长时间运行;或者加入一些定时的操作(利用cron)
Stages
python 复制代码
stages {
    stage('Stage Name') {
        steps {
            // ...
        }
    }
    ...
}
  • 定义 Pipeline 的各个阶段(Stage)。每个阶段包含步骤(Steps),具体执行的操作写在步骤中。
Steps
  • Checkout 代码

    python 复制代码
    stage('Checkout') { 
        steps {
            checkout scm
        }
    }
    • 使用 checkout scm 从版本控制系统中检出代码,通常会从 Git 仓库拉取源代码。
  • Build 构建

    python 复制代码
    stage('Build') {
        steps {
            sh 'npm install'
        }
    }
    • 在这个阶段,通过 sh 执行 Shell 命令来安装项目依赖。
  • Test 测试

    python 复制代码
    stage('Test') {
        steps {
            sh 'npm test'
        }
    }
    • 运行测试命令,确保代码无误。
  • Deploy 部署

    python 复制代码
    stage('Deploy') {
        steps {
            input message: 'Approve deployment?', ok: 'Deploy'
            sh 'npm run deploy'
        }
    }
    • 在部署阶段之前,使用 input 来进行手动审批,然后运行部署脚本。
Post 部分
python 复制代码
post {
    success {
        echo 'Pipeline succeeded!'
    }
    failure {
        echo 'Pipeline failed.'
    }
    always {
        cleanWs()
    }
}
  • success: 当 Pipeline 成功时执行。
  • failure: 当 Pipeline 失败时执行。
  • always: 不论成功与否,都执行,比如清理工作空间或者发送邮件到个人邮箱。

4. 学习资料

  • 官方文档 :Jenkins 官方提供了全面的 使用指南,包括 Jenkinsfile。
  • 在线课程:许多平台提供 Jenkins 和 Jenkinsfile 的在线培训课程。
  • 开源项目:在 GitHub 等平台上查找使用 Jenkins 的开源项目,阅读它们的 Jenkinsfile,很有帮助。

5. 实践运用

  • 创建 Jenkinsfile:为你的项目创建一个 Jenkinsfile,并将其添加到版本控制仓库中。
  • 配置 Jenkins:在 Jenkins 中创建一个新任务,指向包含 Jenkinsfile 的项目仓库。
  • 迭代开发:随着项目需求的改变,不断更新和优化 Jenkinsfile。

通过对 Jenkinsfile 的学习和实践,可以大大提升项目的自动化程度和团队的协作效率。如果你有具体问题或需要更多的指导,欢迎提出!

相关推荐
软件测试-阿涛9 小时前
【性能测试】Jmeter+Grafana+InfluxDB+Prometheus Windows安装部署教程
测试工具·jmeter·性能优化·压力测试·grafana·prometheus
惜.己1 天前
pytest中使用skip跳过某个函数
开发语言·python·测试工具·pytest
慧都小项1 天前
自动化UI测试工具TestComplete的AI双引擎:即时数据集 + 自愈测试
自动化测试·测试工具·llm·数据驱动测试·hipaa标准
alien爱吃蛋挞1 天前
Postman
测试工具·postman
程序员小远2 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
Feng.Lee3 天前
接口测试Postman工具高级使用技巧
功能测试·测试工具·lua·postman·可用性测试
bestsun9993 天前
Notepad++工具操作技巧
测试工具·notepad++
yy鹈鹕灌顶3 天前
Selenium+Java 自动化测试入门到实践:从环境搭建到元素操作
java·selenium·测试工具