关于java:devops实践-teamcity实现持续集成

8次阅读

共计 5537 个字符,预计需要花费 14 分钟才能阅读完成。

解决了什么问题?

疾速 ci cd ;

团队合作效率更高,更快的集成,更快的交付;走 gitops 模式;

支流的 CICD 过程:

teamcity 的架构:

装置形式

docker 的形式装置疾速

装置 server 端

mkdir -p /data/teamcity_server/datadir  /data/teamcity/logs


docker run -it --name teamcity-server \
-v /data/teamcity_server/datadir:/data/teamcity_server/datadir \
-v /data/teamcity_server/logs:/opt/teamcity/logs \
-p 8111:8111 \
jetbrains/teamcity-server:EAP

而后失去拜访的 url, 前面装置客户端的时候须要用到。

比方这里是:http://172.31.12.168:8111

数据库抉择选用默认的 hsqldb, 这里只有挂载的目录不丢,重新安装之后数据也是存在的;

装置 client 端

mkdir -p /data/teamcity_agent/conf
chmod -R 777 /data/teamcity_agent/conf

docker run -it -e SERVER_URL="http://172.31.12.168:8111"  \
    -v /data/teamcity_agent/conf:/data/teamcity_agent/conf  \
    jetbrains/teamcity-agent:EAP

能够装置多个;

然而业余版本的限定了 3 个,所以为了前期的遍历,最多不超过 3 个客户端吧!

装置结束之后须要在 server 端对 agent 进行受权能力应用。

间接备注即可退出到客户端池。

而后即可退出到服务端的客户端池子。构建的工作的执行即可依照并行度为 3 进行执行。

也能够物理化部署,不会有 docker 内核的问题。

这个地位能够下载物理版本的客户端安装包。联合文档批改配置参数即可;

次要批改的是服务端 server 的地址和客户端的利用名称;
地位:/data/team_agent4/conf/buildAgent.properties

启动指令:./bin/agent.sh start

而后在服务端受权即可应用。

应用初体验

一个后端工程的 CI 和 CD 过程:
上面是实际过程:

创立工程

而后贴入你的 gitlab 或者 github 仓库地址;

填写一个有只读权限的账号和明码。

配置 CICD 形成脚本

1 后端打 jar 包

2 打后端 docker 镜像

3 前端 npm 打包

4 前端镜像制作

5 推送前端和后端镜像到镜像仓库

6 公布到 k8s 环境

7 动员钉钉告诉到我的项目群

整体的 kotlin 代码

package _Self.buildTypes

import jetbrains.buildServer.configs.kotlin.v2019_2.*
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.MavenBuildStep
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.dockerCommand
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.maven
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.nodeJS
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.script
import jetbrains.buildServer.configs.kotlin.v2019_2.triggers.vcs

object Build : BuildType({
    name = "appBuild"
    description = "构建"

    allowExternalStatus = true
    artifactRules = "app-tp/start/target/app-tp.jar => app-tp.jar"
    publishArtifacts = PublishMode.SUCCESSFUL

    vcs {root(HttpGitlabH3yunComHermesSystemAppTpGitRefsHeadsMaster)

        showDependenciesChanges = true
    }

    steps {
        maven {
            name = "打 jar 包"
            goals = "clean install -Dmaven.test.skip=true -U"
            pomLocation = "app-tp/pom.xml"
            runnerArgs = "-Dmaven.test.failure.ignore=true"
            workingDir = "app-tp"
            userSettingsSelection = "我的 nexus 配置"
            localRepoScope = MavenBuildStep.RepositoryScope.MAVEN_DEFAULT
            isIncremental = true
            jdkHome = "%env.JDK_18%"
            dockerImagePlatform = MavenBuildStep.ImagePlatform.Linux
            dockerPull = true
        }
        dockerCommand {
            name = "制作后端 docker 镜像"
            commandType = build {
                source = file {path = "app-tp/app.Dockerfile"}
                namesAndTags = "registry.cn-shenzhen.aliyuncs.com/cloudpivot/app-tp:tptest"
                commandArgs = "--pull"
            }
        }
        nodeJS {
            name = "前端 npm 打包"
            shellScript = """
                cd front-tp
                npm install
                npm run build
            """.trimIndent()
            dockerPull = true
        }
        dockerCommand {
            name = "制作前端 docker 镜像"
            commandType = build {
                source = file {path = "front-tp/front.Dockerfile"}
                namesAndTags = "registry.cn-shenzhen.aliyuncs.com/cloudpivot/front-tp:tptest"
                commandArgs = "--pull"
            }
        }
        script {
            name = "登录推送到近程镜像仓库"
            scriptContent = """docker login -u="aaaa" -p xxxxyun registry.cn-shenzhen.aliyuncs.com
                
                echo "推送到近程仓库"
                docker push registry.cn-shenzhen.aliyuncs.com/cloudpivot/app-tp:tptest
                docker push registry.cn-shenzhen.aliyuncs.com/cloudpivot/front-tp:tptest
                
                echo "删除本地镜像 === 节约磁盘空间 ===="
                docker images | grep app-tp | awk '{print ${'$'}3 }' | xargs docker rmi
                docker images | grep front-tp | awk '{print ${'$'}3 }' | xargs docker rmi
            """.trimIndent()}
        script {
            name = "更新 k8s 环境"
            scriptContent = """
                cd %system.teamcity.build.checkoutDir%
                cd deploy
                sh app_tp_deploy.sh
                sh front_tp_deploy.sh
            """.trimIndent()}
        script {
            name = "推送钉钉告诉到我的项目群"
            scriptContent = """url='https://oapi.dingtalk.com/robot/send?access_token=b0dc2aee487a842dd5648566ade86xxxxxxx'
                programe= 技术治理平台
                server=tptest.cloudpivot.cn
                content=%teamcity.build.branch%
                buildInfo=%vcsroot.useAlternates%
                 
                function sendDingtalk(){curl ${'$'}{1} \
                       -H 'Content-Type: application/json' \
                       -d "{\"msgtype\": \"text\", 
                        \"text\": {\"content\": \"音讯内容: 我的项目 -${'$'}{2}, 域名 -${'$'}{3}, 分支 -${'$'}{4} 更新内容 -${'$'}{5}\"
                         },
                         \"isAtAll\": true, 
                      }"
                }
                
                sendDingtalk ${'$'}{url} ${'$'}{programe} ${'$'}{server} ${'$'}{content} ${'$'}{content} ${'$'}{buildInfo}
            """.trimIndent()}
    }

    triggers {
        vcs {branchFilter = "+:refs/heads/test"}
    }
})

