-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
138 lines (124 loc) · 4.98 KB
/
Jenkinsfile
File metadata and controls
138 lines (124 loc) · 4.98 KB
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
137
138
pipeline {
agent { label 'centos' }
environment {
OUTPUT_DIR = 'build'
BRANCH_NAME = "${env.BRANCH}"
GITHUB_REPO = "class-scheduling-system/table-install-cli"
GITHUB = credentials('github-token')
TAG_VERSION = ''
}
stages {
stage('获取分支信息') {
steps {
script {
if (BRANCH_NAME == 'null' || BRANCH_NAME == '') {
BRANCH_NAME = sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
} else if (BRANCH_NAME.startsWith('refs/tags/')) {
echo("❌ 不能在 Tag 分支上执行操作!")
return;
}
}
echo "当前分支是 ${BRANCH_NAME},执行操作..."
}
}
stage('签出代码') {
when {
expression { BRANCH_NAME == 'refs/heads/master' }
}
steps {
echo "拉取代码中..."
checkout scm
}
}
stage('项目打包') {
when {
expression { BRANCH_NAME == 'refs/heads/master' }
}
steps {
script {
// 生成时间戳版本号
TAG_VERSION = sh(script: "date +%Y%m%d%H%M%S", returnStdout: true).trim()
echo "清空构建目录..."
sh "rm -rf ${WORKSPACE}/build/${TAG_VERSION}"
sh "mkdir -p ${WORKSPACE}/build/${TAG_VERSION}"
echo "编译项目..."
def platforms = [
[os: 'linux', arch: 'amd64'],
[os: 'linux', arch: 'arm64'],
[os: 'darwin', arch: 'amd64'],
[os: 'darwin', arch: 'arm64'],
[os: 'windows', arch: 'amd64'],
[os: 'windows', arch: 'arm64']
]
platforms.each { platform ->
def outputFile = "./build/${TAG_VERSION}/cli-${platform.os}-${platform.arch}${platform.os == 'windows' ? '.exe' : ''}"
sh """
cd ${WORKSPACE}
GOOS=${platform.os} GOARCH=${platform.arch} go build -o ${outputFile}
echo "完成打包: ${outputFile}"
"""
}
}
}
}
stage('收集构建产物') {
when {
expression { BRANCH_NAME == 'refs/heads/master' }
}
steps {
script {
echo "版本号: ${TAG_VERSION}"
echo "收集所有构建产物..."
sh "ls -al ${WORKSPACE}/build/${TAG_VERSION}"
}
}
}
stage('发布到 GitHub Release') {
when {
expression { BRANCH_NAME == 'refs/heads/master' }
}
steps {
script {
def RELEASE_NAME = "RELEASE-${TAG_VERSION}"
def CHANGELOG_FILE = "CHANGELOG.md"
echo "生成 CHANGELOG..."
sh """
echo "# DESCRIPTION" > ${CHANGELOG_FILE}
echo "\$(git log -1 --pretty=format:'%B') \n" >> ${CHANGELOG_FILE}
echo "# CHANGELOG" >> ${CHANGELOG_FILE}
# 获取上一个 tag
PREVIOUS_TAG=\$(git describe --tags --abbrev=0 HEAD^)
echo "上一个 tag: \${PREVIOUS_TAG}"
# 仅获取当前 tag 和上一个 tag 之间的日志
git log \${PREVIOUS_TAG}..HEAD --pretty=format:'- %s (@%an) [%h]' >> ${CHANGELOG_FILE}
echo "" >> ${CHANGELOG_FILE}
"""
def loginStatus = sh(
script: "echo $GITHUB | gh auth login --with-token && gh auth status | grep 'Logged in'",
returnStatus: true
)
if (loginStatus != 0) {
error "❌ GitHub CLI 登录失败,请检查 GITHUB_TOKEN 是否正确!"
} else {
echo "✅ GitHub CLI 登录成功"
}
sh """
echo "发布到 GitHub Release..."
gh release create ${TAG_VERSION} ${OUTPUT_DIR}/${TAG_VERSION}/** \\
--repo ${GITHUB_REPO} \\
--title "${RELEASE_NAME}" \\
--notes-file ${CHANGELOG_FILE}
"""
}
}
}
}
post {
success {
echo "流水线执行成功 🎉"
}
failure {
echo "流水线执行失败,请检查日志 ❌"
}
}
}