自由风格任务 #
自由风格项目(Freestyle Project)是Jenkins最基础的任务类型,通过图形化界面进行配置,适合简单的构建任务。
任务结构 #
text
┌─────────────────────────────────────────────────────────────┐
│ Freestyle Project │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ General (通用配置) │ │
│ │ - 描述 │ │
│ │ - 禁止并发构建 │ │
│ │ - 构建后清理工作空间 │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Source Code Management (源码管理) │ │
│ │ - Git / SVN / Mercurial │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Build Triggers (构建触发器) │ │
│ │ - 手动触发 / 定时触发 / 远程触发 │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Build Environment (构建环境) │ │
│ │ - 环境变量 / 工具选择 / 证书 │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Build Steps (构建步骤) │ │
│ │ - Shell / 批处理 / Maven / Gradle │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Post-build Actions (构建后操作) │ │
│ │ - 归档产物 / 邮件通知 / 触发其他任务 │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
通用配置 (General) #
描述 #
text
Description:
这是一个Java项目的构建任务
- 编译源代码
- 运行单元测试
- 打包JAR文件
禁止并发构建 #
text
☑ Do not allow concurrent builds
Execute concurrent builds if necessary: ☐
构建后清理 #
text
☑ Delete workspace before build starts
构建丢弃策略 #
text
☑ Discard old builds
Strategy: Log Rotation
Max # of builds to keep: 10
Max # of builds to keep with artifacts: 5
参数化构建 #
text
☑ This project is parameterized
添加参数:
- Boolean Parameter
Name: DEPLOY
Default Value: false
- Choice Parameter
Name: ENVIRONMENT
Choices: dev, test, prod
- String Parameter
Name: VERSION
Default Value: 1.0.0
源码管理 (Source Code Management) #
Git配置 #
text
☑ Git
Repository URL: https://github.com/user/repo.git
Credentials: github-credentials
Branch Specifier: */main
高级Git配置 #
text
Additional Behaviours:
- Checkout to specific local branch
Local branch: main
- Clean after checkout
- Clean before checkout
- Sparse Checkout paths
Path: src/main
- Submodule options
☑ Recursively update submodules
多仓库配置 #
text
Repositories:
Repository 1:
URL: https://github.com/user/repo1.git
Name: repo1
Repository 2:
URL: https://github.com/user/repo2.git
Name: repo2
SVN配置 #
text
☑ Subversion
Repository URL: svn://svn.example.com/repo/trunk
Credentials: svn-credentials
Local module directory: .
构建触发器 (Build Triggers) #
手动触发 #
点击 Build Now 手动触发。
定时触发 #
text
☑ Build periodically
Schedule: H 2 * * *
常用Cron表达式:
text
H/15 * * * * 每15分钟
H * * * * 每小时
H 2 * * * 每天凌晨2点
H 2 * * 1-5 工作日凌晨2点
H 2 1 * * 每月1日凌晨2点
轮询SCM #
text
☑ Poll SCM
Schedule: H/5 * * * *
远程触发 #
text
☑ Trigger builds remotely
Authentication Token: build-token-123
触发URL:
bash
curl -X POST http://JENKINS_URL/job/my-job/build?token=build-token-123
其他任务触发 #
text
☑ Build after other projects are built
Projects to watch: upstream-job
Trigger only if build is stable
GitLab/GitHub触发 #
text
☑ Build when a change is pushed to GitLab
☑ Push Events
☑ Merge Request Events
构建环境 (Build Environment) #
环境变量 #
text
☑ Inject environment variables to the build process
Properties Content:
APP_NAME=myapp
BUILD_ENV=production
JAVA_HOME=/usr/lib/jvm/java-11
工具选择 #
text
☑ Use secret text(s) or file(s)
☑ JDK: JDK 11
☑ Maven: Maven 3.8
☑ Git: Default
证书绑定 #
text
☑ Use secret text(s) or file(s)
Variable: DOCKER_PASSWORD
Credentials: docker-hub-credentials
超时设置 #
text
☑ Abort the build if it's stuck
Time-out strategy: Absolute
Timeout minutes: 30
工作空间清理 #
text
☑ Delete workspace before build starts
☑ Wipe out current work space
构建步骤 (Build Steps) #
Execute Shell #
bash
#!/bin/bash
set -e
echo "=== 开始构建 ==="
echo "构建编号: ${BUILD_NUMBER}"
echo "工作目录: ${WORKSPACE}"
echo "=== 编译代码 ==="
mvn clean compile
echo "=== 运行测试 ==="
mvn test
echo "=== 打包应用 ==="
mvn package -DskipTests
echo "=== 构建完成 ==="
Execute Windows Batch #
batch
@echo off
echo === 开始构建 ===
echo 构建编号: %BUILD_NUMBER%
echo === 编译代码 ===
call mvn clean compile
echo === 运行测试 ===
call mvn test
echo === 打包应用 ===
call mvn package -DskipTests
echo === 构建完成 ===
Invoke Maven #
text
Maven Version: Maven 3.8
Goals: clean package
POM: pom.xml
Properties:
skipTests=false
environment=test
Invoke Gradle #
text
Gradle Version: Gradle 7.x
Tasks: clean build
Switches: --info
执行Python脚本 #
bash
#!/bin/bash
python3 << 'EOF'
import os
import sys
print(f"Python版本: {sys.version}")
print(f"工作目录: {os.getcwd()}")
# 执行Python代码
os.system("pip install -r requirements.txt")
os.system("python manage.py test")
EOF
构建后操作 (Post-build Actions) #
归档构建产物 #
text
☑ Archive the artifacts
Files to archive: target/*.jar, target/*.war
Exclude files: target/*-sources.jar
发布JUnit测试报告 #
text
☑ Publish JUnit test result report
Test report XMLs: **/target/surefire-reports/*.xml
Health report amplification factor: 1.0
发送邮件通知 #
text
☑ E-mail Notification
Recipients: team@example.com
☑ Send e-mail for every unstable build
☑ Send separate e-mails to individuals who broke the build
触发其他任务 #
text
☑ Build other projects
Projects to build: downstream-job
Trigger only if build is stable
构建状态设置 #
text
☑ Set build result
Set build result to: UNSTABLE
When: File exists
File path: test-results/failure.txt
完整配置示例 #
Java项目构建 #
text
General:
Description: Java项目CI构建
☑ Discard old builds (10 builds)
☑ This project is parameterized
- Choice: ENVIRONMENT (dev/test/prod)
Source Code Management:
☑ Git
Repository URL: https://github.com/user/java-app.git
Branch: */main
Build Triggers:
☑ Poll SCM: H/5 * * * *
Build Environment:
☑ JDK: JDK 11
☑ Inject environment variables
APP_NAME=java-app
Build Steps:
1. Execute Shell:
mvn clean package
2. Execute Shell:
mvn test
Post-build Actions:
☑ Archive the artifacts: target/*.jar
☑ Publish JUnit test result report
☑ E-mail Notification: team@example.com
Node.js项目构建 #
bash
#!/bin/bash
echo "=== 安装依赖 ==="
npm install
echo "=== 代码检查 ==="
npm run lint
echo "=== 运行测试 ==="
npm test
echo "=== 构建应用 ==="
npm run build
echo "=== 构建完成 ==="
ls -la dist/
Docker镜像构建 #
bash
#!/bin/bash
IMAGE_NAME="myapp"
IMAGE_TAG="${BUILD_NUMBER}"
echo "=== 构建Docker镜像 ==="
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
echo "=== 推送到仓库 ==="
docker push ${IMAGE_NAME}:${IMAGE_TAG}
echo "=== 清理旧镜像 ==="
docker image prune -f
最佳实践 #
1. 使用参数化构建 #
text
☑ This project is parameterized
- Choice: ENVIRONMENT (dev/test/prod)
- Boolean: SKIP_TESTS
- String: VERSION
2. 设置构建超时 #
text
☑ Abort the build if it's stuck
Timeout: 30 minutes
3. 清理工作空间 #
text
☑ Delete workspace before build starts
4. 使用环境变量文件 #
bash
source .env
echo "环境: ${ENVIRONMENT}"
5. 错误处理 #
bash
#!/bin/bash
set -e # 遇到错误立即退出
set -u # 使用未定义变量时报错
trap 'echo "构建失败于第 $LINENO 行"' ERR
常见问题 #
1. Shell脚本权限问题 #
bash
chmod +x script.sh
./script.sh
2. 环境变量不生效 #
bash
source /etc/profile
source ~/.bashrc
3. 构建卡住 #
- 检查是否有交互式命令
- 设置构建超时
- 检查资源是否充足
下一步学习 #
小结 #
- Freestyle通过图形界面配置
- 支持多种源码管理工具
- 提供丰富的构建触发器
- 支持多种构建步骤类型
- 构建后操作扩展功能
- 适合简单到中等复杂度的任务
最后更新:2026-03-28