小结

teamcity 业余版本限度 3 个执行客户端,100 个构建配置,适宜小型团队;

用户体验比拟好,界面比拟难看。

自动检测代码变动,进行构建;(能够大大提高 CI 效率)

比方推送了一个批改到某个分支,间接就公布到了集成测试环境了。

pk

(开发结束一个性能,而后合并到集成测试分支,再到 CICD 零碎点公布,碰到问题再惊起一滩鸥鹭)

更优雅。

钉钉音讯告诉

拉一个钉钉群,减少一个机器人:

残缺之后,即可拿到告诉 token:

https://oapi.dingtalk.com/robot/send?access_token=c30f5008258474da14e65d3141536953b79df3bf3ab64f33a583e83165b19665

筹备的 shell 脚本:

url='https://oapi.dingtalk.com/robot/send?access_token=c30f5008258474da14e65d3141536953b79df3bf3ab64f33a583e83165b19665'
programe= 技术治理平台
server=tptest.cloudpivot.cn
content='程序中断'
 
function sendDingtalk(){curl ${1} \
       -H 'Content-Type: application/json' \
       -d "{\"msgtype\": \"text\", 
        \"text\": {\"content\": \"音讯内容: 我的项目 -${2}, 服务地址 -${3}, 更新内容 -${4}\"
         },
         \"isAtAll\": true, 
      }"
}

sendDingtalk ${url} ${programe} ${server} ${content}


理论例子:

url='https://oapi.dingtalk.com/robot/send?access_token=b0dc2aee487a842dd5648566ade86e2217dac868c0ffdcab5138cb7eab163978'
programe= 技术治理平台
server=tptest.cloudpivot.cn
content=%teamcity.build.branch%
buildInfo=%vcsroot.useAlternates%
 
function sendDingtalk(){curl ${1} \
       -H 'Content-Type: application/json' \
       -d "{\"msgtype\": \"text\", 
        \"text\": {\"content\": \"音讯内容: 我的项目 -${2}, 域名 -${3}, 分支 -${4} 更新内容 -${5}\"
         },
         \"isAtAll\": true, 
      }"
}

sendDingtalk ${url} ${programe} ${server} ${content} ${content} ${buildInfo}

告诉成果截图:

资料

使用手册:(必看英文资料)

https://www.jetbrains.com/help/teamcity/2021.1/configure-and-run-your-first-build.html

teamcity 之旅(必看中文资料)
https://developer.aliyun.com/article/738443

腾讯云搭建 teamcity 过程:( 特权容器解决 docker agent 无奈打镜像的问题
https://blog.csdn.net/sD7O95O/article/details/88264986

钉钉机器人告诉文档:
https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

程序启动之后通过 shell 告诉到钉钉群:
https://blog.csdn.net/weixin_37836950/article/details/107924910

原创不易,关注诚可贵,转发价更高!转载请注明出处,让咱们互通有无,共同进步,欢送沟通交流。

正文完
 0