CD(二)
-
- 1. CD
-
- Step 1 - 上传代码
- Step 2 - 下载代码
- Step 3 - 检查代码
- Step 4 - 编译代码
- Step 5 - 上传仓库
- Step 6 - 下载软件
- Step 7 - 制作镜像
- Step 8 - 上传镜像
- Step 9 - 部署服务
- 2. 整体预览
-
- 2.1 预览
-
- 1. 修改代码
- 2. 查看sonarqube检查结果
- 3. 查看nexus仓库
- 4. 查看harbor仓库
- 5. 整体效果
- 2.2 pipeline完整版
- 3. 写在最后...
1. CD
参考前面的流程图,我们正式开始CD的流程
Step 1 - 上传代码
- 安装git
参考 Windows安装Git图文教程 - 配置IDEA
参考 IDEA配置Git,以GitHub远程仓库为例 - 提交代码到本地
Commit
- gitlab账号密码:root/Newm123@
- gitlab创建项目,复制URL
- IDEA push代码
会提示输入gitlab URL 账号密码 - 提交完成
Step 2 - 下载代码
- 在jenkins上创建pipeline项目
- 创建节点,并连接节点
- 配置jenkins gitlab(不配置也可以,因为我们在pipeline中也需要配置)
Dashboard > Manage Jenkins >System
- 配置触发器(不配置也可以,在pipeline中也可以配置)
- pipeline片段
pipeline {
agent {
node {
label 'node-133'}
}
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git credentialsId: 'd3cb7c68-63e5-46d6-a505-d73a89902758', url: 'http://192.168.17.132:8929/devs/myjava.git'
// Run Maven on a Unix agent.
sh "mvn -Dmaven.test.failure.ignore=true clean package"
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
}
}
}
此处把mvn打包也一同配置了,并进行简单测试
[root@node-133 MyJava-Pipeline]# ll target/
total 4
drwxr-xr-x 2 root root 29 Jul 19 05:25 classes
drwxr-xr-x 3 root root 25 Jul 19 05:25 generated-sources
drwxr-xr-x 2 root root 28 Jul 19 05:25 maven-archiver
drwxr-xr-x 3 root root 35 Jul 19 05:25 maven-status
drwxr-xr-x 4 root root 54 Jul 19 05:25 MyJava-latest
-rw-r--r-- 1 root root 3054 Jul 19 05:25 MyJava-latest.war
[root@node-133 MyJava-Pipeline]# pwd
/opt/jenkins/workspace/MyJava-Pipeline
Step 3 - 检查代码
- 启动sonarqube,登录并设置密码
http://192.168.17.133:9090
admin/password
- 部署sonar-scanner
unzip sonar-scanner-cli-4.7.0.2747-linux.zip
cd sonar-scanner-4.7.0.2747-linux/
vim conf/sonar-scanner.properties
[root@dbc-server-554 sonar-scanner-4.7.0.2747-linux]# cat conf/sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://localhost:9090
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
ln -sv /root/docker/sonar/sonar-scanner-4.7.0.2747-linux /usr/local/sonar-scanner
export PATH=/usr/local/sonar-scanner/bin:$PATH
source .bashrc
[root@dbc-server-554 sonar-scanner-4.7.0.2747-linux]# sonar-scanner -h
INFO:
INFO: usage: sonar-scanner [options]
INFO:
...
- 生成扫描命令
mvn clean verify sonar:sonar \
-Dsonar.projectKey=MyJava \
-Dsonar.host.url=http://192.168.17.133:9090 \
-Dsonar.login=6b1dddd3274606082753ff907ee1b0380c28298a
部署jenkins pipeline
- pipeline片段
//定义http方法
def HttpReq(reqType,reqUrl,reqBody){
sonarServer = "http://192.168.17.133:9090/api"
// 可以不加authentication认证,因为默认不需要
result = httpRequest consoleLogResponseBody: true,
httpMode: 'GET',
responseHandle: 'NONE',
url: "${sonarServer}/${reqUrl}"
// customHeaders:[[name:'Authorization', value:"Basic ${credentials}"]]
return result
}
//获取Sonar质量阈状态
def GetProjectStatus(projectName){
apiUrl = "project_branches/list?project=${projectName}"
response = HttpReq("GET",apiUrl,'')
response = readJSON text: """${response.content}"""
result = response["branches"][0]["status"]["qualityGateStatus"]
println(response)
return result
}
pipeline