需求提出
jenkins 的自动化操作可以基于脚本实现,脚本主要就是pipeline的东西。在之前旧的项目中就保留着很多的源代码(jenkins的)虽然自己摸索摸索就知道了三七二十一;但是很多细节的东西依旧没有包含。
答疑解惑
由于不能直接使用公司的源码,提供如下的模版进行学习;
基本的含义和json类似,简单阅读一下就明白大概的内容是个啥。agent代理-stage阶段
pipeline{
agent {
label 'Production'
}
stages {
stage('Build') {
steps {
echo 'Building'
}
}
stage('Test') {
steps {
echo 'Testing'
}
}
stage('Deploy - Staging') {
steps {
sh './deploy staging' sh './run-smoke-tests'
}
}
stage('Sanity check') {
steps {
input "Does the staging environment look ok?"
}
}
stage('Deploy - Production') {
steps {
sh './deploy production'
}
}
}
post {
always {
echo 'One way or another, I have finished' deleteDir() /* clean up our workspace */
}
success {
echo 'I succeeeded!'
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
}
其中还有一个参数parameter上诉代码中没有展示。
jenkins pipeline的parameters参数介绍
例如这样:
parameters {
string(name: 'BRANCH_NAME', defaultValue: "", description: "指定分支或者tag就填这里,选择分支就选下面的 BRANCH_SELECTOR")
choice name: 'BRANCH_SELECTOR', choices: ['', 'feature/v1.9.12_open_source','feature/v1.9.11_person_certer','feature/v1.10.0'], description: ''
string(name: 'BUILD_VERSION', defaultValue: "1.0.0", description: "版本号")
choice name: 'BUILD_TYPE', choices: ['Release', 'Debug'], description: ''
}
每个参数都可以对相关的py脚本起到相关作用,
例如:
class BuildExecutor:
def __init__(self):
self.artifact_project_name = "goapp"
self.artifact_group_name = "public"
self.branch_name = os.environ.get("BRANCH_NAME") + os.environ.get("BRANCH_SELECTOR")
self.store_folder = "feature/" # 默认存放feature目录
for start in ['feature/', 'release/', 'customer/']:
if self.branch_name.startswith(start):
self.store_folder = start
通过os.environ.get
进行获取。