惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
小众软件
小众软件
S
SegmentFault 最新的问题
GbyAI
GbyAI
量子位
爱范儿
爱范儿
L
LangChain Blog
Vercel News
Vercel News
A
About on SuperTechFans
腾讯CDC
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
美团技术团队
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium

俗子

awk常用命令及功能 istio 配置 k8s 节点控制 dns 私有服务器 SeaFile 网盘 golang 私有包 verdaccio 前端私有源 chrony 时间同步 k8s 问题集锦 gitlab k8s 部署 linux 多显示器管理 mysql记录 nginx 日常 服务器ssh登录配置 域名信息收集工具 ssh port forward redis拾遗-性能指标 逆向工具
jenkins
俗子 · 2024-04-11 · via 俗子

Jenkins 提供了软件开发的持续集成服务。它运行在 Servlet 容器中(例如 Apache Tomcat)。它支持软件配置管理(SCM)工具(包括 AccuRev SCM、CVS、Subversion、Git、Perforce、Clearcase 和 RTC),可以执行基于 Apache Ant 和 Apache Maven 的项目,以及任意的 Shell 脚本和 Windows 批处理命令。
官网: https://www.jenkins.io

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// helm 发布
def helmDeploy(Map args) {
// Helm 尝试部署
if (args.dry_run) {
println '尝试 Helm 部署,验证是否能正常部署'
sh 'helm repo update'
sh "helm upgrade --install ${args.name} --version ${args.version} --set image.repository=${args.repository} --set image.tag=${args.version} chartmuseum/${args.name} --dry-run --debug"
}
// Helm 正式部署
else {
println '正式 Helm 部署'
sh 'helm repo update'
sh "helm upgrade --install ${args.name} --version ${args.version} --set image.repository=${args.repository} --set image.tag=${args.version} chartmuseum/${args.name}"
}
}

def noticeRtx(String ids, String info) {}

pipeline {
agent any
parameters {
string(name: 'project_name', defaultValue: 'rbac-service', description: '项目名称')
string(name: 'repo_url', defaultValue: 'git@192.168.1.28:Web/rbac-service.git', description: 'git 仓库')
// Git Parameter
gitParameter(name: 'tag_name',
type: 'PT_TAG',
branchFilter: 'origin/(.*)',
defaultValue: '0.0.1',
selectedValue: 'DEFAULT',
sortMode: 'DESCENDING_SMART',
useRepository: env.repo_url,
description: 'git 标签')
}

stages {
stage('Init') {
steps {
sh 'printenv'
script {
if (env.gitlabSourceRepoSshUrl) {
env.repo_url = env.gitlabSourceRepoSshUrl
}
if (env.gitlabSourceRepoName) {
env.project_name = env.gitlabSourceRepoName
}
if (env.gitlabSourceBranch) {
env.tag_name = env.gitlabSourceBranch.split('/')[-1]
}
}
buildName "#${BUILD_NUMBER}-${project_name}-${tag_name}"
}
}

stage('Source') {
steps {
checkout([$class: 'GitSCM', branches: [[name: "refs/tags/${tag_name}"]], userRemoteConfigs: [[url: "${repo_url}"]]])
script {
env.imagePath = project_name
env.repository = env.imagePath + ':' + env.tag_name
env.commitInfo = sh(script:'git log --oneline --no-merges|head -1', returnStdout: true)
}
echo 'repository: ' + env.repository
}
}

stage('Build') {
steps {
echo 'build repository: ' + env.repository
script {
docker.withRegistry('https://hub.docker.com') {
def customImage = docker.build(repository)
customImage.push()

sh 'docker images | grep citest | awk \'{print $1":"$2}\' | xargs docker rmi -f || true'
sh 'docker rmi -f `docker images | grep \'<none>\' | awk \'{print $3}\'` || true'
}
}
}
}

stage('PackageHelm') {
steps {
echo 'package helm'
sh 'helm package ' + 'chart/' + project_name + ' --app-version ' + env.tag_name + ' --version ' + env.tag_name
echo 'push helm'
sh 'helm cm-push ' + env.project_name + '-' + env.tag_name + '.tgz chartmuseum -f'
}
}

stage('Deploy') {
steps {
withKubeConfig([credentialsId: 'k8s 密钥id', serverUrl: 'k8s地址']) {
// 执行 Helm 方法
echo 'Helm 执行部署测试'
helmDeploy(dry_run: true ,name: env.project_name , repository: env.imagePath , version: env.tag_name)
echo 'Helm 执行正式部署'
helmDeploy(dry_run: false, name: env.project_name, repository: env.imagePath, version: env.tag_name)
}
}
}

stage('Clean') {
steps {
cleanWs(
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: true,
cleanWhenSuccess: true,
cleanWhenUnstable: true,
cleanupMatrixParent: true,
disableDeferredWipeout: true,
deleteDirs: true
)
}
}
}

post {
always {
echo env.project_name + ': 项目构建完成'
}
success {
echo env.project_name + ': 构建成功'
noticeRtx("", env.user + '成功构建了: ' + env.project_name + '\n版本号: ' + env.tag_name + '\n更新内容: \n' + env.commitInfo)
}
unstable {
echo env.project_name + ': 构建过程报错'
noticeRtx("", env.user + '构建: ' + env.project_name + '失败了 \n版本号: ' + env.tag_name + '\n更新内容: \n' + env.commitInfo)
}
failure {
echo env.project_name + ': 构建失败'
noticeRtx("", env.user + '构建: ' + env.project_name + '失败了 \n版本号: ' + env.tag_name + '\n更新内容: \n' + env.commitInfo)
}
}
}