目录
前言
没有其他的目的, 就是想折腾. 在折腾之前需要理解两个概念, teamCity 和teamAgent.
- teamCity: 管理构建代码,构建步骤, 和调度构建的服务
- teamAgent: 真正执行构建的服务, 接受teamAgent调度管理, 可以单独安装在服务器上, 如果要构建的任务很多,可以增加很多的teamAgent.
安装teamCity
查看官网看到提供有docker镜像, 那就简单了.
配置安装docker镜像
按照docker hub 中的文档操作,
# 下载镜像
docker pull jetbrains/teamcity-server
# 运行镜像
docker run -it --name teamcity-server-instance \
-v /home/xiezc/teamcity/data:/data/teamcity_server/datadir \
-v /home/xiezc/teamcity/log:/opt/teamcity/logs \
-p 8111:8111 \
jetbrains/teamcity-server
我的小服务器比较差, 运行时间比较长. 耐心等待.
初始化
docker 镜像启动后 , 在浏览器打开地址 http://yourhost:8111 就会进入配置页面.
忘记截图了, 不想再操作一遍, 这里就不贴了. 在配置页面会有下面的几步需要你操作:
- 接受协议
- 选择数据库, 如果图方便可以直接选择内嵌数据HsqlDB. 而我选择的是我自己的postgreSql数据. 并且需要为teamcity单独建立数据库.
- 设置初始化用户名和密码,
我的这三个步骤过程很长, 留意docker镜像的控制台, 如果没有报错就耐心等待.
安装teamAgent
官方docker方式安装
同样适用docker方式来安装
docker pull jetbrains/teamcity-agent
docker run -it -e SERVER_URL="yourhost:8111" \
-v /home/xiezc/teamagent/conf:/data/teamcity_agent/conf \
jetbrains/teamcity-agent
注意docker启动后, 需要在teamCity的webUI网页中认证下, agent才算链接上了teamCity服务
下面的截图是我的agent链接后的页面
但是默认的docker镜像是只安装了jdk8, 现在个人折腾的项目都是11了, 无法打包啊, 于是我准备换下, 思路有两个:
- 进入这docker镜像中, 在镜像内部在安装个jdk11, 在webUI中可以指定jdk
- 自己创建个安装了jdk11 又安装了agent后的docker镜像,
我选择了方式2 , 下面是我安装好了后的截图
我的agent docker镜像
FROM openjdk:11
USER root
RUN wget -O teamAgent.zip http://www.xiezc.xyz:8111/update/buildAgent.zip \
&& mkdir /home/teamAgent && unzip -d /home/teamAgent teamAgent.zip
VOLUME /home/teanAgent/conf
ENTRYPOINT ["sh", "/home/teamAgent/bin/agent.sh","run"]
agent的配置文件
注意agent的配置文件如下:
## TeamCity build agent configuration file
## buildAgent.dist.properties
######################################
# Required Agent Properties #
######################################
## The address of the TeamCity server. The same as is used to open TeamCity web interface in the browser.
## Example: serverUrl=https://buildserver.mydomain.com:8111
serverUrl=http://yourhost:8111/
## The unique name of the agent used to identify this agent on the TeamCity server
## Use blank name to let server generate it.
## By default, this name would be created from the build agent's host name
name= dockerxzc
## buildAgent.dist.properties
## Container directory to create default checkout directories for the build configurations.
## TeamCity agent assumes ownership of the directory and will delete unknown directories inside.
workDir=../work
## Container directory for the temporary directories.
## TeamCity agent assumes ownership of the directory. The directory may be cleaned between the builds.
tempDir=../temp
## Container directory for agent state files and caches.
## TeamCity agent assumes ownership of the directory and can delete content inside.
systemDir=../system
######################################
# Optional Agent Properties #
######################################
## A token which is used to identify this agent on the TeamCity server for agent authorization purposes.
## It is automatically generated and saved back on the first agent connection to the server.
authorizationToken=
######################################
# Default Build Properties #
######################################
## All properties starting with "system.name" will be passed to the build script as "name"
## All properties starting with "env.name" will be set as environment variable "name" for the build process
## Note that value should be properly escaped. (use "\\" to represent single backslash ("\"))
## More on file structure: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)
# Build Script Properties
#system.exampleProperty=example Value
# Environment Variables
#env.exampleEnvVar=example Env Value
启动agent后就可以连上teamcity的webUI了, 你还需要在web中authorize认证下agent后就可以使用了.
配置构建和使用
登录进去之后的页面如下
创建构建项目
我选择的是gitee的url, 我简单的配置了自己的gitee的账号和密码以及项目地址.
配置构建步骤
这里有超多的内置的构建步骤, 我的项目是maven, 所以我直接选择maven类型, 可以配置maven的版本, 选择agent中的jdk版本, 等等
部署到服务器
部署到服务可以看成是一个构建的步骤, 是有在maven打包承购后的构建步骤, 可以在新增一个 build step , 然后这个step的操作类型, 我直接选择ssh upload 到我的服务器上面去运行.
我的配置如下:
运行打包后, 这样就会第一步使用maven构建jar包, 第二步使用scp把jar包上传到服务, 你可以配置第三步, 使用ssh exec登录到服务器后执行命令启动jar包,
可以看到我的服务器中已经有了自己的jar包了
结束
整个篇幅太长了, 由于我的几乎所有应用都是使用docker方式部署的, 这个方式我不太喜欢, 所以后续我计划换成docker方式部署, 自动打包成docker镜像, 并上传到docker的阿里云镜像仓库, 之后再登录服务器拉取下来镜像执行.
后面我还会出一篇文章讲述如何上传部署到阿里云的docker镜像仓库.