Skip to content

Commit 2893353

Browse files
committed
feat: add staging repo and delete old repo
Signed-off-by: Meng JiaFeng <[email protected]>
1 parent e835bf4 commit 2893353

File tree

32 files changed

+995
-90
lines changed

32 files changed

+995
-90
lines changed

.yamllint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ ignore: |
3232
**/.github/**
3333
**/githubactions/**
3434
**/workflows/**
35+
**/staging/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# gitlab-share-library
2+
3+
This repo contains share library used by DevStream plugin "gitlab-ci" (thereafter: the plugin). It currently has the following functions:
4+
- send notification to dingtalk when pipeline result is success or failed.
5+
- build docker image by current project, and push this image to image repo.
6+
7+
## General config variables
8+
9+
| field | description | default_value |
10+
| ---- | ---- | ---- |
11+
| CI_REGISTRY_USER | image repo owner | |
12+
| CI_REGISTRY_PASSWORD | image repo owner's password | |
13+
14+
## Where does this repo come from?
15+
16+
`gitlab-share-library` is synced from https://github.com/devstream-io/devstream/blob/main/staging/gitlab-share-library.
17+
Code changes are made in that location, merged into `devstream-io/devstream` and later synced here.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.build-docker:
2+
stage: build
3+
script:
4+
- auth=`echo -n "$CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD" | base64`
5+
- mkdir -p ~/.docker
6+
# - authfile=`{"auths": {"test": {"auth": "$auth"}}}`
7+
# - echo $authfile > ~/.docker/config.json
8+
- buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=. --output type=image,name=${imageRepo}:${defaultTag},push=true${opts}
9+
- echo "SUCCESS" > .job_status
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 钉钉消息发送模版任务
2+
# 必须变量
3+
# DINGTALK_ACCESS_TOKEN 群机器人token
4+
5+
variables:
6+
# 钉钉markdown换行符 必须\n且前后跟两个空格(shell 转义)
7+
V_BR: "\ \ \\n\ \ "
8+
9+
# 消息发送准备工作
10+
# 检测钉钉消息发送access_token是否存在
11+
.prepare: &prepare
12+
# token检验
13+
- |
14+
if [ -z $DINGTALK_ACCESS_TOKEN ]; then
15+
echo "使用钉钉消息发送必须配置DINGTALK_ACCESS_TOKEN变量"
16+
exit 1
17+
fi
18+
- |
19+
if [ -z $DINGTALK_SECURITY_VALUE ]; then
20+
echo "使用钉钉消息发送必须配置DINGTALK_SECURITY_VALUE变量"
21+
exit 1
22+
fi
23+
# url编码项目地址及任务地址
24+
- |
25+
project_url="$(curl -s -o /dev/null -w %{url_effective} --get --data-urlencode "${GITLAB_URL}/${CI_PROJECT_PATH}/-/tree/${CI_BUILD_REF_NAME}" "" || true)"
26+
job_url="$(curl -s -o /dev/null -w %{url_effective} --get --data-urlencode "${GITLAB_URL}/${CI_PROJECT_PATH}/-/jobs/${CI_JOB_ID}" "" || true)"
27+
28+
# 钉钉消息发送http Anchors
29+
.send_request: &send_request
30+
# Markdown消息内容
31+
- |
32+
V_TEXT="**CI任务<font color=\\\"${V_COLOR}\\\">${V_STATUS}</font>通知**${V_BR}\
33+
任务ID: **${CI_JOB_ID}**${V_BR}\
34+
任务名: **${CI_JOB_NAME}**${V_BR}\
35+
项目: **${CI_PROJECT_PATH}**${V_BR}\
36+
分支: **${CI_BUILD_REF_NAME}**${V_BR}\
37+
执行人: **${GITLAB_USER_NAME}**\
38+
"
39+
# 钉钉消息发送json报文
40+
- |
41+
V_JSON="{
42+
\"actionCard\": {\
43+
\"title\": \"${V_TITLE}\",\
44+
\"text\": \"${V_TEXT}\", \
45+
\"btnOrientation\": \"1\",\
46+
\"btns\": [{\
47+
\"title\": \"查看项目\",
48+
\"actionURL\": \"dingtalk://dingtalkclient/page/link?url=${project_url##/?}&pc_slide=false\"
49+
}, {\
50+
\"title\": \"查看任务\",
51+
\"actionURL\": \"dingtalk://dingtalkclient/page/link?url=${job_url##/?}&pc_slide=false\"
52+
}]\
53+
},\
54+
\"msgtype\": \"actionCard\"\
55+
}"
56+
- >
57+
curl -s -H 'Content-Type: application/json; charset=utf-8' -X POST
58+
https://oapi.dingtalk.com/robot/send?access_token=${DINGTALK_ACCESS_TOKEN} -d "${V_JSON}" -w "\n"
59+
60+
# 消息发送模板任务
61+
.pre:
62+
stage: .pre
63+
image: curlimages/curl:7.86.0
64+
script:
65+
- *prepare
66+
- |
67+
V_COLOR="#FF9900"
68+
V_STATUS="启动"
69+
V_TITLE="CI任务启动通知"
70+
- echo "GITLAB_CI_STATUS=FAIL" >> build.env
71+
- *send_request
72+
artifacts:
73+
reports:
74+
dotenv: build.env
75+
76+
.post:
77+
stage: .post
78+
image: curlimages/curl:7.86.0
79+
# 发送ci结束消息
80+
script:
81+
- *prepare
82+
# 不同任务状态设置不同消息标题、颜色
83+
- |
84+
case $GITLAB_CI_STATUS in
85+
ALL_SUCCESS)
86+
V_TITLE="CI任务执行成功通知"
87+
V_STATUS="执行成功"
88+
V_COLOR="#33CC00"
89+
;;
90+
FAIL)
91+
V_TITLE="CI任务执行失败通知"
92+
V_STATUS="执行失败"
93+
V_COLOR="#FF3333"
94+
;;
95+
*)
96+
echo "不支持job状态$GITLAB_CI_STATUS"
97+
exit 1
98+
;;
99+
esac
100+
# 执行耗时计算
101+
- *send_request
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# jenkins-share-library
2+
3+
This repo contains share library used by DevStream plugin "jenkins-pipeline" (thereafter: the plugin). It currently has the following functions:
4+
- send notification to dingtalk when pipeline result is success or failed.
5+
- run test for java project.
6+
- run sonar scanner for java project.
7+
- build docker image by current project, and push this image to image repo.
8+
9+
## Example
10+
```groovy dingtalk
11+
/*
12+
config dingtalk related info, if not set, pipeline result will not be sent
13+
required plugins: dingding-notifications
14+
*/
15+
setting.configNotifyDingtalk([
16+
'robot_id': "dingdingtest", // robotID in jenkins config
17+
'at_user': ""
18+
])
19+
20+
/*
21+
config docker image repo info, if not set, image will not be sent to repo
22+
*/
23+
setting.configImageRepo([
24+
'image_repo': "test.com/library",
25+
'image_auth_secret_name': "docker-config",
26+
])
27+
28+
/*
29+
pipeline generic config
30+
*/
31+
runPipeline([
32+
'enable_test': true, // whether run test for code
33+
'name': "spring-test-github", // this name will be used for image repo name and sonar project name
34+
'enable_sonarqube': true, // whether use sonar to scan code
35+
])
36+
37+
```
38+
39+
## General config variables
40+
41+
| field | description | default_value |
42+
| ---- | ---- | ---- |
43+
| repo_type | whether code repo is github or gitlab, if repo_type is gitlab, we can use gitlab_connection to show jenkins pipeline status in gitlab | |
44+
| name | this name will be used for image repo name and sonar project name | |
45+
| language | the project language, current only support Java | java |
46+
| container_requests_cpu | jenkins worker container requests cpu | 0.3 |
47+
| container_requests_memory | jenkins worker container requests memory | 512Mi |
48+
| container_limit_cpu | jenkins worker container limit cpu | 1 |
49+
| container_limit_memory | jenkins worker container limit memory | 2Gi |
50+
| enable_test | whether run test for code | true |
51+
| enable_sonarqube | whether use sonar to scan code | false |
52+
53+
## Where does this repo come from?
54+
55+
`jenkins-share-library` is synced from https://github.com/devstream-io/devstream/blob/main/staging/jenkins-share-library.
56+
Code changes are made in that location, merged into `devstream-io/devstream` and later synced here.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.devstream.ci
2+
3+
def getChangeString() {
4+
def changeString = ""
5+
def MAX_MSG_LEN = 10
6+
def changeLogSets = currentBuild.changeSets
7+
for (int i = 0; i < changeLogSets.size(); i++) {
8+
def entries = changeLogSets[i].items
9+
for (int j = 0; j < entries.length; j++) {
10+
def entry = entries[j]
11+
truncatedMsg = entry.msg.take(MAX_MSG_LEN)
12+
commitTime = new Date(entry.timestamp).format("yyyy-MM-dd HH:mm:ss")
13+
changeString += " - ${truncatedMsg} [${entry.author} ${commitTime}]\n"
14+
}
15+
}
16+
if (!changeString) {
17+
changeString = " - No new changes"
18+
}
19+
return (changeString)
20+
}
21+
22+
def getCommitIDHead() {
23+
String gitCommit
24+
if (env.GIT_COMMIT) {
25+
gitCommit = env.GIT_COMMIT.substring(0, 8)
26+
} else {
27+
sh "git config --global --add safe.directory '*'"
28+
String gitCommitLang = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
29+
gitCommit = gitCommitLang.substring(0, 8)
30+
}
31+
return gitCommit
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.devstream.ci
2+
3+
def selector(String language){
4+
// config default options for different language
5+
switch(language.toLowerCase()){
6+
case "java":
7+
return javaDefault()
8+
default:
9+
if (!Config.generalSettings.ci_test_command) {
10+
throw new Exception("Language %s language should set ci_test_command and ci_test_options in generalSettings")
11+
}
12+
}
13+
}
14+
15+
16+
def javaDefault() {
17+
return [
18+
ci_test_command: 'mvn',
19+
ci_test_options: '-B test',
20+
ci_test_container_repo: 'maven:3.8.1-jdk-8',
21+
container_requests_cpu: "512m",
22+
container_requests_memory: "2Gi",
23+
container_limit_cpu: "512m",
24+
container_limit_memory: "2Gi",
25+
]
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.devstream.notification
2+
3+
4+
def send(changeString, headMessage, statusMessage, Integer _timeout=60) {
5+
// String buildUser = variable.buildUserName()
6+
String notifyUser = Config.notifySettings.get("at_user")
7+
String robotID = Config.notifySettings.get("robot_id")
8+
List<String> atUsers = [] as String[]
9+
if (notifyUser != null && notifyUser != "") {
10+
atUsers = notifyUser.split(",") as String[]
11+
}
12+
timeout(time: _timeout, unit: 'SECONDS') {
13+
dingtalk (
14+
robot: "${robotID}",
15+
type: 'MARKDOWN',
16+
title: "${env.JOB_NAME}[${env.BRANCH_NAME}]构建通知",
17+
text: [
18+
"# $headMessage",
19+
"# 构建详情",
20+
"- 构建变更: ${changeString}",
21+
"- 构建结果: ${statusMessage}",
22+
// "- 构建人: **${buildUser}**",
23+
"- 持续时间: ${currentBuild.durationString}",
24+
"# 构建日志",
25+
"[日志](${env.BUILD_URL}console)"
26+
],
27+
at: atUsers
28+
)
29+
}
30+
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.devstream.scanner
2+
3+
import com.devstream.ci.Git
4+
5+
def scanner(
6+
String name,
7+
String lang,
8+
String options='') {
9+
try {
10+
println('Info: Preparing SonarQube Scanner')
11+
gitUtil = new Git()
12+
version = gitUtil.getCommitIDHead()
13+
withSonarQubeEnv(){
14+
def private opts
15+
16+
opts = ' -Dsonar.projectKey=' + name
17+
opts += ' -Dsonar.projectName=' + name
18+
opts += ' -Dsonar.projectVersion=' + version
19+
opts += ' -Dsonar.language=' + lang
20+
opts += ' -Dsonar.projectBaseDir=.'
21+
opts += ' -Dsonar.sources=.'
22+
opts += ' -Dsonar.java.binaries=.'
23+
sonar_exec = 'sonar-scanner' + opts + ' ' + options
24+
25+
sh(sonar_exec)
26+
}
27+
}
28+
catch (e) {
29+
println('Error: Failed with SonarQube Scanner')
30+
throw e
31+
}
32+
}
33+
34+
def qualityGateStatus(){
35+
try {
36+
timeout(time: Config.generalSettings.sonarqube_timeout_minutes, unit: 'MINUTES') {
37+
def qg_stats = waitForQualityGate()
38+
if (qg_stats.status != 'SUCCESS') {
39+
println('Error: Pipeline aborted due to quality gate failure: ' + qg.stats)
40+
error "Pipeline aborted due to quality gate failure: ${qg.status}"
41+
}
42+
}
43+
}
44+
catch (e) {
45+
throw e
46+
}
47+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Config is a global config for jenkins pipeline
2+
class Config implements Serializable {
3+
static Map generalSettings = [:]
4+
5+
static Map notifySettings = [:]
6+
7+
static Map imageRepoSettings = [:]
8+
}

0 commit comments

Comments
 (0)