Files
Work-configuration-file/jenkins/流水线配置/流水线脚本规则.conf
2025-10-07 15:58:15 +08:00

36 lines
1.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 脚本式流水线完整结构示例(标注包含关系)
pipeline { // 1. 根容器:所有流水线逻辑的顶层包裹
agent any // 代理配置(指定执行环境)
stages { // 2. 阶段集合包含所有「stage」必须有且只能有1个
stage('构建') { // 3. 单个阶段包含「steps」或「script」至少1个可多个
steps { // 4. 步骤容器:直接包裹「具体执行命令」(如 sh、echo
echo "开始构建Go项目"
sh "go build -o app main.go" // 具体步骤执行shell命令
}
}
stage('部署实例A') { // 第二个阶段
steps {
script { // 5. 脚本块嵌套在「steps」内用于编写复杂逻辑如循环、判断
def targetHost = "192.168.1.100"
echo "向实例A发送优雅退出信号"
// 复杂逻辑轮询检查进程状态需用script包裹
def elapsed = 0
while (elapsed < 60) {
sh "ssh user@${targetHost} 'ps -ef | grep app | grep -v grep'"
sleep 2
elapsed += 2
}
}
// script外仍可写简单步骤
sh "scp app user@${targetHost}:/opt/app/" // 同步新产物
}
}
}
post { // 可选:流水线结束后执行的操作(如通知、清理)
success { echo "流水线执行成功!" }
failure { echo "流水线执行失败!" }
}
}