第一个任务 #
本节将指导你创建第一个Jenkins任务,从简单的Hello World开始,逐步了解Jenkins的基本操作流程。
任务类型概览 #
Jenkins支持多种任务类型:
| 类型 | 说明 | 适用场景 |
|---|---|---|
| Freestyle Project | 自由风格项目 | 简单构建任务 |
| Pipeline | 流水线项目 | 复杂CI/CD流程 |
| Multi-configuration Project | 多配置项目 | 多平台/多环境构建 |
| Folder | 文件夹 | 组织管理任务 |
| Multibranch Pipeline | 多分支流水线 | Git分支自动发现 |
创建第一个Freestyle任务 #
步骤1:创建新任务 #
- 点击Jenkins首页的 New Item 或 创建任务
- 输入任务名称:
hello-world - 选择 Freestyle project
- 点击 OK
步骤2:配置任务 #
基本配置 #
text
Description: 我的第一个Jenkins任务
构建步骤 #
点击 Add build step → Execute shell
bash
echo "Hello, Jenkins!"
echo "当前时间: $(date)"
echo "构建编号: ${BUILD_NUMBER}"
echo "工作目录: ${WORKSPACE}"
步骤3:保存并运行 #
- 点击 Save 保存配置
- 点击 Build Now 执行构建
步骤4:查看构建结果 #
- 点击构建历史中的构建编号(如
#1) - 点击 Console Output 查看输出
text
Started by user admin
Running as SYSTEM
Building on the built-in node in workspace /var/jenkins_home/workspace/hello-world
[hello-world] $ /bin/sh -xe /tmp/jenkins123456.sh
+ echo 'Hello, Jenkins!'
Hello, Jenkins!
+ echo '当前时间: Sat Mar 28 10:00:00 UTC 2026'
当前时间: Sat Mar 28 10:00:00 UTC 2026
+ echo '构建编号: 1'
构建编号: 1
+ echo '工作目录: /var/jenkins_home/workspace/hello-world'
工作目录: /var/jenkins_home/workspace/hello-world
Finished: SUCCESS
常用环境变量 #
Jenkins提供了丰富的内置环境变量:
| 变量 | 说明 | 示例 |
|---|---|---|
| BUILD_NUMBER | 构建编号 | 1, 2, 3… |
| BUILD_ID | 构建ID | 2026-03-28_10-00-00 |
| JOB_NAME | 任务名称 | hello-world |
| JOB_URL | 任务URL | http://localhost:8080/job/hello-world/ |
| WORKSPACE | 工作空间路径 | /var/jenkins_home/workspace/hello-world |
| JENKINS_HOME | Jenkins主目录 | /var/jenkins_home |
| JENKINS_URL | Jenkins URL | http://localhost:8080/ |
| NODE_NAME | 节点名称 | master |
| BUILD_URL | 构建URL | http://localhost:8080/job/hello-world/1/ |
使用环境变量 #
bash
#!/bin/bash
echo "=== 构建信息 ==="
echo "任务名称: ${JOB_NAME}"
echo "构建编号: ${BUILD_NUMBER}"
echo "节点名称: ${NODE_NAME}"
echo "工作空间: ${WORKSPACE}"
echo "Jenkins URL: ${JENKINS_URL}"
添加Git仓库 #
步骤1:安装Git插件 #
确保已安装 Git Plugin(通常在推荐插件中已安装)。
步骤2:配置源码管理 #
- 进入任务配置页面
- 勾选 Source Code Management → Git
- 输入仓库URL:
text
Repository URL: https://github.com/user/repo.git
Branch Specifier: */main
步骤3:配置凭据 #
如果仓库需要认证:
- 点击 Add → Jenkins
- 选择凭据类型:
- Username with password
- SSH Username with private key
- 保存凭据
步骤4:添加构建步骤 #
bash
#!/bin/bash
echo "=== 检出代码 ==="
git log -1 --pretty=format:"%h - %an, %ar : %s"
echo ""
echo "=== 列出文件 ==="
ls -la
echo ""
echo "=== 构建项目 ==="
# 根据项目类型执行构建命令
# npm install && npm run build
# mvn clean package
# gradle build
添加构建触发器 #
手动触发 #
点击 Build Now 手动触发构建。
定时触发 #
勾选 Build Triggers → Build periodically
text
Schedule: H/15 * * * *
Cron表达式说明:
text
┌───────────── 分钟 (0 - 59)
│ ┌───────────── 小时 (0 - 23)
│ │ ┌───────────── 日 (1 - 31)
│ │ │ ┌───────────── 月 (1 - 12)
│ │ │ │ ┌───────────── 星期 (0 - 6) (0 是周日)
│ │ │ │ │
* * * * *
常用示例:
text
H/15 * * * * 每15分钟
H * * * * 每小时
H 2 * * * 每天凌晨2点
H 2 * * 1 每周一凌晨2点
H 2 1 * * 每月1日凌晨2点
轮询SCM #
勾选 Build Triggers → Poll SCM
text
Schedule: H/5 * * * *
每5分钟检查一次代码变更,有变更则触发构建。
远程触发 #
勾选 Build Triggers → Trigger builds remotely
text
Authentication Token: my-token
触发URL:
text
http://JENKINS_URL/job/hello-world/build?token=my-token
构建后操作 #
归档构建产物 #
勾选 Post-build Actions → Archive the artifacts
text
Files to archive: target/*.jar
发送邮件通知 #
勾选 Post-build Actions → E-mail Notification
text
Recipients: team@example.com
触发其他任务 #
勾选 Post-build Actions → Build other projects
text
Projects to build: downstream-job
Trigger only if build is stable
创建第一个Pipeline任务 #
步骤1:创建Pipeline任务 #
- 点击 New Item
- 输入名称:
hello-pipeline - 选择 Pipeline
- 点击 OK
步骤2:配置Pipeline #
在 Pipeline 部分,选择 Pipeline script:
groovy
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello, Jenkins Pipeline!'
}
}
stage('Info') {
steps {
script {
echo "构建编号: ${BUILD_NUMBER}"
echo "任务名称: ${JOB_NAME}"
}
}
}
stage('Environment') {
steps {
sh 'printenv | grep JENKINS'
}
}
}
post {
always {
echo '构建完成!'
}
success {
echo '构建成功!'
}
failure {
echo '构建失败!'
}
}
}
步骤3:运行Pipeline #
点击 Build Now 执行Pipeline。
Pipeline从SCM加载 #
步骤1:创建Jenkinsfile #
在项目根目录创建 Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'make build'
}
}
stage('Test') {
steps {
sh 'make test'
}
}
stage('Deploy') {
when {
branch 'main'
}
steps {
sh 'make deploy'
}
}
}
}
步骤2:配置Pipeline from SCM #
- 在Pipeline配置中,选择 Pipeline script from SCM
- 选择 Git
- 输入仓库URL
- 设置 Script Path:
Jenkinsfile
任务配置最佳实践 #
1. 使用有意义的名称 #
text
Good: my-app-ci-build
Bad: job1, test
2. 添加描述 #
text
Description:
构建my-app项目的CI流水线
- 编译代码
- 运行单元测试
- 生成测试报告
3. 限制并发构建 #
勾选 Execute concurrent builds if necessary
4. 设置构建超时 #
groovy
options {
timeout(time: 30, unit: 'MINUTES')
}
5. 丢弃旧构建 #
groovy
options {
buildDiscarder(logRotator(
numToKeepStr: '10',
artifactNumToKeepStr: '5'
))
}
下一步学习 #
小结 #
- Freestyle任务适合简单构建
- Pipeline是推荐的现代方式
- 环境变量提供丰富的构建信息
- 支持多种触发方式
- 构建后操作可以扩展功能
最后更新:2026-03-28